Debian Codegraph @

closure-library/closure/goog/storage/mechanism/mechanismfactory.js

raw
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 /**
16 * @fileoverview Provides factory methods for selecting the best storage
17 * mechanism, depending on availability and needs.
18 *
19 */
20
21 goog.provide('goog.storage.mechanism.mechanismfactory');
22
23 goog.require('goog.storage.mechanism.HTML5LocalStorage');
24 goog.require('goog.storage.mechanism.HTML5SessionStorage');
25 goog.require('goog.storage.mechanism.IEUserData');
26 goog.require('goog.storage.mechanism.PrefixedMechanism');
27
28
29 /**
30 * The key to shared userData storage.
31 * @type {string}
32 */
33 goog.storage.mechanism.mechanismfactory.USER_DATA_SHARED_KEY =
34 'UserDataSharedStore';
35
36
37 /**
38 * Returns the best local storage mechanism, or null if unavailable.
39 * Local storage means that the database is placed on user's computer.
40 * The key-value database is normally shared between all the code paths
41 * that request it, so using an optional namespace is recommended. This
42 * provides separation and makes key collisions unlikely.
43 *
44 * @param {string=} opt_namespace Restricts the visibility to given namespace.
45 * @return {goog.storage.mechanism.IterableMechanism} Created mechanism or null.
46 */
47 goog.storage.mechanism.mechanismfactory.create = function(opt_namespace) {
48 return goog.storage.mechanism.mechanismfactory.createHTML5LocalStorage(
49 opt_namespace) ||
50 goog.storage.mechanism.mechanismfactory.createIEUserData(opt_namespace);
51 };
52
53
54 /**
55 * Returns an HTML5 local storage mechanism, or null if unavailable.
56 * Since the HTML5 local storage does not support namespaces natively,
57 * and the key-value database is shared between all the code paths
58 * that request it, it is recommended that an optional namespace is
59 * used to provide key separation employing a prefix.
60 *
61 * @param {string=} opt_namespace Restricts the visibility to given namespace.
62 * @return {goog.storage.mechanism.IterableMechanism} Created mechanism or null.
63 */
64 goog.storage.mechanism.mechanismfactory.createHTML5LocalStorage = function(
65 opt_namespace) {
66 var storage = new goog.storage.mechanism.HTML5LocalStorage();
67 if (storage.isAvailable()) {
68 return opt_namespace ?
69 new goog.storage.mechanism.PrefixedMechanism(storage, opt_namespace) :
70 storage;
71 }
72 return null;
73 };
74
75
76 /**
77 * Returns an HTML5 session storage mechanism, or null if unavailable.
78 * Since the HTML5 session storage does not support namespaces natively,
79 * and the key-value database is shared between all the code paths
80 * that request it, it is recommended that an optional namespace is
81 * used to provide key separation employing a prefix.
82 *
83 * @param {string=} opt_namespace Restricts the visibility to given namespace.
84 * @return {goog.storage.mechanism.IterableMechanism} Created mechanism or null.
85 */
86 goog.storage.mechanism.mechanismfactory.createHTML5SessionStorage = function(
87 opt_namespace) {
88 var storage = new goog.storage.mechanism.HTML5SessionStorage();
89 if (storage.isAvailable()) {
90 return opt_namespace ?
91 new goog.storage.mechanism.PrefixedMechanism(storage, opt_namespace) :
92 storage;
93 }
94 return null;
95 };
96
97
98 /**
99 * Returns an IE userData local storage mechanism, or null if unavailable.
100 * Using an optional namespace is recommended to provide separation and
101 * avoid key collisions.
102 *
103 * @param {string=} opt_namespace Restricts the visibility to given namespace.
104 * @return {goog.storage.mechanism.IterableMechanism} Created mechanism or null.
105 */
106 goog.storage.mechanism.mechanismfactory.createIEUserData = function(
107 opt_namespace) {
108 var storage = new goog.storage.mechanism.IEUserData(
109 opt_namespace ||
110 goog.storage.mechanism.mechanismfactory.USER_DATA_SHARED_KEY);
111 if (storage.isAvailable()) {
112 return storage;
113 }
114 return null;
115 };