blob: ca28be1aa144da7a55a984a3fdf2f1cdc96d50ed [file] [log] [blame]
Ben Murdochbb1529c2013-08-08 10:24:53 +01001// Copyright 2013 The Chromium Authors. All rights reserved.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Murdochbb1529c2013-08-08 10:24:53 +01005#ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_
6#define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01007
8#include "base/files/file_path.h"
9#include "base/gtest_prod_util.h"
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010012#include "base/strings/nullable_string16.h"
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010013#include "base/strings/string16.h"
Ben Murdochbb1529c2013-08-08 10:24:53 +010014#include "content/common/content_export.h"
15#include "content/common/dom_storage/dom_storage_types.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010016#include "url/gurl.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010017
Ben Murdochbb1529c2013-08-08 10:24:53 +010018namespace content {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010019
Ben Murdochbb1529c2013-08-08 10:24:53 +010020class DOMStorageDatabaseAdapter;
21class DOMStorageMap;
22class DOMStorageTaskRunner;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010023class SessionStorageDatabase;
24
25// Container for a per-origin Map of key/value pairs potentially
26// backed by storage on disk and lazily commits changes to disk.
Ben Murdochbb1529c2013-08-08 10:24:53 +010027// See class comments for DOMStorageContextImpl for a larger overview.
28class CONTENT_EXPORT DOMStorageArea
29 : public base::RefCountedThreadSafe<DOMStorageArea> {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010030
31 public:
32 static const base::FilePath::CharType kDatabaseFileExtension[];
33 static base::FilePath DatabaseFileNameFromOrigin(const GURL& origin);
34 static GURL OriginFromDatabaseFileName(const base::FilePath& file_name);
35
36 // Local storage. Backed on disk if directory is nonempty.
Ben Murdochbb1529c2013-08-08 10:24:53 +010037 DOMStorageArea(const GURL& origin,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010038 const base::FilePath& directory,
Ben Murdochbb1529c2013-08-08 10:24:53 +010039 DOMStorageTaskRunner* task_runner);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010040
41 // Session storage. Backed on disk if |session_storage_backing| is not NULL.
Ben Murdochbb1529c2013-08-08 10:24:53 +010042 DOMStorageArea(int64 namespace_id,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010043 const std::string& persistent_namespace_id,
44 const GURL& origin,
45 SessionStorageDatabase* session_storage_backing,
Ben Murdochbb1529c2013-08-08 10:24:53 +010046 DOMStorageTaskRunner* task_runner);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010047
48 const GURL& origin() const { return origin_; }
49 int64 namespace_id() const { return namespace_id_; }
50
51 // Writes a copy of the current set of values in the area to the |map|.
Ben Murdochbb1529c2013-08-08 10:24:53 +010052 void ExtractValues(DOMStorageValuesMap* map);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010053
54 unsigned Length();
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010055 base::NullableString16 Key(unsigned index);
56 base::NullableString16 GetItem(const base::string16& key);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010057 bool SetItem(const base::string16& key, const base::string16& value,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010058 base::NullableString16* old_value);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010059 bool RemoveItem(const base::string16& key, base::string16* old_value);
60 bool Clear();
61 void FastClear();
62
Ben Murdochbb1529c2013-08-08 10:24:53 +010063 DOMStorageArea* ShallowCopy(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010064 int64 destination_namespace_id,
65 const std::string& destination_persistent_namespace_id);
66
67 bool HasUncommittedChanges() const;
68
69 // Similar to Clear() but more optimized for just deleting
70 // without raising events.
71 void DeleteOrigin();
72
73 // Frees up memory when possible. Typically, this method returns
74 // the object to its just constructed state, however if uncommitted
75 // changes are pending, it does nothing.
76 void PurgeMemory();
77
78 // Schedules the commit of any unsaved changes and enters a
79 // shutdown state such that the value getters and setters will
80 // no longer do anything.
81 void Shutdown();
82
83 // Returns true if the data is loaded in memory.
84 bool IsLoadedInMemory() const { return is_initial_import_done_; }
85
86 private:
Ben Murdochbb1529c2013-08-08 10:24:53 +010087 friend class DOMStorageAreaTest;
88 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, DOMStorageAreaBasics);
89 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, BackingDatabaseOpened);
90 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, TestDatabaseFilePath);
91 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, CommitTasks);
92 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, CommitChangesAtShutdown);
93 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, DeleteOrigin);
94 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, PurgeMemory);
95 FRIEND_TEST_ALL_PREFIXES(DOMStorageContextImplTest, PersistentIds);
96 friend class base::RefCountedThreadSafe<DOMStorageArea>;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010097
98 struct CommitBatch {
99 bool clear_all_first;
Ben Murdochbb1529c2013-08-08 10:24:53 +0100100 DOMStorageValuesMap changed_values;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100101 CommitBatch();
102 ~CommitBatch();
103 };
104
Ben Murdochbb1529c2013-08-08 10:24:53 +0100105 ~DOMStorageArea();
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100106
107 // If we haven't done so already and this is a local storage area,
108 // will attempt to read any values for this origin currently
109 // stored on disk.
110 void InitialImportIfNeeded();
111
112 // Post tasks to defer writing a batch of changed values to
113 // disk on the commit sequence, and to call back on the primary
114 // task sequence when complete.
115 CommitBatch* CreateCommitBatchIfNeeded();
116 void OnCommitTimer();
117 void CommitChanges(const CommitBatch* commit_batch);
118 void OnCommitComplete();
119
120 void ShutdownInCommitSequence();
121
122 int64 namespace_id_;
123 std::string persistent_namespace_id_;
124 GURL origin_;
125 base::FilePath directory_;
Ben Murdochbb1529c2013-08-08 10:24:53 +0100126 scoped_refptr<DOMStorageTaskRunner> task_runner_;
127 scoped_refptr<DOMStorageMap> map_;
128 scoped_ptr<DOMStorageDatabaseAdapter> backing_;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100129 scoped_refptr<SessionStorageDatabase> session_storage_backing_;
130 bool is_initial_import_done_;
131 bool is_shutdown_;
132 scoped_ptr<CommitBatch> commit_batch_;
133 int commit_batches_in_flight_;
134};
135
Ben Murdochbb1529c2013-08-08 10:24:53 +0100136} // namespace content
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100137
Ben Murdochbb1529c2013-08-08 10:24:53 +0100138#endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_