Adding support for launching firefox from webrtc.DEPS.

This is required for the firefox interop test being added in
https://codereview.chromium.org/111383003/.

This is only intended to work for Linux for now. We will need some light
redesign to get this to work on Windows as well.

R=kjellander@chromium.org

Review URL: https://codereview.chromium.org/114293002

git-svn-id: http://src.chromium.org/svn/trunk/deps/third_party/webrtc/webrtc.DEPS@240895 4ff67af0-8c30-449e-8e8b-ad334ec8d88c
diff --git a/DEPS b/DEPS
index cb65df8..bdd7abe 100644
--- a/DEPS
+++ b/DEPS
@@ -10,6 +10,18 @@
 deps = {
   "src/third_party/webrtc_apprtc":
     (Var("googlecode_url") % "webrtc") + "/trunk/samples/js",
+  "webrtc.DEPS/third_party/manifestdestiny":
+    "/trunk/deps/third_party/manifestdestiny@240893",
+  "webrtc.DEPS/third_party/mozdownload":
+    "/trunk/deps/third_party/mozdownload@240893",
+  "webrtc.DEPS/third_party/mozinfo":
+    "/trunk/deps/third_party/mozinfo@240893",
+  "webrtc.DEPS/third_party/mozprocess":
+    "/trunk/deps/third_party/mozprocess@240893",
+  "webrtc.DEPS/third_party/mozprofile":
+    "/trunk/deps/third_party/mozprofile@240893",
+  "webrtc.DEPS/third_party/mozrunner":
+    "/trunk/deps/third_party/mozrunner@240893",
 }
 
 deps_os = {
@@ -49,4 +61,12 @@
                "--bucket", "chromium-webrtc-resources",
                "src/resources"],
   },
+  {
+    # Download firefox for the Firefox AppRTC test.
+    "pattern": ".",
+    "action" : ["python",
+                "webrtc.DEPS/download_firefox_nightly.py",
+                "-t", 
+                "firefox-nightly"],
+  },
 ]
diff --git a/download_firefox_nightly.py b/download_firefox_nightly.py
new file mode 100755
index 0000000..93da3b8
--- /dev/null
+++ b/download_firefox_nightly.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+# Copyright 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Downloads a Firefox Nightly build for the current platform."""
+
+import os
+import shutil
+import sys
+import subprocess
+import tarfile
+import zipfile
+
+from optparse import OptionParser
+
+BASE_DIR = os.path.dirname(os.path.abspath(__file__))
+THIRD_PARTY_DIR = os.path.abspath(os.path.join(BASE_DIR, 'third_party'))
+
+sys.path.append(os.path.join(THIRD_PARTY_DIR, 'mozdownload'))
+sys.path.append(os.path.join(THIRD_PARTY_DIR, 'mozinfo'))
+
+from mozdownload import scraper
+
+
+def main():
+  usage = 'usage: %prog -t <target_dir>'
+  parser = OptionParser(usage)
+  parser.add_option('-t', '--target-dir',
+                    help=('Target directory to put the downloaded and extracted'
+                          ' folder with the Firefox Nightly build in.'))
+  parser.add_option('-f', '--force', action='store_true',
+                    help=('Force download even if the current nightly is '
+                          'already downloaded.'))
+  options, _args = parser.parse_args()
+  if not options.target_dir:
+    parser.error('You must specify the target directory.')
+
+  target_dir = options.target_dir
+  if not os.path.isdir(target_dir):
+    os.mkdir(target_dir)
+
+  downloader = scraper.DailyScraper(directory=target_dir, version=None)
+  firefox_archive = os.path.join(target_dir,
+                                 downloader.build_filename(downloader.binary))
+
+  if os.path.exists(firefox_archive) and not options.force:
+    print 'Skipping download as %s is already downloaded.' % firefox_archive
+    return 0
+
+  downloader.download()
+  print 'Downloaded %s' % firefox_archive
+
+  # Extract the archive.
+  if sys.platform == 'darwin':
+    volume = '/Volumes/Nightly'
+    firefox_executable = '%s/FirefoxNightly.app' % target_dir
+
+    # Unmount any previous downloads.
+    subprocess.call(['hdiutil', 'detach', volume])
+
+    subprocess.check_call(['hdiutil', 'attach', firefox_archive])
+    shutil.copytree('%s/FirefoxNightly.app' % volume, firefox_executable)
+    subprocess.check_call(['hdiutil', 'detach', volume])
+  elif sys.platform == 'linux2':
+    tar_archive = tarfile.open(firefox_archive, 'r:bz2')
+    tar_archive.extractall(path=target_dir)
+  elif sys.platform == 'win32':
+    zip_archive = zipfile.ZipFile(firefox_archive)
+    zip_archive.extractall(path=target_dir)
+  else:
+    print >> sys.stderr, 'Unsupported platform: %s' % sys.platform
+    return 1
+  
+  print 'Extracted %s' % firefox_archive
+  return 0
+
+if __name__ == '__main__':
+  sys.exit(main())
diff --git a/run_firefox_webrtc.py b/run_firefox_webrtc.py
new file mode 100755
index 0000000..91e71c3
--- /dev/null
+++ b/run_firefox_webrtc.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+# Copyright 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Launches Firefox and browses to an URL."""
+
+import os
+import signal
+import sys
+
+from optparse import OptionParser
+
+BASE_DIR = os.path.dirname(os.path.abspath(__file__))
+THIRD_PARTY_DIR = os.path.abspath(os.path.join(BASE_DIR, 'third_party'))
+
+sys.path.append(os.path.join(THIRD_PARTY_DIR, 'manifestdestiny'))
+sys.path.append(os.path.join(THIRD_PARTY_DIR, 'mozinfo'))
+sys.path.append(os.path.join(THIRD_PARTY_DIR, 'mozprocess'))
+sys.path.append(os.path.join(THIRD_PARTY_DIR, 'mozprofile'))
+sys.path.append(os.path.join(THIRD_PARTY_DIR, 'mozrunner'))
+
+from mozprofile import profile
+from mozrunner import runner
+
+WEBRTC_PREFERENCES = {
+    # Automatically gives permission to access the camera/microphone and
+    # bypasses the permission/selection dialog.
+    'media.navigator.permission.disabled': True,
+}
+
+def main():
+  usage = 'usage: %prog --binary <firefox_executable> --webpage <url>'
+  parser = OptionParser(usage)
+  parser.add_option('-b', '--binary',
+                    help=('Firefox executable to run.'))
+  parser.add_option('-w', '--webpage',
+                    help=('Web page to browse to.'))
+  options, _args = parser.parse_args()
+  if not options.binary:
+    parser.error('You must specify the Firefox browser executable.')
+  if not options.webpage:
+    parser.error('You must specify the web page to browse to.')
+
+  firefox_profile = profile.FirefoxProfile(preferences=WEBRTC_PREFERENCES)
+  firefox_runner = runner.FirefoxRunner(profile=firefox_profile,
+                                        binary=options.binary,
+                                        cmdargs=[options.webpage])
+  def KillFirefox(signum, frame):
+    firefox_runner.stop()
+  signal.signal(signal.SIGTERM, KillFirefox)
+
+  firefox_runner.start()
+
+  # Run until Chrome kills us.
+  firefox_runner.process_handler.wait(timeout=600)
+  return 0
+
+if __name__ == '__main__':
+  sys.exit(main())