From dd07ffaa15efc6dff138cdaf89a146f229cc5b9d Mon Sep 17 00:00:00 2001
From: Stephen Blott
Date: Mon, 18 Apr 2016 11:50:39 +0100
Subject: Revert "Cache content_scripts/vimium.css in chrome.storage.local."
This reverts commit 0a49cc45732175c65651b5f054c677d6c42a28c0.
---
 background_scripts/main.coffee      | 14 +++++++-------
 content_scripts/ui_component.coffee | 29 ++++++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 10 deletions(-)
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee
index ab1c8610..4aa2fc41 100644
--- a/background_scripts/main.coffee
+++ b/background_scripts/main.coffee
@@ -129,15 +129,14 @@ helpDialogHtmlForCommand = (html, isAdvanced, bindings, description, showCommand
     html.push "
", Utils.escapeHtml(bindings)
   html.push("")
 
-# Cache "content_scripts/vimium.css" in chrome.storage.local for UI components.
-do ->
-  # Bail if we don't have these (specifically, we don't have them in the tests).
-  return unless XMLHttpRequest? and chrome.runtime.getURL? and chrome.storage.local.set?
+#
+# Fetches the contents of a file bundled with this extension.
+#
+fetchFileContents = (extensionFileName) ->
   req = new XMLHttpRequest()
-  req.open "GET", chrome.runtime.getURL("content_scripts/vimium.css"), true # true -> asynchronous.
-  req.onload = ({status, responseText}) ->
-    chrome.storage.local.set vimiumCSSInChromeStorage: responseText if status == 200
+  req.open("GET", chrome.runtime.getURL(extensionFileName), false) # false => synchronous
   req.send()
+  req.responseText
 
 TabOperations =
   # Opens the url in the current tab.
@@ -418,6 +417,7 @@ sendRequestHandlers =
   gotoMark: Marks.goto.bind(Marks)
   # Send a message to all frames in the current tab.
   sendMessageToFrames: (request, sender) -> chrome.tabs.sendMessage sender.tab.id, request.message
+  fetchFileContents: (request, sender) -> fetchFileContents request.fileName
   # For debugging only. This allows content scripts to log messages to the extension's logging page.
   log: ({frameId, message}, sender) -> BgUtils.log "#{frameId} #{message}", sender
 
diff --git a/content_scripts/ui_component.coffee b/content_scripts/ui_component.coffee
index 7422aada..1a7a1634 100644
--- a/content_scripts/ui_component.coffee
+++ b/content_scripts/ui_component.coffee
@@ -5,6 +5,7 @@ class UIComponent
   iframeFrameId: null
   options: null
   shadowDOM: null
+  styleSheetGetter: null
 
   toggleIframeElementClasses: (removeClass, addClass) ->
     @iframeElement.classList.remove removeClass
@@ -17,9 +18,10 @@ class UIComponent
       # Default to everything hidden while the stylesheet loads.
       styleSheet.innerHTML = "iframe {display: none;}"
 
-      # Fetch "content_scripts/vimium.css" from chrome.storage.local; the background page caches it there.
-      chrome.storage.local.get "vimiumCSSInChromeStorage", (items) ->
-        styleSheet.innerHTML = items.vimiumCSSInChromeStorage
+      # Use an XMLHttpRequest, possibly via the background page, to fetch the stylesheet. This allows us to
+      # catch and recover from failures that we could not have caught when using CSS @include (eg. #1817).
+      UIComponent::styleSheetGetter ?= new AsyncDataFetcher @fetchFileContents "content_scripts/vimium.css"
+      @styleSheetGetter.use (styles) -> styleSheet.innerHTML = styles
 
       @iframeElement = DomUtils.createElement "iframe"
       extend @iframeElement,
@@ -97,5 +99,26 @@ class UIComponent
         @options = null
         @postMessage "hidden" # Inform the UI component that it is hidden.
 
+  # Fetch a Vimium file/resource (such as "content_scripts/vimium.css").
+  # We try making an XMLHttpRequest request.  That can fail (see #1817), in which case we fetch the
+  # file/resource via the background page.
+  fetchFileContents: (file) -> (callback) ->
+    request = new XMLHttpRequest()
+
+    request.onload = ->
+      if request.status == 200
+        callback request.responseText
+      else
+        request.onerror()
+
+    request.onerror = ->
+      chrome.runtime.sendMessage
+        handler: "fetchFileContents"
+        fileName: file
+      , callback
+
+    request.open "GET", (chrome.runtime.getURL file), true
+    request.send()
+
 root = exports ? window
 root.UIComponent = UIComponent
-- 
cgit v1.2.3 |