Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_ |
| 6 #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_ | 6 #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_ |
| 7 | 7 |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <vector> | |
| 11 | |
| 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" | |
| 8 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 9 #include "content/public/browser/dom_storage_context.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" | |
| 10 | 21 |
| 11 namespace base { | 22 namespace base { |
| 12 class FilePath; | 23 class FilePath; |
| 13 } | 24 class NullableString16; |
| 14 | 25 class Time; |
| 15 namespace dom_storage { | |
| 16 class DomStorageContext; | |
| 17 } | 26 } |
| 18 | 27 |
| 19 namespace quota { | 28 namespace quota { |
| 20 class SpecialStoragePolicy; | 29 class SpecialStoragePolicy; |
| 21 } | 30 } |
| 22 | 31 |
| 23 namespace content { | 32 namespace content { |
| 24 | 33 |
| 25 // This is owned by BrowserContext (aka Profile) and encapsulates all | 34 class DOMStorageArea; |
| 26 // per-profile dom storage state. | 35 class DOMStorageNamespace; |
| 27 class CONTENT_EXPORT DOMStorageContextImpl : | 36 class DOMStorageSession; |
| 28 NON_EXPORTED_BASE(public DOMStorageContext), | 37 class DOMStorageTaskRunner; |
| 29 public base::RefCountedThreadSafe<DOMStorageContextImpl> { | 38 class SessionStorageDatabase; |
| 39 struct LocalStorageUsageInfo; | |
| 40 struct SessionStorageUsageInfo; | |
| 41 | |
| 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. | |
| 48 // | |
| 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. | |
| 53 // | |
| 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. | |
| 57 // | |
| 58 // Classes intended to be used by an embedder are DOMStorageContextImpl, | |
| 59 // DOMStorageHost, and DOMStorageSession. The other classes are for | |
| 60 // internal consumption. | |
| 61 // | |
| 62 // TODO(kinuko): Merge this into DOMStorageContextProxy. | |
| 63 class CONTENT_EXPORT DOMStorageContextImpl | |
| 64 : public base::RefCountedThreadSafe<DOMStorageContextImpl> { | |
| 30 public: | 65 public: |
| 31 // If |data_path| is empty, nothing will be saved to disk. | 66 // An interface for observing Local and Session Storage events on the |
| 32 DOMStorageContextImpl(const base::FilePath& data_path, | 67 // background thread. |
| 33 quota::SpecialStoragePolicy* special_storage_policy); | 68 class EventObserver { |
| 34 | 69 public: |
| 35 // DOMStorageContext implementation. | 70 virtual void OnDOMStorageItemSet( |
| 36 virtual void GetLocalStorageUsage( | 71 const DOMStorageArea* area, |
| 37 const GetLocalStorageUsageCallback& callback) OVERRIDE; | 72 const base::string16& key, |
| 38 virtual void GetSessionStorageUsage( | 73 const base::string16& new_value, |
| 39 const GetSessionStorageUsageCallback& callback) OVERRIDE; | 74 const base::NullableString16& old_value, // may be null on initial inse rt |
|
jam
2013/08/06 16:13:49
nit: 80 chars, also below in lines 90 and 91
kinuko
2013/08/07 14:29:52
Done.
| |
| 40 virtual void DeleteLocalStorage(const GURL& origin) OVERRIDE; | 75 const GURL& page_url) = 0; |
| 41 virtual void DeleteSessionStorage( | 76 virtual void OnDOMStorageItemRemoved( |
| 42 const dom_storage::SessionStorageUsageInfo& usage_info) OVERRIDE; | 77 const DOMStorageArea* area, |
| 43 virtual void SetSaveSessionStorageOnDisk() OVERRIDE; | 78 const base::string16& key, |
| 44 virtual scoped_refptr<SessionStorageNamespace> | 79 const base::string16& old_value, |
| 45 RecreateSessionStorage(const std::string& persistent_id) OVERRIDE; | 80 const GURL& page_url) = 0; |
| 46 virtual void StartScavengingUnusedSessionStorage() OVERRIDE; | 81 virtual void OnDOMStorageAreaCleared( |
| 47 | 82 const DOMStorageArea* area, |
| 48 // Called to free up memory that's not strictly needed. | 83 const GURL& page_url) = 0; |
| 84 | |
| 85 protected: | |
| 86 virtual ~EventObserver() {} | |
| 87 }; | |
| 88 | |
| 89 DOMStorageContextImpl( | |
| 90 const base::FilePath& localstorage_directory, // empty for incognito prof iles | |
| 91 const base::FilePath& sessionstorage_directory, // empty for incognito pr ofiles | |
| 92 quota::SpecialStoragePolicy* special_storage_policy, | |
| 93 DOMStorageTaskRunner* task_runner); | |
| 94 | |
| 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_; | |
| 99 } | |
| 100 | |
| 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_; | |
| 105 } | |
| 106 | |
| 107 DOMStorageTaskRunner* task_runner() const { return task_runner_.get(); } | |
| 108 DOMStorageNamespace* GetStorageNamespace(int64 namespace_id); | |
| 109 | |
| 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); | |
| 49 void PurgeMemory(); | 115 void PurgeMemory(); |
| 50 | 116 |
| 51 // Used by content settings to alter the behavior around | 117 // Used by content settings to alter the behavior around |
| 52 // what data to keep and what data to discard at shutdown. | 118 // what data to keep and what data to discard at shutdown. |
| 53 // The policy is not so straight forward to describe, see | 119 // The policy is not so straight forward to describe, see |
| 54 // the implementation for details. | 120 // the implementation for details. |
| 55 void SetForceKeepSessionState(); | 121 void SetForceKeepSessionState() { |
| 56 | 122 force_keep_session_state_ = true; |
| 57 // Called when the BrowserContext/Profile is going away. | 123 } |
| 124 | |
| 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. | |
| 58 void Shutdown(); | 130 void Shutdown(); |
| 59 | 131 |
| 132 // Methods to add, remove, and notify EventObservers. | |
| 133 void AddEventObserver(EventObserver* observer); | |
| 134 void RemoveEventObserver(EventObserver* observer); | |
| 135 void NotifyItemSet( | |
| 136 const DOMStorageArea* area, | |
| 137 const base::string16& key, | |
| 138 const base::string16& new_value, | |
| 139 const base::NullableString16& old_value, | |
| 140 const GURL& page_url); | |
| 141 void NotifyItemRemoved( | |
| 142 const DOMStorageArea* area, | |
| 143 const base::string16& key, | |
| 144 const base::string16& old_value, | |
| 145 const GURL& page_url); | |
| 146 void NotifyAreaCleared( | |
| 147 const DOMStorageArea* area, | |
| 148 const GURL& page_url); | |
| 149 | |
| 150 // May be called on any thread. | |
| 151 int64 AllocateSessionId() { | |
| 152 return session_id_sequence_.GetNext(); | |
| 153 } | |
| 154 | |
| 155 std::string AllocatePersistentSessionId(); | |
| 156 | |
| 157 // Must be called on the background thread. | |
| 158 void CreateSessionNamespace(int64 namespace_id, | |
| 159 const std::string& persistent_namespace_id); | |
| 160 void DeleteSessionNamespace(int64 namespace_id, bool should_persist_data); | |
| 161 void CloneSessionNamespace(int64 existing_id, int64 new_id, | |
| 162 const std::string& new_persistent_id); | |
| 163 | |
| 164 // Starts backing sessionStorage on disk. This function must be called right | |
| 165 // after DOMStorageContextImpl is created, before it's used. | |
| 166 void SetSaveSessionStorageOnDisk(); | |
| 167 | |
| 168 // Deletes all namespaces which don't have an associated DOMStorageNamespace | |
| 169 // alive. This function is used for deleting possible leftover data after an | |
| 170 // unclean exit. | |
| 171 void StartScavengingUnusedSessionStorage(); | |
| 172 | |
| 60 private: | 173 private: |
| 61 friend class DOMStorageMessageFilter; // for access to context() | 174 friend class DOMStorageContextImplTest; |
| 62 friend class SessionStorageNamespaceImpl; // ditto | 175 FRIEND_TEST_ALL_PREFIXES(DOMStorageContextImplTest, Basics); |
| 63 friend class base::RefCountedThreadSafe<DOMStorageContextImpl>; | 176 friend class base::RefCountedThreadSafe<DOMStorageContextImpl>; |
| 64 | 177 typedef std::map<int64, scoped_refptr<DOMStorageNamespace> > |
| 65 virtual ~DOMStorageContextImpl(); | 178 StorageNamespaceMap; |
| 66 dom_storage::DomStorageContext* context() const { return context_.get(); } | 179 |
| 67 | 180 ~DOMStorageContextImpl(); |
| 68 scoped_refptr<dom_storage::DomStorageContext> context_; | 181 |
| 69 | 182 void ClearSessionOnlyOrigins(); |
| 70 DISALLOW_IMPLICIT_CONSTRUCTORS(DOMStorageContextImpl); | 183 |
| 184 // For scavenging unused sessionStorages. | |
| 185 void FindUnusedNamespaces(); | |
| 186 void FindUnusedNamespacesInCommitSequence( | |
| 187 const std::set<std::string>& namespace_ids_in_use, | |
| 188 const std::set<std::string>& protected_persistent_session_ids); | |
| 189 void DeleteNextUnusedNamespace(); | |
| 190 void DeleteNextUnusedNamespaceInCommitSequence(); | |
| 191 | |
| 192 // Collection of namespaces keyed by id. | |
| 193 StorageNamespaceMap namespaces_; | |
| 194 | |
| 195 // Where localstorage data is stored, maybe empty for the incognito use case. | |
| 196 base::FilePath localstorage_directory_; | |
| 197 | |
| 198 // Where sessionstorage data is stored, maybe empty for the incognito use | |
| 199 // case. Always empty until the file-backed session storage feature is | |
| 200 // implemented. | |
| 201 base::FilePath sessionstorage_directory_; | |
| 202 | |
| 203 // Used to schedule sequenced background tasks. | |
| 204 scoped_refptr<DOMStorageTaskRunner> task_runner_; | |
| 205 | |
| 206 // List of objects observing local storage events. | |
| 207 ObserverList<EventObserver> event_observers_; | |
| 208 | |
| 209 // We use a 32 bit identifier for per tab storage sessions. | |
| 210 // At a tab per second, this range is large enough for 68 years. | |
| 211 base::AtomicSequenceNumber session_id_sequence_; | |
| 212 | |
| 213 bool is_shutdown_; | |
| 214 bool force_keep_session_state_; | |
| 215 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; | |
| 216 scoped_refptr<SessionStorageDatabase> session_storage_database_; | |
| 217 | |
| 218 // For cleaning up unused namespaces gradually. | |
| 219 bool scavenging_started_; | |
| 220 std::vector<std::string> deletable_persistent_namespace_ids_; | |
| 221 | |
| 222 // Persistent namespace IDs to protect from gradual deletion (they will | |
| 223 // be needed for session restore). | |
| 224 std::set<std::string> protected_persistent_session_ids_; | |
| 225 | |
| 226 // Mapping between persistent namespace IDs and namespace IDs for | |
| 227 // sessionStorage. | |
| 228 std::map<std::string, int64> persistent_namespace_id_to_namespace_id_; | |
| 71 }; | 229 }; |
| 72 | 230 |
| 73 } // namespace content | 231 } // namespace content |
| 74 | 232 |
| 75 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_ | 233 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_IMPL_H_ |
| OLD | NEW |