1 // Copyright 2013 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.
5 #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_
6 #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_
10 #include "base/files/file_path.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/nullable_string16.h"
15 #include "base/strings/string16.h"
16 #include "content/common/content_export.h"
17 #include "content/common/dom_storage/dom_storage_types.h"
22 class DOMStorageDatabaseAdapter
;
24 class DOMStorageTaskRunner
;
25 class SessionStorageDatabase
;
27 // Container for a per-origin Map of key/value pairs potentially
28 // backed by storage on disk and lazily commits changes to disk.
29 // See class comments for DOMStorageContextImpl for a larger overview.
30 class CONTENT_EXPORT DOMStorageArea
31 : public base::RefCountedThreadSafe
<DOMStorageArea
> {
34 static const base::FilePath::CharType kDatabaseFileExtension
[];
35 static base::FilePath
DatabaseFileNameFromOrigin(const GURL
& origin
);
36 static GURL
OriginFromDatabaseFileName(const base::FilePath
& file_name
);
38 // Local storage. Backed on disk if directory is nonempty.
39 DOMStorageArea(const GURL
& origin
,
40 const base::FilePath
& directory
,
41 DOMStorageTaskRunner
* task_runner
);
43 // Session storage. Backed on disk if |session_storage_backing| is not NULL.
44 DOMStorageArea(int64 namespace_id
,
45 const std::string
& persistent_namespace_id
,
47 SessionStorageDatabase
* session_storage_backing
,
48 DOMStorageTaskRunner
* task_runner
);
50 const GURL
& origin() const { return origin_
; }
51 int64
namespace_id() const { return namespace_id_
; }
53 // Writes a copy of the current set of values in the area to the |map|.
54 void ExtractValues(DOMStorageValuesMap
* map
);
57 base::NullableString16
Key(unsigned index
);
58 base::NullableString16
GetItem(const base::string16
& key
);
59 bool SetItem(const base::string16
& key
, const base::string16
& value
,
60 base::NullableString16
* old_value
);
61 bool RemoveItem(const base::string16
& key
, base::string16
* old_value
);
65 DOMStorageArea
* ShallowCopy(
66 int64 destination_namespace_id
,
67 const std::string
& destination_persistent_namespace_id
);
69 bool HasUncommittedChanges() const;
70 void ScheduleImmediateCommit();
72 // Similar to Clear() but more optimized for just deleting
73 // without raising events.
76 // Frees up memory when possible. Typically, this method returns
77 // the object to its just constructed state, however if uncommitted
78 // changes are pending, it does nothing.
81 // Schedules the commit of any unsaved changes and enters a
82 // shutdown state such that the value getters and setters will
83 // no longer do anything.
86 // Returns true if the data is loaded in memory.
87 bool IsLoadedInMemory() const { return is_initial_import_done_
; }
90 friend class DOMStorageAreaTest
;
91 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, DOMStorageAreaBasics
);
92 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, BackingDatabaseOpened
);
93 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, TestDatabaseFilePath
);
94 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, CommitTasks
);
95 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, CommitChangesAtShutdown
);
96 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, DeleteOrigin
);
97 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, PurgeMemory
);
98 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, RateLimiter
);
99 FRIEND_TEST_ALL_PREFIXES(DOMStorageContextImplTest
, PersistentIds
);
100 friend class base::RefCountedThreadSafe
<DOMStorageArea
>;
102 // Used to rate limit commits.
103 class CONTENT_EXPORT RateLimiter
{
105 RateLimiter(size_t desired_rate
, base::TimeDelta time_quantum
);
107 void add_samples(size_t samples
) {
111 // Computes the total time needed to process the total samples seen
112 // at the desired rate.
113 base::TimeDelta
ComputeTimeNeeded() const;
115 // Given the elapsed time since the start of the rate limiting session,
116 // computes the delay needed to mimic having processed the total samples
117 // seen at the desired rate.
118 base::TimeDelta
ComputeDelayNeeded(
119 const base::TimeDelta elapsed_time
) const;
124 base::TimeDelta time_quantum_
;
128 bool clear_all_first
;
129 DOMStorageValuesMap changed_values
;
133 size_t GetDataSize() const;
138 // If we haven't done so already and this is a local storage area,
139 // will attempt to read any values for this origin currently
141 void InitialImportIfNeeded();
143 // Post tasks to defer writing a batch of changed values to
144 // disk on the commit sequence, and to call back on the primary
145 // task sequence when complete.
146 CommitBatch
* CreateCommitBatchIfNeeded();
147 void OnCommitTimer();
148 void PostCommitTask();
149 void CommitChanges(const CommitBatch
* commit_batch
);
150 void OnCommitComplete();
151 base::TimeDelta
ComputeCommitDelay() const;
153 void ShutdownInCommitSequence();
156 std::string persistent_namespace_id_
;
158 base::FilePath directory_
;
159 scoped_refptr
<DOMStorageTaskRunner
> task_runner_
;
160 scoped_refptr
<DOMStorageMap
> map_
;
161 scoped_ptr
<DOMStorageDatabaseAdapter
> backing_
;
162 scoped_refptr
<SessionStorageDatabase
> session_storage_backing_
;
163 bool is_initial_import_done_
;
165 scoped_ptr
<CommitBatch
> commit_batch_
;
166 int commit_batches_in_flight_
;
167 base::TimeTicks start_time_
;
168 RateLimiter data_rate_limiter_
;
169 RateLimiter commit_rate_limiter_
;
171 DISALLOW_COPY_AND_ASSIGN(DOMStorageArea
);
174 } // namespace content
176 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_