Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Unified Diff: extension/background.js

Issue 11092092: Support compiling templates in the browser. Base URL: git@github.com:dart-lang/dart-web-components.git@master
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « extension/background.html ('k') | extension/build.sh » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extension/background.js
diff --git a/extension/background.js b/extension/background.js
new file mode 100644
index 0000000000000000000000000000000000000000..f6edcee7c9b9fc6d1d854fb1abf1bb20db1030c8
--- /dev/null
+++ b/extension/background.js
@@ -0,0 +1,68 @@
+// Copyright (c) 2012 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.
+
+// Glue code that interacts with dwc_compiler.dart to proxy arbitrary content
+// for urls.
+
+
+var existingRuleIds = {};
+var onParse = null;
+
+// TODO(jacobr): set the onParse property directly from Dart.
+function setOnParseCallback(cb) {
+ onParse = cb;
+}
+
+function proxyUrls(port, requests) {
+ // Unfortunately we have to create the data urls from the content script due
+ // to chrome security restrictions.
+ port.postMessage({type: 'CREATE_DATA_URLS', requests: requests});
+}
+
+function onProxyUrls(requests) {
+ var rulesToAdd = [];
+ var ruleIdsToRemove = [];
+ for (var i = 0; i < requests.length; i++) {
+ var request = requests[i];
+ var url = request.url;
+ var redirectUrl = request.redirectUrl;
+ var rule = {
+ conditions: [
+ new chrome.declarativeWebRequest.RequestMatcher(
+ {url: {urlEquals: url}})
+ ],
+ actions: [
+ new chrome.declarativeWebRequest.RedirectRequest(
+ {redirectUrl: redirectUrl})
+ ]};
+ if (existingRuleIds[url]) {
+ ruleIdsToRemove.push(existingRuleIds[url]);
+ }
+ rulesToAdd.push(rule);
+ }
+
+ if (ruleIdsToRemove.length > 0) {
+ chrome.declarativeWebRequest.onRequest.removeRules(ruleIdsToRemove);
+ }
+ chrome.declarativeWebRequest.onRequest.addRules(rulesToAdd, function(added) {
+ for(var i = 0; i < added.length; i++) {
+ var r = added[i];
+ existingRuleIds[r.conditions[0].url.urlEquals] = r.id;
+ }
+ // TODO(jacobr): notify caller that the proxy server is ready to use.
+ })
+}
+chrome.extension.onConnect.addListener(function(port) {
+ if (port.name == "parse") {
+ // Listen to requests from chrome tabs to parse templates.
+ port.onMessage.addListener(function(msg) {
+ onParse(port, msg.url);
+ });
+ } else if (port.name == "proxy") {
+ // Listen for requests to proxy urls.
+ port.onMessage.addListener(function(msg) {
+ onProxyUrls(msg.requests);
+ });
+ }
+});
« no previous file with comments | « extension/background.html ('k') | extension/build.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698