Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/firefox/extension.rb
1865 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 Firefox
23
#
24
# @api private
25
#
26
27
class Extension
28
NAMESPACE = 'http://www.mozilla.org/2004/em-rdf#'
29
30
def initialize(path)
31
raise Error::WebDriverError, "could not find extension at #{path.inspect}" unless File.exist?(path)
32
33
@path = path
34
@should_reap_root = false
35
end
36
37
def write_to(extensions_dir)
38
root_dir = create_root
39
ext_path = File.join extensions_dir, read_id(root_dir)
40
41
FileUtils.rm_rf ext_path
42
FileUtils.mkdir_p File.dirname(ext_path), mode: 0o700
43
FileUtils.cp_r root_dir, ext_path
44
45
FileReaper.reap(root_dir) if @should_reap_root
46
end
47
48
private
49
50
def create_root
51
if File.directory? @path
52
@path
53
else
54
unless Zipper::EXTENSIONS.include? File.extname(@path)
55
raise Error::WebDriverError, "expected #{Zipper::EXTENSIONS.join(' or ')}, got #{@path.inspect}"
56
end
57
58
@should_reap_root = true
59
Zipper.unzip(@path)
60
end
61
end
62
63
def read_id(directory)
64
read_id_from_install_rdf(directory) || read_id_from_manifest_json(directory)
65
end
66
67
def read_id_from_install_rdf(directory)
68
rdf_path = File.join(directory, 'install.rdf')
69
return unless File.exist?(rdf_path)
70
71
doc = REXML::Document.new(File.read(rdf_path))
72
namespace = doc.root.namespaces.key(NAMESPACE)
73
74
if namespace
75
id_node = REXML::XPath.first(doc, "//#{namespace}:id")
76
return id_node.text if id_node
77
78
attr_node = REXML::XPath.first(doc, "//@#{namespace}:id")
79
return attr_node.value if attr_node
80
end
81
82
raise Error::WebDriverError, "cannot locate extension id in #{rdf_path}"
83
end
84
85
def read_id_from_manifest_json(directory)
86
manifest_path = File.join(directory, 'manifest.json')
87
return unless File.exist?(manifest_path)
88
89
manifest = JSON.parse(File.read(manifest_path))
90
applications_gecko_id(manifest) || name_and_version(manifest)
91
end
92
93
def applications_gecko_id(manifest)
94
manifest.dig('applications', 'gecko', 'id')&.strip
95
end
96
97
def name_and_version(manifest)
98
[manifest['name'].delete(' '), manifest['version']].join('@')
99
end
100
end # Extension
101
end # Firefox
102
end # WebDriver
103
end # Selenium
104
105