Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/chromium/profile.rb
1856 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
module Chromium
23
#
24
# @private
25
#
26
27
class Profile
28
include ProfileHelper
29
30
def initialize(model = nil)
31
@model = verify_model(model)
32
@extensions = []
33
@encoded_extensions = []
34
@directory = nil
35
end
36
37
def add_extension(path)
38
raise Error::WebDriverError, "could not find extension at #{path.inspect}" unless File.file?(path)
39
40
@extensions << path
41
end
42
43
def add_encoded_extension(encoded)
44
@encoded_extensions << encoded
45
end
46
47
def directory
48
@directory || layout_on_disk
49
end
50
51
#
52
# Set a preference in the profile.
53
#
54
# See https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc
55
#
56
57
def []=(key, value)
58
parts = key.split('.')
59
parts[0..-2].inject(prefs) { |a, e| a[e] ||= {} }[parts.last] = value
60
end
61
62
def [](key)
63
parts = key.split('.')
64
parts.inject(prefs) { |a, e| a.fetch(e) }
65
end
66
67
def layout_on_disk
68
@directory = @model ? create_tmp_copy(@model) : Dir.mktmpdir('webdriver-chrome-profile')
69
FileReaper << @directory
70
71
write_prefs_to @directory
72
73
@directory
74
end
75
76
def as_json(*)
77
extensions = @extensions.map do |crx_path|
78
File.open(crx_path, 'rb') { |crx_file| Base64.strict_encode64 crx_file.read }
79
end
80
81
extensions.concat(@encoded_extensions)
82
83
opts = {'directory' => directory || layout_on_disk}
84
opts['extensions'] = extensions if extensions.any?
85
opts
86
end
87
88
private
89
90
def write_prefs_to(dir)
91
prefs_file = prefs_file_for(dir)
92
93
FileUtils.mkdir_p File.dirname(prefs_file)
94
File.open(prefs_file, 'w') { |file| file << JSON.generate(prefs) }
95
end
96
97
def prefs
98
@prefs ||= read_model_prefs
99
end
100
101
def read_model_prefs
102
return {} unless @model
103
104
JSON.parse File.read(prefs_file_for(@model))
105
end
106
107
def prefs_file_for(dir)
108
File.join dir, 'Default', 'Preferences'
109
end
110
end # Profile
111
end # Chromium
112
end # WebDriver
113
end # Selenium
114
115