| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef WEBKIT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_H_ | |
| 6 #define WEBKIT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_H_ | |
| 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" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/observer_list.h" | |
| 18 #include "base/time/time.h" | |
| 19 #include "url/gurl.h" | |
| 20 #include "webkit/browser/webkit_storage_browser_export.h" | |
| 21 | |
| 22 namespace base { | |
| 23 class FilePath; | |
| 24 class NullableString16; | |
| 25 class Time; | |
| 26 } | |
| 27 | |
| 28 namespace quota { | |
| 29 class SpecialStoragePolicy; | |
| 30 } | |
| 31 | |
| 32 namespace dom_storage { | |
| 33 | |
| 34 class DomStorageArea; | |
| 35 class DomStorageNamespace; | |
| 36 class DomStorageSession; | |
| 37 class DomStorageTaskRunner; | |
| 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 DomStorageContext, | |
| 59 // DomStorageHost, and DomStorageSession. The other classes are for | |
| 60 // internal consumption. | |
| 61 class WEBKIT_STORAGE_BROWSER_EXPORT DomStorageContext | |
| 62 : public base::RefCountedThreadSafe<DomStorageContext> { | |
| 63 public: | |
| 64 // An interface for observing Local and Session Storage events on the | |
| 65 // background thread. | |
| 66 class EventObserver { | |
| 67 public: | |
| 68 virtual void OnDomStorageItemSet( | |
| 69 const DomStorageArea* area, | |
| 70 const base::string16& key, | |
| 71 const base::string16& new_value, | |
| 72 const base::NullableString16& old_value, // may be null on initial inse
rt | |
| 73 const GURL& page_url) = 0; | |
| 74 virtual void OnDomStorageItemRemoved( | |
| 75 const DomStorageArea* area, | |
| 76 const base::string16& key, | |
| 77 const base::string16& old_value, | |
| 78 const GURL& page_url) = 0; | |
| 79 virtual void OnDomStorageAreaCleared( | |
| 80 const DomStorageArea* area, | |
| 81 const GURL& page_url) = 0; | |
| 82 | |
| 83 protected: | |
| 84 virtual ~EventObserver() {} | |
| 85 }; | |
| 86 | |
| 87 DomStorageContext( | |
| 88 const base::FilePath& localstorage_directory, // empty for incognito prof
iles | |
| 89 const base::FilePath& sessionstorage_directory, // empty for incognito pr
ofiles | |
| 90 quota::SpecialStoragePolicy* special_storage_policy, | |
| 91 DomStorageTaskRunner* task_runner); | |
| 92 | |
| 93 // Returns the directory path for localStorage, or an empty directory, if | |
| 94 // there is no backing on disk. | |
| 95 const base::FilePath& localstorage_directory() { | |
| 96 return localstorage_directory_; | |
| 97 } | |
| 98 | |
| 99 // Returns the directory path for sessionStorage, or an empty directory, if | |
| 100 // there is no backing on disk. | |
| 101 const base::FilePath& sessionstorage_directory() { | |
| 102 return sessionstorage_directory_; | |
| 103 } | |
| 104 | |
| 105 DomStorageTaskRunner* task_runner() const { return task_runner_.get(); } | |
| 106 DomStorageNamespace* GetStorageNamespace(int64 namespace_id); | |
| 107 | |
| 108 void GetLocalStorageUsage(std::vector<LocalStorageUsageInfo>* infos, | |
| 109 bool include_file_info); | |
| 110 void GetSessionStorageUsage(std::vector<SessionStorageUsageInfo>* infos); | |
| 111 void DeleteLocalStorage(const GURL& origin); | |
| 112 void DeleteSessionStorage(const SessionStorageUsageInfo& usage_info); | |
| 113 void PurgeMemory(); | |
| 114 | |
| 115 // Used by content settings to alter the behavior around | |
| 116 // what data to keep and what data to discard at shutdown. | |
| 117 // The policy is not so straight forward to describe, see | |
| 118 // the implementation for details. | |
| 119 void SetForceKeepSessionState() { | |
| 120 force_keep_session_state_ = true; | |
| 121 } | |
| 122 | |
| 123 // Called when the owning BrowserContext is ending. | |
| 124 // Schedules the commit of any unsaved changes and will delete | |
| 125 // and keep data on disk per the content settings and special storage | |
| 126 // policies. Contained areas and namespaces will stop functioning after | |
| 127 // this method has been called. | |
| 128 void Shutdown(); | |
| 129 | |
| 130 // Methods to add, remove, and notify EventObservers. | |
| 131 void AddEventObserver(EventObserver* observer); | |
| 132 void RemoveEventObserver(EventObserver* observer); | |
| 133 void NotifyItemSet( | |
| 134 const DomStorageArea* area, | |
| 135 const base::string16& key, | |
| 136 const base::string16& new_value, | |
| 137 const base::NullableString16& old_value, | |
| 138 const GURL& page_url); | |
| 139 void NotifyItemRemoved( | |
| 140 const DomStorageArea* area, | |
| 141 const base::string16& key, | |
| 142 const base::string16& old_value, | |
| 143 const GURL& page_url); | |
| 144 void NotifyAreaCleared( | |
| 145 const DomStorageArea* area, | |
| 146 const GURL& page_url); | |
| 147 | |
| 148 // May be called on any thread. | |
| 149 int64 AllocateSessionId() { | |
| 150 return session_id_sequence_.GetNext(); | |
| 151 } | |
| 152 | |
| 153 std::string AllocatePersistentSessionId(); | |
| 154 | |
| 155 // Must be called on the background thread. | |
| 156 void CreateSessionNamespace(int64 namespace_id, | |
| 157 const std::string& persistent_namespace_id); | |
| 158 void DeleteSessionNamespace(int64 namespace_id, bool should_persist_data); | |
| 159 void CloneSessionNamespace(int64 existing_id, int64 new_id, | |
| 160 const std::string& new_persistent_id); | |
| 161 | |
| 162 // Starts backing sessionStorage on disk. This function must be called right | |
| 163 // after DomStorageContext is created, before it's used. | |
| 164 void SetSaveSessionStorageOnDisk(); | |
| 165 | |
| 166 // Deletes all namespaces which don't have an associated DomStorageNamespace | |
| 167 // alive. This function is used for deleting possible leftover data after an | |
| 168 // unclean exit. | |
| 169 void StartScavengingUnusedSessionStorage(); | |
| 170 | |
| 171 private: | |
| 172 friend class DomStorageContextTest; | |
| 173 FRIEND_TEST_ALL_PREFIXES(DomStorageContextTest, Basics); | |
| 174 friend class base::RefCountedThreadSafe<DomStorageContext>; | |
| 175 typedef std::map<int64, scoped_refptr<DomStorageNamespace> > | |
| 176 StorageNamespaceMap; | |
| 177 | |
| 178 ~DomStorageContext(); | |
| 179 | |
| 180 void ClearSessionOnlyOrigins(); | |
| 181 | |
| 182 // For scavenging unused sessionStorages. | |
| 183 void FindUnusedNamespaces(); | |
| 184 void FindUnusedNamespacesInCommitSequence( | |
| 185 const std::set<std::string>& namespace_ids_in_use, | |
| 186 const std::set<std::string>& protected_persistent_session_ids); | |
| 187 void DeleteNextUnusedNamespace(); | |
| 188 void DeleteNextUnusedNamespaceInCommitSequence(); | |
| 189 | |
| 190 // Collection of namespaces keyed by id. | |
| 191 StorageNamespaceMap namespaces_; | |
| 192 | |
| 193 // Where localstorage data is stored, maybe empty for the incognito use case. | |
| 194 base::FilePath localstorage_directory_; | |
| 195 | |
| 196 // Where sessionstorage data is stored, maybe empty for the incognito use | |
| 197 // case. Always empty until the file-backed session storage feature is | |
| 198 // implemented. | |
| 199 base::FilePath sessionstorage_directory_; | |
| 200 | |
| 201 // Used to schedule sequenced background tasks. | |
| 202 scoped_refptr<DomStorageTaskRunner> task_runner_; | |
| 203 | |
| 204 // List of objects observing local storage events. | |
| 205 ObserverList<EventObserver> event_observers_; | |
| 206 | |
| 207 // We use a 32 bit identifier for per tab storage sessions. | |
| 208 // At a tab per second, this range is large enough for 68 years. | |
| 209 base::AtomicSequenceNumber session_id_sequence_; | |
| 210 | |
| 211 bool is_shutdown_; | |
| 212 bool force_keep_session_state_; | |
| 213 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; | |
| 214 scoped_refptr<SessionStorageDatabase> session_storage_database_; | |
| 215 | |
| 216 // For cleaning up unused namespaces gradually. | |
| 217 bool scavenging_started_; | |
| 218 std::vector<std::string> deletable_persistent_namespace_ids_; | |
| 219 | |
| 220 // Persistent namespace IDs to protect from gradual deletion (they will | |
| 221 // be needed for session restore). | |
| 222 std::set<std::string> protected_persistent_session_ids_; | |
| 223 | |
| 224 // Mapping between persistent namespace IDs and namespace IDs for | |
| 225 // sessionStorage. | |
| 226 std::map<std::string, int64> persistent_namespace_id_to_namespace_id_; | |
| 227 }; | |
| 228 | |
| 229 } // namespace dom_storage | |
| 230 | |
| 231 #endif // WEBKIT_BROWSER_DOM_STORAGE_DOM_STORAGE_CONTEXT_H_ | |
| OLD | NEW |