Path: blob/trunk/rb/lib/selenium/webdriver/chromium/profile.rb
1856 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21module Chromium22#23# @private24#2526class Profile27include ProfileHelper2829def initialize(model = nil)30@model = verify_model(model)31@extensions = []32@encoded_extensions = []33@directory = nil34end3536def add_extension(path)37raise Error::WebDriverError, "could not find extension at #{path.inspect}" unless File.file?(path)3839@extensions << path40end4142def add_encoded_extension(encoded)43@encoded_extensions << encoded44end4546def directory47@directory || layout_on_disk48end4950#51# Set a preference in the profile.52#53# See https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc54#5556def []=(key, value)57parts = key.split('.')58parts[0..-2].inject(prefs) { |a, e| a[e] ||= {} }[parts.last] = value59end6061def [](key)62parts = key.split('.')63parts.inject(prefs) { |a, e| a.fetch(e) }64end6566def layout_on_disk67@directory = @model ? create_tmp_copy(@model) : Dir.mktmpdir('webdriver-chrome-profile')68FileReaper << @directory6970write_prefs_to @directory7172@directory73end7475def as_json(*)76extensions = @extensions.map do |crx_path|77File.open(crx_path, 'rb') { |crx_file| Base64.strict_encode64 crx_file.read }78end7980extensions.concat(@encoded_extensions)8182opts = {'directory' => directory || layout_on_disk}83opts['extensions'] = extensions if extensions.any?84opts85end8687private8889def write_prefs_to(dir)90prefs_file = prefs_file_for(dir)9192FileUtils.mkdir_p File.dirname(prefs_file)93File.open(prefs_file, 'w') { |file| file << JSON.generate(prefs) }94end9596def prefs97@prefs ||= read_model_prefs98end99100def read_model_prefs101return {} unless @model102103JSON.parse File.read(prefs_file_for(@model))104end105106def prefs_file_for(dir)107File.join dir, 'Default', 'Preferences'108end109end # Profile110end # Chromium111end # WebDriver112end # Selenium113114115