Initial add of extension

Change-Id: I418bb0ea63c4e191975d5f738aac0f6975951e64
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..2e2e3d4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,13 @@
+Copyright (C) 2016 The Android Open Source Project
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c5a06e8
--- /dev/null
+++ b/README.md
@@ -0,0 +1,2 @@
+# gerrit-switch
+Chrome extension used to toggle PolyGerrit UI in Gerrit
diff --git a/background.js b/background.js
new file mode 100644
index 0000000..dc885ae
--- /dev/null
+++ b/background.js
@@ -0,0 +1,99 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+chrome.runtime.onInstalled.addListener(function() {
+  var loadImages = new Promise(function(resolve) {
+    var canvas = document.createElement('canvas');
+    var ctx = canvas.getContext('2d');
+    var inactiveImage = new Image();
+    var inactiveImageData;
+    var activeImage = new Image();
+    var activeImageData;
+    activeImage.onload = function() {
+      ctx.drawImage(activeImage, 0, 0, 38, 38);
+      var activeImageData = ctx.getImageData(0, 0, 38, 38);
+
+      inactiveImage.onload = function() {
+        ctx.drawImage(inactiveImage, 0, 0, 38, 38);
+        inactiveImageData = ctx.getImageData(0, 0, 38, 38);
+        resolve({active: activeImageData, inactive: inactiveImageData});
+      }
+      inactiveImage.src = chrome.runtime.getURL('icon.png');
+    }
+    activeImage.src = chrome.runtime.getURL('icon_active.png');
+  });
+
+  loadImages.then(function(imageData) {
+    chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
+      chrome.declarativeContent.onPageChanged.addRules([
+        {
+          conditions: [
+            new chrome.declarativeContent.PageStateMatcher({
+              pageUrl: { hostSuffix: '-review.googlesource.com' },
+            })
+          ],
+          // And shows the extension's page action.
+          actions: [ new chrome.declarativeContent.ShowPageAction() ]
+        },
+        {
+          conditions: [
+            new chrome.declarativeContent.PageStateMatcher({
+              css: ['gr-app'],
+            })
+          ],
+          actions: [ new chrome.declarativeContent.SetIcon({imageData: {38: imageData.active}}) ]
+        }
+      ]);
+    });
+  });
+
+  var tabIDToReload = null;
+  chrome.webRequest.onCompleted.addListener(
+    function(details) {
+      if (details.type == 'main_frame' && tabIDToReload != null) {
+        chrome.tabs.reload(tabIDToReload, {bypassCache: true});
+        tabIDToReload = null;
+      }
+    },
+    {urls: ['*://*.googlesource.com/*']});
+
+  chrome.pageAction.onClicked.addListener(function(tab) {
+    var cookieID = {
+      name: 'GERRIT_UI',
+      url: tab.url,
+    };
+    chrome.cookies.get(cookieID, function(cookie) {
+      if (cookie) {
+        chrome.cookies.remove(cookieID, function() {
+          chrome.tabs.update(tab.id, {
+            url: tab.url.replace('googlesource.com/', 'googlesource.com/#/')
+          }, function(tab) {
+            tabIDToReload = tab.id;
+          });
+        });
+      } else {
+        chrome.cookies.set({
+          url: tab.url,
+          name: 'GERRIT_UI',
+          value: 'polygerrit',
+          httpOnly: true ,
+          path: '/',
+          secure: true,
+        }, function() {
+          chrome.tabs.reload(tab.id, {bypassCache: true});
+        });
+      }
+    });
+  });
+});
diff --git a/icon.png b/icon.png
new file mode 100644
index 0000000..071066b
--- /dev/null
+++ b/icon.png
Binary files differ
diff --git a/icon_active.png b/icon_active.png
new file mode 100644
index 0000000..ca96211
--- /dev/null
+++ b/icon_active.png
Binary files differ
diff --git a/manifest.json b/manifest.json
new file mode 100644
index 0000000..b67b009
--- /dev/null
+++ b/manifest.json
@@ -0,0 +1,23 @@
+{
+  "manifest_version": 2,
+
+  "name": "Gerrit UI Switcher",
+  "description": "Easily switch between the current Gerrit UI and the new PolyGerrit UI",
+  "version": "0.2",
+  "background": {
+    "scripts": ["background.js"]
+  },
+  "page_action": {
+    "default_icon": {
+      "38": "icon.png"
+    },
+    "default_title": "Toggle PolyGerrit UI"
+  },
+  "permissions": [
+    "cookies",
+    "declarativeContent",
+    "tabs",
+    "webRequest",
+    "*://*.googlesource.com/*"
+  ]
+}