Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: content/browser/dom_storage/dom_storage_area.h

Issue 896643002: [DOMStorage] Rate limiting writes to disk. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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_DOM_STORAGE_AREA_H_ 5 #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_
6 #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_ 6 #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_
7 7
8 #include <string>
9
8 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
9 #include "base/gtest_prod_util.h" 11 #include "base/gtest_prod_util.h"
10 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/nullable_string16.h" 14 #include "base/strings/nullable_string16.h"
13 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
14 #include "content/common/content_export.h" 16 #include "content/common/content_export.h"
15 #include "content/common/dom_storage/dom_storage_types.h" 17 #include "content/common/dom_storage/dom_storage_types.h"
16 #include "url/gurl.h" 18 #include "url/gurl.h"
17 19
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 base::NullableString16* old_value); 60 base::NullableString16* old_value);
59 bool RemoveItem(const base::string16& key, base::string16* old_value); 61 bool RemoveItem(const base::string16& key, base::string16* old_value);
60 bool Clear(); 62 bool Clear();
61 void FastClear(); 63 void FastClear();
62 64
63 DOMStorageArea* ShallowCopy( 65 DOMStorageArea* ShallowCopy(
64 int64 destination_namespace_id, 66 int64 destination_namespace_id,
65 const std::string& destination_persistent_namespace_id); 67 const std::string& destination_persistent_namespace_id);
66 68
67 bool HasUncommittedChanges() const; 69 bool HasUncommittedChanges() const;
70 void ScheduleImmediateCommit();
68 71
69 // Similar to Clear() but more optimized for just deleting 72 // Similar to Clear() but more optimized for just deleting
70 // without raising events. 73 // without raising events.
71 void DeleteOrigin(); 74 void DeleteOrigin();
72 75
73 // Frees up memory when possible. Typically, this method returns 76 // Frees up memory when possible. Typically, this method returns
74 // the object to its just constructed state, however if uncommitted 77 // the object to its just constructed state, however if uncommitted
75 // changes are pending, it does nothing. 78 // changes are pending, it does nothing.
76 void PurgeMemory(); 79 void PurgeMemory();
77 80
78 // Schedules the commit of any unsaved changes and enters a 81 // Schedules the commit of any unsaved changes and enters a
79 // shutdown state such that the value getters and setters will 82 // shutdown state such that the value getters and setters will
80 // no longer do anything. 83 // no longer do anything.
81 void Shutdown(); 84 void Shutdown();
82 85
83 // Returns true if the data is loaded in memory. 86 // Returns true if the data is loaded in memory.
84 bool IsLoadedInMemory() const { return is_initial_import_done_; } 87 bool IsLoadedInMemory() const { return is_initial_import_done_; }
85 88
86 private: 89 private:
87 friend class DOMStorageAreaTest; 90 friend class DOMStorageAreaTest;
88 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, DOMStorageAreaBasics); 91 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, DOMStorageAreaBasics);
89 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, BackingDatabaseOpened); 92 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, BackingDatabaseOpened);
90 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, TestDatabaseFilePath); 93 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, TestDatabaseFilePath);
91 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, CommitTasks); 94 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, CommitTasks);
92 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, CommitChangesAtShutdown); 95 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, CommitChangesAtShutdown);
93 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, DeleteOrigin); 96 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, DeleteOrigin);
94 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, PurgeMemory); 97 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, PurgeMemory);
98 FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, RateLimiter);
95 FRIEND_TEST_ALL_PREFIXES(DOMStorageContextImplTest, PersistentIds); 99 FRIEND_TEST_ALL_PREFIXES(DOMStorageContextImplTest, PersistentIds);
96 friend class base::RefCountedThreadSafe<DOMStorageArea>; 100 friend class base::RefCountedThreadSafe<DOMStorageArea>;
97 101
102 // Used to rate limit commits.
103 class CONTENT_EXPORT RateLimiter {
104 public:
105 RateLimiter(size_t desired_rate, base::TimeDelta time_quantum);
106
107 void add_samples(size_t samples) {
108 samples_ += samples;
109 }
110
111 // Computes the total time needed to process the total samples seen
112 // at the desired rate.
113 base::TimeDelta ComputeTimeNeeded() const;
114
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;
120
121 private:
122 float rate_;
123 float samples_;
124 base::TimeDelta time_quantum_;
125 };
126
98 struct CommitBatch { 127 struct CommitBatch {
99 bool clear_all_first; 128 bool clear_all_first;
100 DOMStorageValuesMap changed_values; 129 DOMStorageValuesMap changed_values;
130
101 CommitBatch(); 131 CommitBatch();
102 ~CommitBatch(); 132 ~CommitBatch();
133 size_t GetDataSize() const;
103 }; 134 };
104 135
105 ~DOMStorageArea(); 136 ~DOMStorageArea();
106 137
107 // If we haven't done so already and this is a local storage area, 138 // 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 139 // will attempt to read any values for this origin currently
109 // stored on disk. 140 // stored on disk.
110 void InitialImportIfNeeded(); 141 void InitialImportIfNeeded();
111 142
112 // Post tasks to defer writing a batch of changed values to 143 // Post tasks to defer writing a batch of changed values to
113 // disk on the commit sequence, and to call back on the primary 144 // disk on the commit sequence, and to call back on the primary
114 // task sequence when complete. 145 // task sequence when complete.
115 CommitBatch* CreateCommitBatchIfNeeded(); 146 CommitBatch* CreateCommitBatchIfNeeded();
116 void OnCommitTimer(); 147 void OnCommitTimer();
148 void PostCommitTask();
117 void CommitChanges(const CommitBatch* commit_batch); 149 void CommitChanges(const CommitBatch* commit_batch);
118 void OnCommitComplete(); 150 void OnCommitComplete();
151 base::TimeDelta ComputeCommitDelay() const;
119 152
120 void ShutdownInCommitSequence(); 153 void ShutdownInCommitSequence();
121 154
122 int64 namespace_id_; 155 int64 namespace_id_;
123 std::string persistent_namespace_id_; 156 std::string persistent_namespace_id_;
124 GURL origin_; 157 GURL origin_;
125 base::FilePath directory_; 158 base::FilePath directory_;
126 scoped_refptr<DOMStorageTaskRunner> task_runner_; 159 scoped_refptr<DOMStorageTaskRunner> task_runner_;
127 scoped_refptr<DOMStorageMap> map_; 160 scoped_refptr<DOMStorageMap> map_;
128 scoped_ptr<DOMStorageDatabaseAdapter> backing_; 161 scoped_ptr<DOMStorageDatabaseAdapter> backing_;
129 scoped_refptr<SessionStorageDatabase> session_storage_backing_; 162 scoped_refptr<SessionStorageDatabase> session_storage_backing_;
130 bool is_initial_import_done_; 163 bool is_initial_import_done_;
131 bool is_shutdown_; 164 bool is_shutdown_;
132 scoped_ptr<CommitBatch> commit_batch_; 165 scoped_ptr<CommitBatch> commit_batch_;
133 int commit_batches_in_flight_; 166 int commit_batches_in_flight_;
167 base::TimeTicks start_time_;
168 RateLimiter data_rate_limiter_;
169 RateLimiter commit_rate_limiter_;
170
171 DISALLOW_COPY_AND_ASSIGN(DOMStorageArea);
134 }; 172 };
135 173
136 } // namespace content 174 } // namespace content
137 175
138 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_ 176 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_AREA_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698