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

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

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.



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

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.



132
133
134
# File 'lib/selenium/webdriver/safari/extensions.rb', line 132

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.



128
129
130
# File 'lib/selenium/webdriver/safari/extensions.rb', line 128

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.



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
114
115
# File 'lib/selenium/webdriver/safari/extensions.rb', line 76

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.



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

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.



136
137
138
# File 'lib/selenium/webdriver/safari/extensions.rb', line 136

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.



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

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.



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

def uninstall
  return unless @installed

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

  nil
ensure
  @installed = false
end