diff options
| -rw-r--r-- | background_scripts/settings.coffee | 76 | ||||
| -rw-r--r-- | background_scripts/sync.coffee | 74 | ||||
| -rw-r--r-- | manifest.json | 1 | ||||
| -rw-r--r-- | tests/unit_tests/exclusion_test.coffee | 1 | ||||
| -rw-r--r-- | tests/unit_tests/settings_test.coffee | 1 | ||||
| -rw-r--r-- | tests/unit_tests/utils_test.coffee | 1 | 
6 files changed, 74 insertions, 80 deletions
diff --git a/background_scripts/settings.coffee b/background_scripts/settings.coffee index d23649ee..5442a1cf 100644 --- a/background_scripts/settings.coffee +++ b/background_scripts/settings.coffee @@ -1,8 +1,81 @@  # -# Used by all parts of Vimium to manipulate localStorage. +# * Sync.set() and Sync.clear() propagate local changes to chrome.storage.sync. +# * Sync.handleStorageUpdate() listens for changes to chrome.storage.sync and propagates those +#   changes to localStorage and into vimium's internal state. +# * Sync.fetchAsync() polls chrome.storage.sync at startup, similarly propagating +#   changes to localStorage and into vimium's internal state. +# +# Changes are propagated into vimium's state using the same mechanism +# (Settings.performPostUpdateHook) that is used when options are changed on +# the options page. +# +# The effect is best-effort synchronization of vimium options/settings between +# chrome/vimium instances. +# +# NOTE: +#   Values handled within this module are ALWAYS already JSON.stringifed, so +#   they're always non-empty strings.  #  root = exports ? window +root.Sync = Sync = + +  storage: chrome.storage.sync +  doNotSync: ["settingsVersion", "previousVersion"] + +  # This is called in main.coffee. +  init: -> +    chrome.storage.onChanged.addListener (changes, area) -> Sync.handleStorageUpdate changes, area +    @fetchAsync() + +  # Asynchronous fetch from synced storage, called only at startup. +  fetchAsync: -> +    @storage.get null, (items) => +      unless chrome.runtime.lastError +        for own key, value of items +          @storeAndPropagate key, value + +  # Asynchronous message from synced storage. +  handleStorageUpdate: (changes, area) -> +    for own key, change of changes +      @storeAndPropagate key, change?.newValue + +  # Only ever called from asynchronous synced-storage callbacks (fetchAsync and handleStorageUpdate). +  storeAndPropagate: (key, value) -> +    return unless key of Settings.defaults +    return if not @shouldSyncKey key +    return if value and key of localStorage and localStorage[key] is value +    defaultValue = Settings.defaults[key] +    defaultValueJSON = JSON.stringify(defaultValue) + +    if value and value != defaultValueJSON +      # Key/value has been changed to non-default value at remote instance. +      localStorage[key] = value +      Settings.performPostUpdateHook key, JSON.parse(value) +    else +      # Key has been reset to default value at remote instance. +      if key of localStorage +        delete localStorage[key] +      Settings.performPostUpdateHook key, defaultValue + +  # Only called synchronously from within vimium, never on a callback. +  # No need to propagate updates to the rest of vimium, that's already been done. +  set: (key, value) -> +    if @shouldSyncKey key +      setting = {}; setting[key] = value +      @storage.set setting + +  # Only called synchronously from within vimium, never on a callback. +  clear: (key) -> +    @storage.remove key if @shouldSyncKey key + +  # Should we synchronize this key? +  shouldSyncKey: (key) -> key not in @doNotSync + +# +# Used by all parts of Vimium to manipulate localStorage. +# +  root.Settings = Settings =    get: (key) ->      if (key of localStorage) then JSON.parse(localStorage[key]) else @defaults[key] @@ -123,4 +196,3 @@ chrome.storage.local.get "findModeRawQueryList", (items) ->    unless chrome.runtime.lastError or items.findModeRawQueryList      rawQuery = Settings.get "findModeRawQuery"      chrome.storage.local.set findModeRawQueryList: (if rawQuery then [ rawQuery ] else []) - diff --git a/background_scripts/sync.coffee b/background_scripts/sync.coffee deleted file mode 100644 index d0d501d3..00000000 --- a/background_scripts/sync.coffee +++ /dev/null @@ -1,74 +0,0 @@ -# -# * Sync.set() and Sync.clear() propagate local changes to chrome.storage.sync. -# * Sync.handleStorageUpdate() listens for changes to chrome.storage.sync and propagates those -#   changes to localStorage and into vimium's internal state. -# * Sync.fetchAsync() polls chrome.storage.sync at startup, similarly propagating -#   changes to localStorage and into vimium's internal state. -# -# Changes are propagated into vimium's state using the same mechanism -# (Settings.performPostUpdateHook) that is used when options are changed on -# the options page. -# -# The effect is best-effort synchronization of vimium options/settings between -# chrome/vimium instances. -# -# NOTE: -#   Values handled within this module are ALWAYS already JSON.stringifed, so -#   they're always non-empty strings. -# - -root = exports ? window -root.Sync = Sync = - -  storage: chrome.storage.sync -  doNotSync: ["settingsVersion", "previousVersion"] - -  # This is called in main.coffee. -  init: -> -    chrome.storage.onChanged.addListener (changes, area) -> Sync.handleStorageUpdate changes, area -    @fetchAsync() - -  # Asynchronous fetch from synced storage, called only at startup. -  fetchAsync: -> -    @storage.get null, (items) => -      unless chrome.runtime.lastError -        for own key, value of items -          @storeAndPropagate key, value - -  # Asynchronous message from synced storage. -  handleStorageUpdate: (changes, area) -> -    for own key, change of changes -      @storeAndPropagate key, change?.newValue - -  # Only ever called from asynchronous synced-storage callbacks (fetchAsync and handleStorageUpdate). -  storeAndPropagate: (key, value) -> -    return unless key of Settings.defaults -    return if not @shouldSyncKey key -    return if value and key of localStorage and localStorage[key] is value -    defaultValue = Settings.defaults[key] -    defaultValueJSON = JSON.stringify(defaultValue) - -    if value and value != defaultValueJSON -      # Key/value has been changed to non-default value at remote instance. -      localStorage[key] = value -      Settings.performPostUpdateHook key, JSON.parse(value) -    else -      # Key has been reset to default value at remote instance. -      if key of localStorage -        delete localStorage[key] -      Settings.performPostUpdateHook key, defaultValue - -  # Only called synchronously from within vimium, never on a callback. -  # No need to propagate updates to the rest of vimium, that's already been done. -  set: (key, value) -> -    if @shouldSyncKey key -      setting = {}; setting[key] = value -      @storage.set setting - -  # Only called synchronously from within vimium, never on a callback. -  clear: (key) -> -    @storage.remove key if @shouldSyncKey key - -  # Should we synchronize this key? -  shouldSyncKey: (key) -> key not in @doNotSync - diff --git a/manifest.json b/manifest.json index fe5c69ca..5b65b6fd 100644 --- a/manifest.json +++ b/manifest.json @@ -11,7 +11,6 @@        "lib/utils.js",        "background_scripts/commands.js",        "lib/clipboard.js", -      "background_scripts/sync.js",        "background_scripts/settings.js",        "background_scripts/exclusions.js",        "background_scripts/completion_engines.js", diff --git a/tests/unit_tests/exclusion_test.coffee b/tests/unit_tests/exclusion_test.coffee index b3ed7194..217f2309 100644 --- a/tests/unit_tests/exclusion_test.coffee +++ b/tests/unit_tests/exclusion_test.coffee @@ -14,7 +14,6 @@ root.Marks =  extend(global, require "../../lib/utils.js")  Utils.getCurrentVersion = -> '1.44' -extend(global,require "../../background_scripts/sync.js")  extend(global,require "../../background_scripts/settings.js")  Sync.init()  extend(global, require "../../background_scripts/exclusions.js") diff --git a/tests/unit_tests/settings_test.coffee b/tests/unit_tests/settings_test.coffee index 4cd20211..e85c45f2 100644 --- a/tests/unit_tests/settings_test.coffee +++ b/tests/unit_tests/settings_test.coffee @@ -4,7 +4,6 @@ extend global, require "./test_chrome_stubs.js"  extend(global, require "../../lib/utils.js")  Utils.getCurrentVersion = -> '1.44'  global.localStorage = {} -extend(global,require "../../background_scripts/sync.js")  extend(global,require "../../background_scripts/settings.js")  Sync.init() diff --git a/tests/unit_tests/utils_test.coffee b/tests/unit_tests/utils_test.coffee index bfe066c3..e7febe13 100644 --- a/tests/unit_tests/utils_test.coffee +++ b/tests/unit_tests/utils_test.coffee @@ -2,7 +2,6 @@ require "./test_helper.js"  extend global, require "./test_chrome_stubs.js"  extend(global, require "../../lib/utils.js")  Utils.getCurrentVersion = -> '1.43' -extend(global, require "../../background_scripts/sync.js")  extend(global, require "../../background_scripts/settings.js")  Sync.init()  | 
