Add ICU message format support
[chromium-blink-merge.git] / content / browser / dom_storage / dom_storage_context_impl.h
blobc2b72bed5369c57aa6c3172b07547240c72577a9
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_
6 #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/atomic_sequence_num.h"
14 #include "base/basictypes.h"
15 #include "base/files/file_path.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/observer_list.h"
19 #include "base/time/time.h"
20 #include "content/common/content_export.h"
21 #include "url/gurl.h"
23 namespace base {
24 class FilePath;
25 class NullableString16;
26 class Time;
29 namespace storage {
30 class SpecialStoragePolicy;
33 namespace content {
35 class DOMStorageArea;
36 class DOMStorageNamespace;
37 class DOMStorageSession;
38 class DOMStorageTaskRunner;
39 class SessionStorageDatabase;
40 struct LocalStorageUsageInfo;
41 struct SessionStorageUsageInfo;
43 // The Context is the root of an object containment hierarchy for
44 // Namespaces and Areas related to the owning profile.
45 // One instance is allocated in the main process for each profile,
46 // instance methods should be called serially in the background as
47 // determined by the task_runner. Specifically not on chrome's non-blocking
48 // IO thread since these methods can result in blocking file io.
50 // In general terms, the DOMStorage object relationships are...
51 // Contexts (per-profile) own Namespaces which own Areas which share Maps.
52 // Hosts(per-renderer) refer to Namespaces and Areas open in its renderer.
53 // Sessions (per-tab) cause the creation and deletion of session Namespaces.
55 // Session Namespaces are cloned by initially making a shallow copy of
56 // all contained Areas, the shallow copies refer to the same refcounted Map,
57 // and does a deep copy-on-write if needed.
59 // Classes intended to be used by an embedder are DOMStorageContextImpl,
60 // DOMStorageHost, and DOMStorageSession. The other classes are for
61 // internal consumption.
62 class CONTENT_EXPORT DOMStorageContextImpl
63 : public base::RefCountedThreadSafe<DOMStorageContextImpl> {
64 public:
65 // An interface for observing Local and Session Storage events on the
66 // background thread.
67 class EventObserver {
68 public:
69 // |old_value| may be null on initial insert.
70 virtual void OnDOMStorageItemSet(
71 const DOMStorageArea* area,
72 const base::string16& key,
73 const base::string16& new_value,
74 const base::NullableString16& old_value,
75 const GURL& page_url) = 0;
76 virtual void OnDOMStorageItemRemoved(
77 const DOMStorageArea* area,
78 const base::string16& key,
79 const base::string16& old_value,
80 const GURL& page_url) = 0;
81 virtual void OnDOMStorageAreaCleared(
82 const DOMStorageArea* area,
83 const GURL& page_url) = 0;
85 protected:
86 virtual ~EventObserver() {}
89 // |localstorage_directory| and |sessionstorage_directory| may be empty
90 // for incognito browser contexts.
91 DOMStorageContextImpl(const base::FilePath& localstorage_directory,
92 const base::FilePath& sessionstorage_directory,
93 storage::SpecialStoragePolicy* special_storage_policy,
94 DOMStorageTaskRunner* task_runner);
96 // Returns the directory path for localStorage, or an empty directory, if
97 // there is no backing on disk.
98 const base::FilePath& localstorage_directory() {
99 return localstorage_directory_;
102 // Returns the directory path for sessionStorage, or an empty directory, if
103 // there is no backing on disk.
104 const base::FilePath& sessionstorage_directory() {
105 return sessionstorage_directory_;
108 DOMStorageTaskRunner* task_runner() const { return task_runner_.get(); }
109 DOMStorageNamespace* GetStorageNamespace(int64 namespace_id);
111 void GetLocalStorageUsage(std::vector<LocalStorageUsageInfo>* infos,
112 bool include_file_info);
113 void GetSessionStorageUsage(std::vector<SessionStorageUsageInfo>* infos);
114 void DeleteLocalStorage(const GURL& origin);
115 void DeleteSessionStorage(const SessionStorageUsageInfo& usage_info);
117 // Used by content settings to alter the behavior around
118 // what data to keep and what data to discard at shutdown.
119 // The policy is not so straight forward to describe, see
120 // the implementation for details.
121 void SetForceKeepSessionState() {
122 force_keep_session_state_ = true;
125 // Called when the owning BrowserContext is ending.
126 // Schedules the commit of any unsaved changes and will delete
127 // and keep data on disk per the content settings and special storage
128 // policies. Contained areas and namespaces will stop functioning after
129 // this method has been called.
130 void Shutdown();
132 // Initiate the process of flushing (writing - not sync'ing) any unwritten
133 // data managed by this instance. Flushing will start "soon".
134 void Flush();
136 // Methods to add, remove, and notify EventObservers.
137 void AddEventObserver(EventObserver* observer);
138 void RemoveEventObserver(EventObserver* observer);
139 void NotifyItemSet(
140 const DOMStorageArea* area,
141 const base::string16& key,
142 const base::string16& new_value,
143 const base::NullableString16& old_value,
144 const GURL& page_url);
145 void NotifyItemRemoved(
146 const DOMStorageArea* area,
147 const base::string16& key,
148 const base::string16& old_value,
149 const GURL& page_url);
150 void NotifyAreaCleared(
151 const DOMStorageArea* area,
152 const GURL& page_url);
154 // May be called on any thread.
155 int64 AllocateSessionId();
156 std::string AllocatePersistentSessionId();
158 // Must be called on the background thread.
159 void CreateSessionNamespace(int64 namespace_id,
160 const std::string& persistent_namespace_id);
161 void DeleteSessionNamespace(int64 namespace_id, bool should_persist_data);
162 void CloneSessionNamespace(int64 existing_id, int64 new_id,
163 const std::string& new_persistent_id);
165 // Starts backing sessionStorage on disk. This function must be called right
166 // after DOMStorageContextImpl is created, before it's used.
167 void SetSaveSessionStorageOnDisk();
169 // Deletes all namespaces which don't have an associated DOMStorageNamespace
170 // alive. This function is used for deleting possible leftover data after an
171 // unclean exit.
172 void StartScavengingUnusedSessionStorage();
174 private:
175 friend class DOMStorageContextImplTest;
176 FRIEND_TEST_ALL_PREFIXES(DOMStorageContextImplTest, Basics);
177 friend class base::RefCountedThreadSafe<DOMStorageContextImpl>;
178 typedef std::map<int64, scoped_refptr<DOMStorageNamespace> >
179 StorageNamespaceMap;
181 ~DOMStorageContextImpl();
183 void ClearSessionOnlyOrigins();
185 // For scavenging unused sessionStorages.
186 void FindUnusedNamespaces();
187 void FindUnusedNamespacesInCommitSequence(
188 const std::set<std::string>& namespace_ids_in_use,
189 const std::set<std::string>& protected_persistent_session_ids);
190 void DeleteNextUnusedNamespace();
191 void DeleteNextUnusedNamespaceInCommitSequence();
193 // Collection of namespaces keyed by id.
194 StorageNamespaceMap namespaces_;
196 // Where localstorage data is stored, maybe empty for the incognito use case.
197 base::FilePath localstorage_directory_;
199 // Where sessionstorage data is stored, maybe empty for the incognito use
200 // case. Always empty until the file-backed session storage feature is
201 // implemented.
202 base::FilePath sessionstorage_directory_;
204 // Used to schedule sequenced background tasks.
205 scoped_refptr<DOMStorageTaskRunner> task_runner_;
207 // List of objects observing local storage events.
208 base::ObserverList<EventObserver> event_observers_;
210 // We use a 32 bit identifier for per tab storage sessions.
211 // At a tab per second, this range is large enough for 68 years.
212 // The offset is to more quickly detect the error condition where
213 // an id related to one context is mistakenly used in another.
214 base::AtomicSequenceNumber session_id_sequence_;
215 const int session_id_offset_;
217 bool is_shutdown_;
218 bool force_keep_session_state_;
219 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_;
220 scoped_refptr<SessionStorageDatabase> session_storage_database_;
222 // For cleaning up unused namespaces gradually.
223 bool scavenging_started_;
224 std::vector<std::string> deletable_persistent_namespace_ids_;
226 // Persistent namespace IDs to protect from gradual deletion (they will
227 // be needed for session restore).
228 std::set<std::string> protected_persistent_session_ids_;
230 // Mapping between persistent namespace IDs and namespace IDs for
231 // sessionStorage.
232 std::map<std::string, int64> persistent_namespace_id_to_namespace_id_;
235 } // namespace content
237 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_