OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_LOCAL_STORAGE_CONTEXT_MOJO_H_ | 5 #ifndef CONTENT_BROWSER_DOM_STORAGE_LOCAL_STORAGE_CONTEXT_MOJO_H_ |
6 #define CONTENT_BROWSER_DOM_STORAGE_LOCAL_STORAGE_CONTEXT_MOJO_H_ | 6 #define CONTENT_BROWSER_DOM_STORAGE_LOCAL_STORAGE_CONTEXT_MOJO_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
12 #include "content/common/leveldb_wrapper.mojom.h" | 12 #include "content/common/leveldb_wrapper.mojom.h" |
13 #include "content/public/browser/browser_thread.h" | |
13 #include "services/file/public/interfaces/file_system.mojom.h" | 14 #include "services/file/public/interfaces/file_system.mojom.h" |
14 #include "url/origin.h" | 15 #include "url/origin.h" |
15 | 16 |
16 namespace service_manager { | 17 namespace service_manager { |
17 class Connector; | 18 class Connector; |
18 } | 19 } |
19 | 20 |
21 namespace storage { | |
22 class SpecialStoragePolicy; | |
23 } | |
24 | |
20 namespace content { | 25 namespace content { |
21 | 26 |
22 class DOMStorageTaskRunner; | 27 class DOMStorageTaskRunner; |
23 class LevelDBWrapperImpl; | 28 class LevelDBWrapperImpl; |
24 struct LocalStorageUsageInfo; | 29 struct LocalStorageUsageInfo; |
25 | 30 |
26 // Used for mojo-based LocalStorage implementation (behind --mojo-local-storage | 31 // Used for mojo-based LocalStorage implementation (behind --mojo-local-storage |
27 // for now). | 32 // for now). |
michaeln
2017/05/03 21:01:29
Describe the thread access pattern in the class co
Marijn Kruisselbrink
2017/05/04 23:03:46
Done
| |
28 class CONTENT_EXPORT LocalStorageContextMojo { | 33 class CONTENT_EXPORT LocalStorageContextMojo |
34 : public base::RefCountedThreadSafe<LocalStorageContextMojo> { | |
michaeln
2017/05/03 21:01:29
I'd rather avoid refcounting if we can, but i see
Marijn Kruisselbrink
2017/05/04 23:03:46
Went that route. Seems reasonable, but somewhat un
| |
29 public: | 35 public: |
30 using GetStorageUsageCallback = | 36 using GetStorageUsageCallback = |
31 base::OnceCallback<void(std::vector<LocalStorageUsageInfo>)>; | 37 base::OnceCallback<void(std::vector<LocalStorageUsageInfo>)>; |
32 | 38 |
33 LocalStorageContextMojo(service_manager::Connector* connector, | 39 LocalStorageContextMojo( |
34 scoped_refptr<DOMStorageTaskRunner> task_runner, | 40 service_manager::Connector* connector, |
35 const base::FilePath& old_localstorage_path, | 41 scoped_refptr<DOMStorageTaskRunner> task_runner, |
36 const base::FilePath& subdirectory); | 42 const base::FilePath& old_localstorage_path, |
37 ~LocalStorageContextMojo(); | 43 const base::FilePath& subdirectory, |
44 storage::SpecialStoragePolicy* special_storage_policy); | |
38 | 45 |
39 void OpenLocalStorage(const url::Origin& origin, | 46 void OpenLocalStorage(const url::Origin& origin, |
40 mojom::LevelDBWrapperRequest request); | 47 mojom::LevelDBWrapperRequest request); |
41 void GetStorageUsage(GetStorageUsageCallback callback); | 48 void GetStorageUsage(GetStorageUsageCallback callback); |
42 void DeleteStorage(const url::Origin& origin); | 49 void DeleteStorage(const url::Origin& origin); |
43 // Like DeleteStorage(), but also deletes storage for all sub-origins. | 50 // Like DeleteStorage(), but also deletes storage for all sub-origins. |
44 void DeleteStorageForPhysicalOrigin(const url::Origin& origin); | 51 void DeleteStorageForPhysicalOrigin(const url::Origin& origin); |
45 void Flush(); | 52 void Flush(); |
46 | 53 |
54 // Used by content settings to alter the behavior around | |
55 // what data to keep and what data to discard at shutdown. | |
56 // The policy is not so straight forward to describe, see | |
57 // the implementation for details. | |
58 void SetForceKeepSessionState() { force_keep_session_state_ = true; } | |
59 | |
60 // Called when the owning BrowserContext is ending. | |
61 // Schedules the commit of any unsaved changes and will delete | |
62 // and keep data on disk per the content settings and special storage | |
63 // policies. | |
64 void Shutdown(); | |
65 | |
47 // Clears any caches, to free up as much memory as possible. Next access to | 66 // Clears any caches, to free up as much memory as possible. Next access to |
48 // storage for a particular origin will reload the data from the database. | 67 // storage for a particular origin will reload the data from the database. |
49 void PurgeMemory(); | 68 void PurgeMemory(); |
50 | 69 |
51 leveldb::mojom::LevelDBDatabaseAssociatedRequest DatabaseRequestForTesting(); | 70 leveldb::mojom::LevelDBDatabaseAssociatedRequest DatabaseRequestForTesting(); |
52 | 71 |
53 // Converts a string from the old storage format to the new storage format. | 72 // Converts a string from the old storage format to the new storage format. |
54 static std::vector<uint8_t> MigrateString(const base::string16& input); | 73 static std::vector<uint8_t> MigrateString(const base::string16& input); |
55 | 74 |
56 private: | 75 private: |
57 friend class MojoDOMStorageBrowserTest; | 76 friend class MojoDOMStorageBrowserTest; |
58 | 77 |
59 class LevelDBWrapperHolder; | 78 class LevelDBWrapperHolder; |
60 | 79 |
80 friend class base::RefCountedThreadSafe<LocalStorageContextMojo>; | |
81 ~LocalStorageContextMojo(); | |
82 | |
61 // Runs |callback| immediately if already connected to a database, otherwise | 83 // Runs |callback| immediately if already connected to a database, otherwise |
62 // delays running |callback| untill after a connection has been established. | 84 // delays running |callback| untill after a connection has been established. |
63 // Initiates connecting to the database if no connection is in progres yet. | 85 // Initiates connecting to the database if no connection is in progres yet. |
64 void RunWhenConnected(base::OnceClosure callback); | 86 void RunWhenConnected(base::OnceClosure callback); |
65 | 87 |
66 // Part of our asynchronous directory opening called from RunWhenConnected(). | 88 // Part of our asynchronous directory opening called from RunWhenConnected(). |
67 void InitiateConnection(bool in_memory_only = false); | 89 void InitiateConnection(bool in_memory_only = false); |
68 void OnDirectoryOpened(filesystem::mojom::FileError err); | 90 void OnDirectoryOpened(filesystem::mojom::FileError err); |
69 void OnDatabaseOpened(bool in_memory, leveldb::mojom::DatabaseError status); | 91 void OnDatabaseOpened(bool in_memory, leveldb::mojom::DatabaseError status); |
70 void OnGotDatabaseVersion(leveldb::mojom::DatabaseError status, | 92 void OnGotDatabaseVersion(leveldb::mojom::DatabaseError status, |
(...skipping 13 matching lines...) Expand all Loading... | |
84 // directly from that function, or through |on_database_open_callbacks_|. | 106 // directly from that function, or through |on_database_open_callbacks_|. |
85 void RetrieveStorageUsage(GetStorageUsageCallback callback); | 107 void RetrieveStorageUsage(GetStorageUsageCallback callback); |
86 void OnGotMetaData(GetStorageUsageCallback callback, | 108 void OnGotMetaData(GetStorageUsageCallback callback, |
87 leveldb::mojom::DatabaseError status, | 109 leveldb::mojom::DatabaseError status, |
88 std::vector<leveldb::mojom::KeyValuePtr> data); | 110 std::vector<leveldb::mojom::KeyValuePtr> data); |
89 | 111 |
90 void OnGotStorageUsageForDeletePhysicalOrigin( | 112 void OnGotStorageUsageForDeletePhysicalOrigin( |
91 const url::Origin& origin, | 113 const url::Origin& origin, |
92 std::vector<LocalStorageUsageInfo> usage); | 114 std::vector<LocalStorageUsageInfo> usage); |
93 | 115 |
94 service_manager::Connector* const connector_; | 116 void OnGotStorageUsageForShutdown(std::vector<LocalStorageUsageInfo> usage); |
117 void OnShutdownCommitComplete(leveldb::mojom::DatabaseError error); | |
118 | |
119 std::unique_ptr<service_manager::Connector> connector_; | |
95 const base::FilePath subdirectory_; | 120 const base::FilePath subdirectory_; |
96 | 121 |
97 enum ConnectionState { | 122 enum ConnectionState { |
98 NO_CONNECTION, | 123 NO_CONNECTION, |
99 CONNECTION_IN_PROGRESS, | 124 CONNECTION_IN_PROGRESS, |
100 CONNECTION_FINISHED | 125 CONNECTION_FINISHED, |
126 CONNECTION_SHUTDOWN | |
101 } connection_state_ = NO_CONNECTION; | 127 } connection_state_ = NO_CONNECTION; |
102 bool database_initialized_ = false; | 128 bool database_initialized_ = false; |
103 | 129 |
130 bool is_shutdown_ = false; | |
131 bool force_keep_session_state_ = false; | |
132 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_; | |
133 | |
104 file::mojom::FileSystemPtr file_system_; | 134 file::mojom::FileSystemPtr file_system_; |
105 filesystem::mojom::DirectoryPtr directory_; | 135 filesystem::mojom::DirectoryPtr directory_; |
106 | 136 |
107 leveldb::mojom::LevelDBServicePtr leveldb_service_; | 137 leveldb::mojom::LevelDBServicePtr leveldb_service_; |
108 leveldb::mojom::LevelDBDatabaseAssociatedPtr database_; | 138 leveldb::mojom::LevelDBDatabaseAssociatedPtr database_; |
109 bool tried_to_recreate_ = false; | 139 bool tried_to_recreate_ = false; |
110 | 140 |
111 std::vector<base::OnceClosure> on_database_opened_callbacks_; | 141 std::vector<base::OnceClosure> on_database_opened_callbacks_; |
112 | 142 |
113 // Maps between an origin and its prefixed LevelDB view. | 143 // Maps between an origin and its prefixed LevelDB view. |
114 std::map<url::Origin, std::unique_ptr<LevelDBWrapperHolder>> | 144 std::map<url::Origin, std::unique_ptr<LevelDBWrapperHolder>> |
115 level_db_wrappers_; | 145 level_db_wrappers_; |
116 | 146 |
117 // Used to access old data for migration. | 147 // Used to access old data for migration. |
118 scoped_refptr<DOMStorageTaskRunner> task_runner_; | 148 scoped_refptr<DOMStorageTaskRunner> task_runner_; |
119 base::FilePath old_localstorage_path_; | 149 base::FilePath old_localstorage_path_; |
120 | 150 |
121 base::WeakPtrFactory<LocalStorageContextMojo> weak_ptr_factory_; | 151 base::WeakPtrFactory<LocalStorageContextMojo> weak_ptr_factory_; |
122 }; | 152 }; |
123 | 153 |
124 } // namespace content | 154 } // namespace content |
125 | 155 |
126 #endif // CONTENT_BROWSER_DOM_STORAGE_LOCAL_STORAGE_CONTEXT_MOJO_H_ | 156 #endif // CONTENT_BROWSER_DOM_STORAGE_LOCAL_STORAGE_CONTEXT_MOJO_H_ |
OLD | NEW |