From cd40bdbec868768cea5bdc64c5695ba81b1cd86c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 27 Aug 2014 16:01:15 +0800 Subject: [PATCH] Add chrome-extension protocol for loading devtools extensions. --- atom.gyp | 1 + atom/browser/lib/chrome-extension.coffee | 41 ++++++++++++++++++++++++ atom/browser/lib/init.coffee | 3 ++ 3 files changed, 45 insertions(+) create mode 100644 atom/browser/lib/chrome-extension.coffee diff --git a/atom.gyp b/atom.gyp index df9cf202a516..21284c55e4e9 100644 --- a/atom.gyp +++ b/atom.gyp @@ -28,6 +28,7 @@ 'atom/browser/api/lib/protocol.coffee', 'atom/browser/api/lib/tray.coffee', 'atom/browser/api/lib/web-contents.coffee', + 'atom/browser/lib/chrome-extension.coffee', 'atom/browser/lib/init.coffee', 'atom/browser/lib/objects-registry.coffee', 'atom/browser/lib/rpc-server.coffee', diff --git a/atom/browser/lib/chrome-extension.coffee b/atom/browser/lib/chrome-extension.coffee new file mode 100644 index 000000000000..736bbdd00897 --- /dev/null +++ b/atom/browser/lib/chrome-extension.coffee @@ -0,0 +1,41 @@ +app = require 'app' +fs = require 'fs' +path = require 'path' +url = require 'url' + +# Mapping between hostname and file path. +hostPathMap = {} +hostPathMapNextKey = 0 + +getHostForPath = (path) -> + key = "#{++hostPathMapNextKey}" + hostPathMap[key] = path + key + +getPathForHost = (host) -> + hostPathMap[host] + +app.once 'ready', -> + protocol = require 'protocol' + BrowserWindow = require 'browser-window' + + protocol.registerProtocol 'chrome-extension', (request) -> + parsed = url.parse request.url + return unless parsed.hostname and parsed.path? + + directory = getPathForHost parsed.hostname + return new protocol.RequestFileJob(path.join(directory, parsed.path)) + + BrowserWindow::loadDevToolsExtension = (srcDirectory) -> + manifest = JSON.parse fs.readFileSync(path.join(srcDirectory, 'manifest.json')) + + # We can not use 'file://' directly because all resources in the extension + # will be treated as relative to the root in Chrome. + page = url.format + protocol: 'chrome-extension' + slashes: true + hostname: getHostForPath srcDirectory + pathname: manifest.devtools_page + + extensionInfo = startPage: page, name: manifest.name + @devToolsWebContents?.executeJavaScript "WebInspector.addExtensions([#{JSON.stringify(extensionInfo)}]);" diff --git a/atom/browser/lib/init.coffee b/atom/browser/lib/init.coffee index ff6416d0f9cd..3c5b059b3130 100644 --- a/atom/browser/lib/init.coffee +++ b/atom/browser/lib/init.coffee @@ -52,6 +52,9 @@ setImmediate -> detail: message buttons: ['OK'] + # Load the chrome extension support. + require './chrome-extension.js' + # Load the RPC server. require './rpc-server.js'