Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 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_IN_PROCESS_WEBKIT_DOM_STORAGE_CONTEXT_H_ | 5 #ifndef WEBKIT_DOM_STORAGE_DOM_STORAGE_CONTEXT_H_ |
| 6 #define CONTENT_BROWSER_IN_PROCESS_WEBKIT_DOM_STORAGE_CONTEXT_H_ | 6 #define WEBKIT_DOM_STORAGE_DOM_STORAGE_CONTEXT_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | |
| 11 | 10 |
| 11 #include "base/atomic_sequence_num.h" | |
| 12 #include "base/basictypes.h" | |
| 12 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 13 #include "base/gtest_prod_util.h" | |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/string16.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/time.h" | |
| 17 #include "content/common/content_export.h" | |
| 18 | 16 |
| 19 class DOMStorageArea; | 17 class FilePath; |
| 20 class DOMStorageMessageFilter; | 18 class GURL; |
| 21 class DOMStorageNamespace; | 19 class NullableString16; |
| 22 class WebKitContext; | 20 |
| 21 namespace base { | |
| 22 class Time; | |
| 23 } | |
| 23 | 24 |
| 24 namespace quota { | 25 namespace quota { |
| 25 class SpecialStoragePolicy; | 26 class SpecialStoragePolicy; |
| 26 } | 27 } |
| 27 | 28 |
| 28 // This is owned by WebKitContext and is all the dom storage information that's | 29 namespace dom_storage { |
| 29 // shared by all the DOMStorageMessageFilters that share the same browser | 30 |
| 30 // context. The specifics of responsibilities are fairly well documented here | 31 class DomStorageArea; |
| 31 // and in StorageNamespace and StorageArea. Everything is only to be accessed | 32 class DomStorageNamespace; |
| 32 // on the WebKit thread unless noted otherwise. | 33 class DomStorageSession; |
| 33 // | 34 class DomStorageTaskRunner; |
| 34 // NOTE: Virtual methods facilitate mocking functions for testing. | 35 |
| 35 class CONTENT_EXPORT DOMStorageContext { | 36 // One instance is allocated in the main process for each profile, |
| 37 // it should be called on the background thread determined by | |
| 38 // the task_runner. | |
| 39 class DomStorageContext | |
| 40 : public base::RefCountedThreadSafe<DomStorageContext> { | |
| 36 public: | 41 public: |
| 37 DOMStorageContext(WebKitContext* webkit_context, | 42 // An interface for observing LocalStorage events on the |
| 38 quota::SpecialStoragePolicy* special_storage_policy); | 43 // background thread. |
| 39 virtual ~DOMStorageContext(); | 44 class EventObserver { |
| 45 public: | |
| 46 virtual void OnDomStorageItemSet( | |
| 47 const DomStorageArea* area, | |
| 48 const string16& key, | |
| 49 const string16& new_value, | |
| 50 const NullableString16& old_value, // may be null on initial insert | |
| 51 const GURL& page_url) = 0; | |
| 52 virtual void OnDomStorageItemRemoved( | |
| 53 const DomStorageArea* area, | |
| 54 const string16& key, | |
| 55 const string16& old_value, | |
| 56 const GURL& page_url) = 0; | |
| 57 virtual void OnDomStorageAreaCleared( | |
| 58 const DomStorageArea* area, | |
| 59 const GURL& page_url) = 0; | |
| 60 virtual ~EventObserver() {} | |
| 61 }; | |
| 40 | 62 |
| 41 // Invalid storage id. No storage session will ever report this value. | 63 DomStorageContext(const FilePath& directory, // empty for incognito profiles |
| 42 // Used in DOMStorageMessageFilter::OnStorageAreaId when coping with | 64 quota::SpecialStoragePolicy* special_storage_policy, |
| 43 // interactions with non-existent storage sessions. | 65 DomStorageTaskRunner* task_runner); |
| 44 static const int64 kInvalidStorageId = -1; | 66 DomStorageTaskRunner* task_runner() const { return task_runner_; } |
| 67 DomStorageNamespace* GetStorageNamespace(int64 namespace_id); | |
| 45 | 68 |
| 46 // Allocate a new storage area id. Only call on the WebKit thread. | 69 // Methods to add, remove, and notify EventObservers. |
| 47 int64 AllocateStorageAreaId(); | 70 void AddEventObserver(EventObserver* observer); |
| 71 void RemoveEventObserver(EventObserver* observer); | |
| 72 void NotifyItemSet( | |
| 73 const DomStorageArea* area, | |
| 74 const string16& key, | |
| 75 const string16& new_value, | |
| 76 const NullableString16& old_value, | |
| 77 const GURL& page_url); | |
| 78 void NotifyItemRemoved( | |
| 79 const DomStorageArea* area, | |
| 80 const string16& key, | |
| 81 const string16& old_value, | |
| 82 const GURL& page_url); | |
| 83 void NotifyAreaCleared( | |
| 84 const DomStorageArea* area, | |
| 85 const GURL& page_url); | |
| 48 | 86 |
| 49 // Allocate a new session storage id. Only call on the UI or IO thread. | 87 #if 0 |
| 50 int64 AllocateSessionStorageNamespaceId(); | 88 // TODO(michaeln) |
| 51 | 89 // Methods needed to support chrome content settings and memory management |
| 52 // Clones a session storage namespace and returns the cloned namespaces' id. | 90 // features, we'll have to provide them, but just not yet. |
|
benm (inactive)
2012/02/02 16:23:19
Not sure what the Chrome style is on this but in m
michaeln
2012/02/03 00:33:42
nuked... we have svn history to see what interface
| |
| 53 // Only call on the IO thread. | |
| 54 int64 CloneSessionStorage(int64 original_id); | |
| 55 | |
| 56 // Various storage area methods. The storage area is owned by one of the | |
| 57 // namespaces that's owned by this class. | |
| 58 void RegisterStorageArea(DOMStorageArea* storage_area); | |
| 59 void UnregisterStorageArea(DOMStorageArea* storage_area); | |
| 60 DOMStorageArea* GetStorageArea(int64 id); | |
| 61 | |
| 62 // Called on WebKit thread when a session storage namespace can be deleted. | |
| 63 void DeleteSessionStorageNamespace(int64 namespace_id); | |
| 64 | |
| 65 // Get a namespace from an id. What's returned is owned by this class. If | |
| 66 // allocation_allowed is true, then this function will create the storage | |
| 67 // namespace if it hasn't been already. | |
| 68 DOMStorageNamespace* GetStorageNamespace(int64 id, bool allocation_allowed); | |
| 69 | |
| 70 // Sometimes an event from one DOM storage message filter requires | |
| 71 // communication to all of them. | |
| 72 typedef std::set<DOMStorageMessageFilter*> MessageFilterSet; | |
| 73 void RegisterMessageFilter(DOMStorageMessageFilter* message_filter); | |
| 74 void UnregisterMessageFilter(DOMStorageMessageFilter* MessageFilter); | |
| 75 const MessageFilterSet* GetMessageFilterSet() const; | |
| 76 | 91 |
| 77 // Tells storage namespaces to purge any memory they do not need. | 92 // Tells storage namespaces to purge any memory they do not need. |
| 78 virtual void PurgeMemory(); | 93 void PurgeMemory(); |
| 79 | 94 |
| 80 // Delete any local storage files that have been touched since the cutoff | 95 // Delete any local storage files that have been touched since the cutoff |
| 81 // date that's supplied. Protected origins, per the SpecialStoragePolicy, | 96 // date that's supplied. Protected origins, per the SpecialStoragePolicy, |
| 82 // are not deleted by this method. | 97 // are not deleted by this method. |
| 83 void DeleteDataModifiedSince(const base::Time& cutoff); | 98 void DeleteDataModifiedSince(const base::Time& cutoff); |
| 84 | 99 |
| 85 // Deletes a single local storage file. | 100 // Delete any local storage files which are allowed to be stored only until |
| 86 void DeleteLocalStorageFile(const FilePath& file_path); | 101 // the end of the session. Protected origins, per the SpecialStoragePolicy, |
| 102 // are not deleted by this method. | |
| 103 void DeleteSessionOnlyData(); | |
| 87 | 104 |
| 88 // Deletes the local storage file for the given origin. | 105 // Deletes the local storage file for the given origin. |
| 89 void DeleteLocalStorageForOrigin(const string16& origin_id); | 106 void DeleteLocalStorageForOrigin(const GURL& origin); |
| 90 | 107 |
| 91 // Deletes all local storage files. | 108 // Deletes all local storage files. |
| 92 void DeleteAllLocalStorageFiles(); | 109 void DeleteAllLocalStorageFiles(); |
| 93 | 110 |
| 94 // The local storage directory. | |
| 95 static const FilePath::CharType kLocalStorageDirectory[]; | |
| 96 | |
| 97 // The local storage file extension. | |
| 98 static const FilePath::CharType kLocalStorageExtension[]; | |
| 99 | |
| 100 // Get the file name of the local storage file for the given origin. | |
| 101 FilePath GetLocalStorageFilePath(const string16& origin_id) const; | |
| 102 | |
| 103 void set_clear_local_state_on_exit_(bool clear_local_state) { | 111 void set_clear_local_state_on_exit_(bool clear_local_state) { |
| 104 clear_local_state_on_exit_ = clear_local_state; | 112 clear_local_state_on_exit_ = clear_local_state; |
| 105 } | 113 } |
| 106 | 114 |
| 107 // Disables the exit-time deletion for all data (also session-only data). | 115 // Disables the exit-time deletion for all data (also session-only data). |
| 108 void SaveSessionState() { | 116 void SaveSessionState() { |
| 109 save_session_state_ = true; | 117 save_session_state_ = true; |
| 110 } | 118 } |
| 111 | 119 |
| 112 void set_data_path_for_testing(const FilePath& data_path) { | |
| 113 data_path_ = data_path; | |
| 114 } | |
| 115 | |
| 116 private: | 120 private: |
| 117 | 121 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; |
| 118 FRIEND_TEST_ALL_PREFIXES(DOMStorageTest, SessionOnly); | |
| 119 FRIEND_TEST_ALL_PREFIXES(DOMStorageTest, SaveSessionState); | |
| 120 | |
| 121 // Get the local storage instance. The object is owned by this class. | |
| 122 DOMStorageNamespace* CreateLocalStorage(); | |
| 123 | |
| 124 // Get a new session storage namespace. The object is owned by this class. | |
| 125 DOMStorageNamespace* CreateSessionStorage(int64 namespace_id); | |
| 126 | |
| 127 // Used internally to register storage namespaces we create. | |
| 128 void RegisterStorageNamespace(DOMStorageNamespace* storage_namespace); | |
| 129 | |
| 130 // The WebKit thread half of CloneSessionStorage above. Static because | |
| 131 // DOMStorageContext isn't ref counted thus we can't use a runnable method. | |
| 132 // That said, we know this is safe because this class is destroyed on the | |
| 133 // WebKit thread, so there's no way it could be destroyed before this is run. | |
| 134 static void CompleteCloningSessionStorage(DOMStorageContext* context, | |
| 135 int64 existing_id, int64 clone_id); | |
| 136 | |
| 137 // The last used storage_area_id and storage_namespace_id's. For the storage | |
| 138 // namespaces, IDs allocated on the UI thread are positive and count up while | |
| 139 // IDs allocated on the IO thread are negative and count down. This allows us | |
| 140 // to allocate unique IDs on both without any locking. All storage area ids | |
| 141 // are allocated on the WebKit thread. | |
| 142 int64 last_storage_area_id_; | |
| 143 int64 last_session_storage_namespace_id_on_ui_thread_; | |
| 144 int64 last_session_storage_namespace_id_on_io_thread_; | |
| 145 | 122 |
| 146 // True if the destructor should delete its files. | 123 // True if the destructor should delete its files. |
| 147 bool clear_local_state_on_exit_; | 124 bool clear_local_state_on_exit_; |
| 148 | 125 |
| 149 // If true, nothing (not even session-only data) should be deleted on exit. | 126 // If true, nothing (not even session-only data) should be deleted on exit. |
| 150 bool save_session_state_; | 127 bool save_session_state_; |
| 128 #endif // 0 (deferred for now) | |
| 151 | 129 |
| 152 // Path where the browser context data is stored. | 130 private: |
| 153 // TODO(pastarmovj): Keep in mind that unlike indexed db data_path_ variable | 131 friend class DomStorageSession; |
| 154 // this one still has to point to the upper level dir because of the | 132 friend class base::RefCountedThreadSafe<DomStorageContext>; |
| 155 // MigrateLocalStorageDirectory function. Once this function disappears we can | 133 typedef std::map<int64, scoped_refptr<DomStorageNamespace> > |
| 156 // make it point directly to the dom storage path. | 134 StorageNamespaceMap; |
| 157 FilePath data_path_; | |
| 158 | 135 |
| 159 // All the DOMStorageMessageFilters that are attached to us. ONLY USE ON THE | 136 ~DomStorageContext(); |
| 160 // IO THREAD! | |
| 161 MessageFilterSet message_filter_set_; | |
| 162 | 137 |
| 163 // Maps ids to StorageAreas. We do NOT own these objects. StorageNamespace | 138 // May be called on any thread. |
| 164 // (which does own them) will notify us when we should remove the entries. | 139 int64 AllocateSessionId() { |
| 165 typedef std::map<int64, DOMStorageArea*> StorageAreaMap; | 140 return session_id_sequence_.GetNext(); |
| 166 StorageAreaMap storage_area_map_; | 141 } |
| 167 | 142 |
| 168 // Maps ids to StorageNamespaces. We own these objects. | 143 // Must be called on the background thread. |
| 169 typedef std::map<int64, DOMStorageNamespace*> StorageNamespaceMap; | 144 void CreateSessionNamespace(int64 namespace_id); |
| 170 StorageNamespaceMap storage_namespace_map_; | 145 void DeleteSessionNamespace(int64 namespace_id); |
| 146 void CloneSessionNamespace(int64 existing_id, int64 new_id); | |
| 171 | 147 |
| 172 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; | 148 // Collection of namespaces keyed by id. |
| 149 StorageNamespaceMap namespaces_; | |
| 173 | 150 |
| 174 DISALLOW_IMPLICIT_CONSTRUCTORS(DOMStorageContext); | 151 // Where localstorage data is stored, maybe empty for the incognito use case. |
| 152 FilePath directory_; | |
| 153 | |
| 154 // Used to schedule sequenced background tasks. | |
| 155 scoped_refptr<DomStorageTaskRunner> task_runner_; | |
| 156 | |
| 157 // List of objects observing local storage events. | |
| 158 ObserverList<EventObserver> event_observers_; | |
| 159 | |
| 160 // We use a 32 bit identifier for per tab storage sessions. | |
| 161 // At a tab per second, this range is large enough for 68 years. | |
| 162 base::AtomicSequenceNumber session_id_sequence_; | |
| 175 }; | 163 }; |
| 176 | 164 |
| 177 #endif // CONTENT_BROWSER_IN_PROCESS_WEBKIT_DOM_STORAGE_CONTEXT_H_ | 165 } // namespace dom_storage |
| 166 | |
| 167 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_CONTEXT_H_ | |
| OLD | NEW |