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