mechanismfactory.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Provides factory methods for selecting the best storage
  16. * mechanism, depending on availability and needs.
  17. *
  18. */
  19. goog.provide('goog.storage.mechanism.mechanismfactory');
  20. goog.require('goog.storage.mechanism.HTML5LocalStorage');
  21. goog.require('goog.storage.mechanism.HTML5SessionStorage');
  22. goog.require('goog.storage.mechanism.IEUserData');
  23. goog.require('goog.storage.mechanism.PrefixedMechanism');
  24. /**
  25. * The key to shared userData storage.
  26. * @type {string}
  27. */
  28. goog.storage.mechanism.mechanismfactory.USER_DATA_SHARED_KEY =
  29. 'UserDataSharedStore';
  30. /**
  31. * Returns the best local storage mechanism, or null if unavailable.
  32. * Local storage means that the database is placed on user's computer.
  33. * The key-value database is normally shared between all the code paths
  34. * that request it, so using an optional namespace is recommended. This
  35. * provides separation and makes key collisions unlikely.
  36. *
  37. * @param {string=} opt_namespace Restricts the visibility to given namespace.
  38. * @return {goog.storage.mechanism.IterableMechanism} Created mechanism or null.
  39. */
  40. goog.storage.mechanism.mechanismfactory.create = function(opt_namespace) {
  41. return goog.storage.mechanism.mechanismfactory.createHTML5LocalStorage(
  42. opt_namespace) ||
  43. goog.storage.mechanism.mechanismfactory.createIEUserData(opt_namespace);
  44. };
  45. /**
  46. * Returns an HTML5 local storage mechanism, or null if unavailable.
  47. * Since the HTML5 local storage does not support namespaces natively,
  48. * and the key-value database is shared between all the code paths
  49. * that request it, it is recommended that an optional namespace is
  50. * used to provide key separation employing a prefix.
  51. *
  52. * @param {string=} opt_namespace Restricts the visibility to given namespace.
  53. * @return {goog.storage.mechanism.IterableMechanism} Created mechanism or null.
  54. */
  55. goog.storage.mechanism.mechanismfactory.createHTML5LocalStorage = function(
  56. opt_namespace) {
  57. var storage = new goog.storage.mechanism.HTML5LocalStorage();
  58. if (storage.isAvailable()) {
  59. return opt_namespace ?
  60. new goog.storage.mechanism.PrefixedMechanism(storage, opt_namespace) :
  61. storage;
  62. }
  63. return null;
  64. };
  65. /**
  66. * Returns an HTML5 session storage mechanism, or null if unavailable.
  67. * Since the HTML5 session storage does not support namespaces natively,
  68. * and the key-value database is shared between all the code paths
  69. * that request it, it is recommended that an optional namespace is
  70. * used to provide key separation employing a prefix.
  71. *
  72. * @param {string=} opt_namespace Restricts the visibility to given namespace.
  73. * @return {goog.storage.mechanism.IterableMechanism} Created mechanism or null.
  74. */
  75. goog.storage.mechanism.mechanismfactory.createHTML5SessionStorage = function(
  76. opt_namespace) {
  77. var storage = new goog.storage.mechanism.HTML5SessionStorage();
  78. if (storage.isAvailable()) {
  79. return opt_namespace ?
  80. new goog.storage.mechanism.PrefixedMechanism(storage, opt_namespace) :
  81. storage;
  82. }
  83. return null;
  84. };
  85. /**
  86. * Returns an IE userData local storage mechanism, or null if unavailable.
  87. * Using an optional namespace is recommended to provide separation and
  88. * avoid key collisions.
  89. *
  90. * @param {string=} opt_namespace Restricts the visibility to given namespace.
  91. * @return {goog.storage.mechanism.IterableMechanism} Created mechanism or null.
  92. */
  93. goog.storage.mechanism.mechanismfactory.createIEUserData = function(
  94. opt_namespace) {
  95. var storage = new goog.storage.mechanism.IEUserData(
  96. opt_namespace ||
  97. goog.storage.mechanism.mechanismfactory.USER_DATA_SHARED_KEY);
  98. if (storage.isAvailable()) {
  99. return storage;
  100. }
  101. return null;
  102. };