migration.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Copyright 2016 Mozilla Foundation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /* eslint strict: ["error", "function"] */
  14. (function () {
  15. "use strict";
  16. var storageLocal = chrome.storage.local;
  17. var storageSync = chrome.storage.sync;
  18. if (!storageSync) {
  19. // No sync storage area - nothing to migrate to.
  20. return;
  21. }
  22. getStorageNames(function (storageKeys) {
  23. storageLocal.get(storageKeys, function (values) {
  24. if (!values || !Object.keys(values).length) {
  25. // No local storage - nothing to migrate.
  26. // ... except possibly for a renamed preference name.
  27. migrateRenamedStorage();
  28. return;
  29. }
  30. migrateToSyncStorage(values);
  31. });
  32. });
  33. function getStorageNames(callback) {
  34. var x = new XMLHttpRequest();
  35. var schema_location = chrome.runtime.getManifest().storage.managed_schema;
  36. x.open("get", chrome.runtime.getURL(schema_location));
  37. x.onload = function () {
  38. var storageKeys = Object.keys(x.response.properties);
  39. callback(storageKeys);
  40. };
  41. x.responseType = "json";
  42. x.send();
  43. }
  44. // Save |values| to storage.sync and delete the values with that key from
  45. // storage.local.
  46. function migrateToSyncStorage(values) {
  47. storageSync.set(values, function () {
  48. if (chrome.runtime.lastError) {
  49. console.error(
  50. "Failed to migrate settings due to an error: " +
  51. chrome.runtime.lastError.message
  52. );
  53. return;
  54. }
  55. // Migration successful. Delete local settings.
  56. storageLocal.remove(Object.keys(values), function () {
  57. // In theory remove() could fail (e.g. if the browser's storage
  58. // backend is corrupt), but since storageSync.set succeeded, consider
  59. // the migration successful.
  60. console.log(
  61. "Successfully migrated preferences from local to sync storage."
  62. );
  63. migrateRenamedStorage();
  64. });
  65. });
  66. }
  67. // TODO: Remove this migration code somewhere in the future, when most users
  68. // have had their chance of migrating to the new preference format.
  69. // Note: We cannot modify managed preferences, so the migration logic is
  70. // duplicated in web/chromecom.js too.
  71. function migrateRenamedStorage() {
  72. storageSync.get(
  73. [
  74. "enableHandToolOnLoad",
  75. "cursorToolOnLoad",
  76. "disableTextLayer",
  77. "enhanceTextSelection",
  78. "textLayerMode",
  79. "showPreviousViewOnLoad",
  80. "disablePageMode",
  81. "viewOnLoad",
  82. ],
  83. function (items) {
  84. // Migration code for https://github.com/mozilla/pdf.js/pull/7635.
  85. if (typeof items.enableHandToolOnLoad === "boolean") {
  86. if (items.enableHandToolOnLoad) {
  87. storageSync.set(
  88. {
  89. cursorToolOnLoad: 1,
  90. },
  91. function () {
  92. if (!chrome.runtime.lastError) {
  93. storageSync.remove("enableHandToolOnLoad");
  94. }
  95. }
  96. );
  97. } else {
  98. storageSync.remove("enableHandToolOnLoad");
  99. }
  100. }
  101. // Migration code for https://github.com/mozilla/pdf.js/pull/9479.
  102. if (typeof items.disableTextLayer === "boolean") {
  103. if (items.disableTextLayer) {
  104. storageSync.set(
  105. {
  106. textLayerMode: 0,
  107. },
  108. function () {
  109. if (!chrome.runtime.lastError) {
  110. storageSync.remove([
  111. "disableTextLayer",
  112. "enhanceTextSelection",
  113. ]);
  114. }
  115. }
  116. );
  117. } else {
  118. storageSync.remove(["disableTextLayer", "enhanceTextSelection"]);
  119. }
  120. }
  121. // Migration code for https://github.com/mozilla/pdf.js/pull/10502.
  122. if (typeof items.showPreviousViewOnLoad === "boolean") {
  123. if (!items.showPreviousViewOnLoad) {
  124. storageSync.set(
  125. {
  126. viewOnLoad: 1,
  127. },
  128. function () {
  129. if (!chrome.runtime.lastError) {
  130. storageSync.remove([
  131. "showPreviousViewOnLoad",
  132. "disablePageMode",
  133. ]);
  134. }
  135. }
  136. );
  137. } else {
  138. storageSync.remove(["showPreviousViewOnLoad", "disablePageMode"]);
  139. }
  140. }
  141. }
  142. );
  143. }
  144. })();