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 // Commence aggressive flushing. This should be called early in the startup -
39 // before any localStorage writing. Currently scheduled writes will not be
40 // rescheduled and will be flushed at the scheduled time after which
41 // aggressive flushing will commence.
42 static void EnableAggressiveCommitDelay();
44 // Local storage. Backed on disk if directory is nonempty.
45 DOMStorageArea(const GURL
& origin
,
46 const base::FilePath
& directory
,
47 DOMStorageTaskRunner
* task_runner
);
49 // Session storage. Backed on disk if |session_storage_backing| is not NULL.
50 DOMStorageArea(int64 namespace_id
,
51 const std::string
& persistent_namespace_id
,
53 SessionStorageDatabase
* session_storage_backing
,
54 DOMStorageTaskRunner
* task_runner
);
56 const GURL
& origin() const { return origin_
; }
57 int64
namespace_id() const { return namespace_id_
; }
59 // Writes a copy of the current set of values in the area to the |map|.
60 void ExtractValues(DOMStorageValuesMap
* map
);
63 base::NullableString16
Key(unsigned index
);
64 base::NullableString16
GetItem(const base::string16
& key
);
65 bool SetItem(const base::string16
& key
, const base::string16
& value
,
66 base::NullableString16
* old_value
);
67 bool RemoveItem(const base::string16
& key
, base::string16
* old_value
);
71 DOMStorageArea
* ShallowCopy(
72 int64 destination_namespace_id
,
73 const std::string
& destination_persistent_namespace_id
);
75 bool HasUncommittedChanges() const;
76 void ScheduleImmediateCommit();
78 // Similar to Clear() but more optimized for just deleting
79 // without raising events.
82 // Frees up memory when possible. Typically, this method returns
83 // the object to its just constructed state, however if uncommitted
84 // changes are pending, it does nothing.
87 // Schedules the commit of any unsaved changes and enters a
88 // shutdown state such that the value getters and setters will
89 // no longer do anything.
92 // Returns true if the data is loaded in memory.
93 bool IsLoadedInMemory() const { return is_initial_import_done_
; }
96 friend class DOMStorageAreaTest
;
97 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, DOMStorageAreaBasics
);
98 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, BackingDatabaseOpened
);
99 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, TestDatabaseFilePath
);
100 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, CommitTasks
);
101 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, CommitChangesAtShutdown
);
102 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, DeleteOrigin
);
103 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, PurgeMemory
);
104 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest
, RateLimiter
);
105 FRIEND_TEST_ALL_PREFIXES(DOMStorageContextImplTest
, PersistentIds
);
106 friend class base::RefCountedThreadSafe
<DOMStorageArea
>;
108 // Used to rate limit commits.
109 class CONTENT_EXPORT RateLimiter
{
111 RateLimiter(size_t desired_rate
, base::TimeDelta time_quantum
);
113 void add_samples(size_t samples
) {
117 // Computes the total time needed to process the total samples seen
118 // at the desired rate.
119 base::TimeDelta
ComputeTimeNeeded() const;
121 // Given the elapsed time since the start of the rate limiting session,
122 // computes the delay needed to mimic having processed the total samples
123 // seen at the desired rate.
124 base::TimeDelta
ComputeDelayNeeded(
125 const base::TimeDelta elapsed_time
) const;
130 base::TimeDelta time_quantum_
;
134 bool clear_all_first
;
135 DOMStorageValuesMap changed_values
;
139 size_t GetDataSize() const;
144 // If we haven't done so already and this is a local storage area,
145 // will attempt to read any values for this origin currently
147 void InitialImportIfNeeded();
149 // Post tasks to defer writing a batch of changed values to
150 // disk on the commit sequence, and to call back on the primary
151 // task sequence when complete.
152 CommitBatch
* CreateCommitBatchIfNeeded();
153 void StartCommitTimer();
154 void OnCommitTimer();
155 void PostCommitTask();
156 void CommitChanges(const CommitBatch
* commit_batch
);
157 void OnCommitComplete();
158 base::TimeDelta
ComputeCommitDelay() const;
160 void ShutdownInCommitSequence();
162 static bool s_aggressive_flushing_enabled_
;
165 std::string persistent_namespace_id_
;
167 base::FilePath directory_
;
168 scoped_refptr
<DOMStorageTaskRunner
> task_runner_
;
169 scoped_refptr
<DOMStorageMap
> map_
;
170 scoped_ptr
<DOMStorageDatabaseAdapter
> backing_
;
171 scoped_refptr
<SessionStorageDatabase
> session_storage_backing_
;
172 bool is_initial_import_done_
;
174 scoped_ptr
<CommitBatch
> commit_batch_
;
175 int commit_batches_in_flight_
;
176 base::TimeTicks start_time_
;
177 RateLimiter data_rate_limiter_
;
178 RateLimiter commit_rate_limiter_
;
180 DISALLOW_COPY_AND_ASSIGN(DOMStorageArea
);
183 } // namespace content
185 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_