class Selenium::WebDriver::Chromium::Profile

def [](key)

def [](key)
  parts = key.split('.')
  parts.inject(prefs) { |a, e| a.fetch(e) }
end

def []=(key, value)

def []=(key, value)
  parts = key.split('.')
  parts[0..-2].inject(prefs) { |a, e| a[e] ||= {} }[parts.last] = value
end

def add_encoded_extension(encoded)

def add_encoded_extension(encoded)
  @encoded_extensions << encoded
end

def add_extension(path)

def add_extension(path)
  raise Error::WebDriverError, "could not find extension at #{path.inspect}" unless File.file?(path)
  @extensions << path
end

def as_json(*)

def as_json(*)
  extensions = @extensions.map do |crx_path|
    File.open(crx_path, 'rb') { |crx_file| Base64.strict_encode64 crx_file.read }
  end
  extensions.concat(@encoded_extensions)
  opts = {'directory' => directory || layout_on_disk}
  opts['extensions'] = extensions if extensions.any?
  opts
end

def directory

def directory
  @directory || layout_on_disk
end

def initialize(model = nil)

def initialize(model = nil)
  @model = verify_model(model)
  @extensions = []
  @encoded_extensions = []
  @directory = nil
end

def layout_on_disk

def layout_on_disk
  @directory = @model ? create_tmp_copy(@model) : Dir.mktmpdir('webdriver-chrome-profile')
  FileReaper << @directory
  write_prefs_to @directory
  @directory
end

def prefs

def prefs
  @prefs ||= read_model_prefs
end

def prefs_file_for(dir)

def prefs_file_for(dir)
  File.join dir, 'Default', 'Preferences'
end

def read_model_prefs

def read_model_prefs
  return {} unless @model
  JSON.parse File.read(prefs_file_for(@model))
end

def write_prefs_to(dir)

def write_prefs_to(dir)
  prefs_file = prefs_file_for(dir)
  FileUtils.mkdir_p File.dirname(prefs_file)
  File.open(prefs_file, 'w') { |file| file << JSON.generate(prefs) }
end