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 WEBKIT_DOM_STORAGE_DOM_STORAGE_CONTEXT_H_
6 #define WEBKIT_DOM_STORAGE_DOM_STORAGE_CONTEXT_H_
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.h"
19 #include "googleurl/src/gurl.h"
20 #include "webkit/storage/webkit_storage_export.h"
22 class NullableString16
;
30 class SpecialStoragePolicy
;
33 namespace dom_storage
{
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 hierachy 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. Specifcally 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 DomStorageContext,
60 // DomStorageHost, and DomStorageSession. The other classes are for
61 // internal consumption.
62 class WEBKIT_STORAGE_EXPORT DomStorageContext
63 : public base::RefCountedThreadSafe
<DomStorageContext
> {
65 // An interface for observing Local and Session Storage events on the
69 virtual void OnDomStorageItemSet(
70 const DomStorageArea
* area
,
71 const base::string16
& key
,
72 const base::string16
& new_value
,
73 const NullableString16
& old_value
, // may be null on initial insert
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;
85 virtual ~EventObserver() {}
89 const base::FilePath
& localstorage_directory
, // empty for incognito profiles
90 const base::FilePath
& sessionstorage_directory
, // empty for incognito profiles
91 quota::SpecialStoragePolicy
* special_storage_policy
,
92 DomStorageTaskRunner
* task_runner
);
94 // Returns the directory path for localStorage, or an empty directory, if
95 // there is no backing on disk.
96 const base::FilePath
& localstorage_directory() { return localstorage_directory_
; }
98 // Returns the directory path for sessionStorage, or an empty directory, if
99 // there is no backing on disk.
100 const base::FilePath
& sessionstorage_directory() {
101 return sessionstorage_directory_
;
104 DomStorageTaskRunner
* task_runner() const { return task_runner_
; }
105 DomStorageNamespace
* GetStorageNamespace(int64 namespace_id
);
107 void GetLocalStorageUsage(std::vector
<LocalStorageUsageInfo
>* infos
,
108 bool include_file_info
);
109 void GetSessionStorageUsage(std::vector
<SessionStorageUsageInfo
>* infos
);
110 void DeleteLocalStorage(const GURL
& origin
);
111 void DeleteSessionStorage(const SessionStorageUsageInfo
& usage_info
);
114 // Used by content settings to alter the behavior around
115 // what data to keep and what data to discard at shutdown.
116 // The policy is not so straight forward to describe, see
117 // the implementation for details.
118 void SetForceKeepSessionState() {
119 force_keep_session_state_
= true;
122 // Called when the owning BrowserContext is ending.
123 // Schedules the commit of any unsaved changes and will delete
124 // and keep data on disk per the content settings and special storage
125 // policies. Contained areas and namespaces will stop functioning after
126 // this method has been called.
129 // Methods to add, remove, and notify EventObservers.
130 void AddEventObserver(EventObserver
* observer
);
131 void RemoveEventObserver(EventObserver
* observer
);
133 const DomStorageArea
* area
,
134 const base::string16
& key
,
135 const base::string16
& new_value
,
136 const NullableString16
& old_value
,
137 const GURL
& page_url
);
138 void NotifyItemRemoved(
139 const DomStorageArea
* area
,
140 const base::string16
& key
,
141 const base::string16
& old_value
,
142 const GURL
& page_url
);
143 void NotifyAreaCleared(
144 const DomStorageArea
* area
,
145 const GURL
& page_url
);
147 // May be called on any thread.
148 int64
AllocateSessionId() {
149 return session_id_sequence_
.GetNext();
152 std::string
AllocatePersistentSessionId();
154 // Must be called on the background thread.
155 void CreateSessionNamespace(int64 namespace_id
,
156 const std::string
& persistent_namespace_id
);
157 void DeleteSessionNamespace(int64 namespace_id
, bool should_persist_data
);
158 void CloneSessionNamespace(int64 existing_id
, int64 new_id
,
159 const std::string
& new_persistent_id
);
161 // Starts backing sessionStorage on disk. This function must be called right
162 // after DomStorageContext is created, before it's used.
163 void SetSaveSessionStorageOnDisk();
165 // Deletes all namespaces which don't have an associated DomStorageNamespace
166 // alive. This function is used for deleting possible leftover data after an
168 void StartScavengingUnusedSessionStorage();
171 friend class DomStorageContextTest
;
172 FRIEND_TEST_ALL_PREFIXES(DomStorageContextTest
, Basics
);
173 friend class base::RefCountedThreadSafe
<DomStorageContext
>;
174 typedef std::map
<int64
, scoped_refptr
<DomStorageNamespace
> >
177 ~DomStorageContext();
179 void ClearSessionOnlyOrigins();
181 // For scavenging unused sessionStorages.
182 void FindUnusedNamespaces();
183 void FindUnusedNamespacesInCommitSequence(
184 const std::set
<std::string
>& namespace_ids_in_use
,
185 const std::set
<std::string
>& protected_persistent_session_ids
);
186 void DeleteNextUnusedNamespace();
187 void DeleteNextUnusedNamespaceInCommitSequence();
189 // Collection of namespaces keyed by id.
190 StorageNamespaceMap namespaces_
;
192 // Where localstorage data is stored, maybe empty for the incognito use case.
193 base::FilePath localstorage_directory_
;
195 // Where sessionstorage data is stored, maybe empty for the incognito use
196 // case. Always empty until the file-backed session storage feature is
198 base::FilePath sessionstorage_directory_
;
200 // Used to schedule sequenced background tasks.
201 scoped_refptr
<DomStorageTaskRunner
> task_runner_
;
203 // List of objects observing local storage events.
204 ObserverList
<EventObserver
> event_observers_
;
206 // We use a 32 bit identifier for per tab storage sessions.
207 // At a tab per second, this range is large enough for 68 years.
208 base::AtomicSequenceNumber session_id_sequence_
;
211 bool force_keep_session_state_
;
212 scoped_refptr
<quota::SpecialStoragePolicy
> special_storage_policy_
;
213 scoped_refptr
<SessionStorageDatabase
> session_storage_database_
;
215 // For cleaning up unused namespaces gradually.
216 bool scavenging_started_
;
217 std::vector
<std::string
> deletable_persistent_namespace_ids_
;
219 // Persistent namespace IDs to protect from gradual deletion (they will
220 // be needed for session restore).
221 std::set
<std::string
> protected_persistent_session_ids_
;
223 // Mapping between persistent namespace IDs and namespace IDs for
225 std::map
<std::string
, int64
> persistent_namespace_id_to_namespace_id_
;
228 } // namespace dom_storage
230 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_CONTEXT_H_