Class: Selenium::WebDriver::Safari::Extensions Private

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/safari/extensions.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: Backup

Constant Summary collapse

PLIST =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"  <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n  <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n  <plist version=\"1.0\">\n  <dict>\n    <key>Available Updates</key>\n    <dict>\n      <key>Last Update Check Time</key>\n      <real>370125644.75941497</real>\n      <key>Updates List</key>\n      <array/>\n    </dict>\n    <key>Installed Extensions</key>\n    <array>\n      %s\n    </array>\n    <key>Version</key>\n    <integer>1</integer>\n  </dict>\n  </plist>\n"
PLIST_EXTENSION_LINE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"  <dict>\n    <key>Added Non-Default Toolbar Items</key>\n    <array/>\n    <key>Archive File Name</key>\n    <string>%s.safariextz</string>\n    <key>Bundle Directory Name</key>\n    <string>%s.safariextension</string>\n    <key>Enabled</key>\n    <true/>\n    <key>Hidden Bars</key>\n    <array/>\n    <key>Removed Default Toolbar Items</key>\n    <array/>\n  </dict>\n"

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Extensions

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Extensions.



66
67
68
69
70
71
72
# File 'lib/selenium/webdriver/safari/extensions.rb', line 66

def initialize(opts = {})
  @data_dir   = opts.data_dir || safari_data_dir
  @skip       = opts.skip_extension_installation?
  @extensions = opts.extensions
  @backup     = Backup.new
  @installed  = false
end

Instance Method Details

#extension_destinationObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



130
131
132
# File 'lib/selenium/webdriver/safari/extensions.rb', line 130

def extension_destination
  install_directory.join('WebDriver.safariextz')
end

#extension_sourceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



126
127
128
# File 'lib/selenium/webdriver/safari/extensions.rb', line 126

def extension_source
  Safari.resource_path.join('SafariDriver.safariextz')
end

#installObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/selenium/webdriver/safari/extensions.rb', line 74

def install
  return if @installed
  installed_extensions = []

  if install_directory.exist?
    @backup.backup install_directory
  end

  install_directory.mkpath

  unless @skip
    extension_destination.rmtree if extension_destination.exist?
    FileUtils.cp extension_source.to_s, extension_destination.to_s

    installed_extensions << extension_destination
  end

  @extensions.each do |extension|
    target = install_directory.join(extension.basename)

    if extension.expand_path == target.expand_path
      @backup.backup(target)
    else
      FileUtils.cp extension, target
    end

    installed_extensions << target
  end

  plist_destination.open('w') do |io|
    extension_lines = installed_extensions.map do |ext|
      name = ext.basename('.safariextz').to_s
      PLIST_EXTENSION_LINE % [name, name]
    end
    io << PLIST % extension_lines.join("\n")
  end

  Platform.exit_hook { uninstall }
  @installed = true
end

#install_directoryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/selenium/webdriver/safari/extensions.rb', line 138

def install_directory
  @install_directory ||= (
    data_dir = Pathname.new(@data_dir || safari_data_dir)

    unless data_dir.exist? && data_dir.directory?
      raise Errno::ENOENT, "Safari data directory not found at #{data_dir.to_s}"
    end

    data_dir.join('Extensions')
  )
end

#plist_destinationObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



134
135
136
# File 'lib/selenium/webdriver/safari/extensions.rb', line 134

def plist_destination
  install_directory.join('Extensions.plist')
end

#safari_data_dirObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/selenium/webdriver/safari/extensions.rb', line 150

def safari_data_dir
  current = Platform.os

  case current
  when :macosx
    Pathname.new(Platform.home).join('Library/Safari')
  when :windows
    Pathname.new(ENV['APPDATA']).join('Apple Computer/Safari')
  else
    raise Error::WebDriverError, "unsupported platform: #{current}"
  end
end

#uninstallObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



115
116
117
118
119
120
121
122
123
124
# File 'lib/selenium/webdriver/safari/extensions.rb', line 115

def uninstall
  return unless @installed

  install_directory.rmtree if install_directory.exist?
  @backup.restore_all

  nil
ensure
  @installed = false
end