Path: blob/trunk/rb/lib/selenium/webdriver/firefox/extension.rb
1865 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 Firefox22#23# @api private24#2526class Extension27NAMESPACE = 'http://www.mozilla.org/2004/em-rdf#'2829def initialize(path)30raise Error::WebDriverError, "could not find extension at #{path.inspect}" unless File.exist?(path)3132@path = path33@should_reap_root = false34end3536def write_to(extensions_dir)37root_dir = create_root38ext_path = File.join extensions_dir, read_id(root_dir)3940FileUtils.rm_rf ext_path41FileUtils.mkdir_p File.dirname(ext_path), mode: 0o70042FileUtils.cp_r root_dir, ext_path4344FileReaper.reap(root_dir) if @should_reap_root45end4647private4849def create_root50if File.directory? @path51@path52else53unless Zipper::EXTENSIONS.include? File.extname(@path)54raise Error::WebDriverError, "expected #{Zipper::EXTENSIONS.join(' or ')}, got #{@path.inspect}"55end5657@should_reap_root = true58Zipper.unzip(@path)59end60end6162def read_id(directory)63read_id_from_install_rdf(directory) || read_id_from_manifest_json(directory)64end6566def read_id_from_install_rdf(directory)67rdf_path = File.join(directory, 'install.rdf')68return unless File.exist?(rdf_path)6970doc = REXML::Document.new(File.read(rdf_path))71namespace = doc.root.namespaces.key(NAMESPACE)7273if namespace74id_node = REXML::XPath.first(doc, "//#{namespace}:id")75return id_node.text if id_node7677attr_node = REXML::XPath.first(doc, "//@#{namespace}:id")78return attr_node.value if attr_node79end8081raise Error::WebDriverError, "cannot locate extension id in #{rdf_path}"82end8384def read_id_from_manifest_json(directory)85manifest_path = File.join(directory, 'manifest.json')86return unless File.exist?(manifest_path)8788manifest = JSON.parse(File.read(manifest_path))89applications_gecko_id(manifest) || name_and_version(manifest)90end9192def applications_gecko_id(manifest)93manifest.dig('applications', 'gecko', 'id')&.strip94end9596def name_and_version(manifest)97[manifest['name'].delete(' '), manifest['version']].join('@')98end99end # Extension100end # Firefox101end # WebDriver102end # Selenium103104105