Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "content/browser/dom_storage/dom_storage_context_wrapper.h" | 5 #include "content/browser/dom_storage/dom_storage_context_wrapper.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/barrier_closure.h" | |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 14 #include "base/location.h" | 15 #include "base/location.h" |
| 15 #include "base/memory/memory_coordinator_client_registry.h" | 16 #include "base/memory/memory_coordinator_client_registry.h" |
| 16 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 17 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 18 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
| 19 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 32 #include "content/public/common/content_features.h" | 33 #include "content/public/common/content_features.h" |
| 33 #include "content/public/common/content_switches.h" | 34 #include "content/public/common/content_switches.h" |
| 34 | 35 |
| 35 namespace content { | 36 namespace content { |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| 38 const char kLocalStorageDirectory[] = "Local Storage"; | 39 const char kLocalStorageDirectory[] = "Local Storage"; |
| 39 const char kSessionStorageDirectory[] = "Session Storage"; | 40 const char kSessionStorageDirectory[] = "Session Storage"; |
| 40 | 41 |
| 41 void InvokeLocalStorageUsageCallbackHelper( | 42 void InvokeLocalStorageUsageCallbackHelper( |
| 42 const DOMStorageContext::GetLocalStorageUsageCallback& callback, | 43 const DOMStorageContext::GetLocalStorageUsageCallback& callback, |
| 43 const std::vector<LocalStorageUsageInfo>* infos) { | 44 std::unique_ptr<std::vector<LocalStorageUsageInfo>> infos) { |
| 44 callback.Run(*infos); | 45 callback.Run(*infos); |
| 45 } | 46 } |
| 46 | 47 |
| 47 void GetLocalStorageUsageHelper( | 48 void GetLocalStorageUsageHelper( |
| 48 std::vector<LocalStorageUsageInfo> mojo_usage, | |
| 49 base::SingleThreadTaskRunner* reply_task_runner, | 49 base::SingleThreadTaskRunner* reply_task_runner, |
| 50 DOMStorageContextImpl* context, | 50 DOMStorageContextImpl* context, |
| 51 const DOMStorageContext::GetLocalStorageUsageCallback& callback) { | 51 const DOMStorageContext::GetLocalStorageUsageCallback& callback) { |
| 52 std::vector<LocalStorageUsageInfo>* infos = | 52 auto infos = base::MakeUnique<std::vector<LocalStorageUsageInfo>>(); |
|
dcheng
2017/06/06 22:22:55
Out of curiosity: why wrap it in unique_ptr? vecto
Marijn Kruisselbrink
2017/06/06 22:52:35
Good question... The old code presumably wrapped i
dcheng
2017/06/06 23:02:44
Ah, I see. The code felt kind of like it wanted pr
| |
| 53 new std::vector<LocalStorageUsageInfo>(std::move(mojo_usage)); | 53 context->GetLocalStorageUsage(infos.get(), true); |
| 54 context->GetLocalStorageUsage(infos, true); | |
| 55 reply_task_runner->PostTask( | 54 reply_task_runner->PostTask( |
| 56 FROM_HERE, base::Bind(&InvokeLocalStorageUsageCallbackHelper, callback, | 55 FROM_HERE, base::BindOnce(&InvokeLocalStorageUsageCallbackHelper, |
| 57 base::Owned(infos))); | 56 callback, std::move(infos))); |
| 57 } | |
| 58 | |
| 59 void CollectLocalStorageUsage( | |
| 60 std::vector<LocalStorageUsageInfo>* out_info, | |
| 61 base::Closure done_callback, | |
| 62 const std::vector<LocalStorageUsageInfo>& in_info) { | |
| 63 out_info->insert(out_info->end(), in_info.begin(), in_info.end()); | |
| 64 done_callback.Run(); | |
| 65 } | |
| 66 | |
| 67 void GotMojoLocalStorageUsage( | |
| 68 scoped_refptr<base::SingleThreadTaskRunner> reply_task_runner, | |
| 69 const DOMStorageContext::GetLocalStorageUsageCallback& callback, | |
| 70 std::vector<LocalStorageUsageInfo> usage) { | |
| 71 reply_task_runner->PostTask(FROM_HERE, | |
| 72 base::BindOnce(callback, std::move(usage))); | |
| 58 } | 73 } |
| 59 | 74 |
| 60 void InvokeSessionStorageUsageCallbackHelper( | 75 void InvokeSessionStorageUsageCallbackHelper( |
| 61 const DOMStorageContext::GetSessionStorageUsageCallback& callback, | 76 const DOMStorageContext::GetSessionStorageUsageCallback& callback, |
| 62 const std::vector<SessionStorageUsageInfo>* infos) { | 77 const std::vector<SessionStorageUsageInfo>* infos) { |
| 63 callback.Run(*infos); | 78 callback.Run(*infos); |
| 64 } | 79 } |
| 65 | 80 |
| 66 void GetSessionStorageUsageHelper( | 81 void GetSessionStorageUsageHelper( |
| 67 base::SingleThreadTaskRunner* reply_task_runner, | 82 base::SingleThreadTaskRunner* reply_task_runner, |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 97 | 112 |
| 98 context_ = new DOMStorageContextImpl( | 113 context_ = new DOMStorageContextImpl( |
| 99 data_path.empty() ? data_path | 114 data_path.empty() ? data_path |
| 100 : data_path.AppendASCII(kLocalStorageDirectory), | 115 : data_path.AppendASCII(kLocalStorageDirectory), |
| 101 data_path.empty() ? data_path | 116 data_path.empty() ? data_path |
| 102 : data_path.AppendASCII(kSessionStorageDirectory), | 117 : data_path.AppendASCII(kSessionStorageDirectory), |
| 103 special_storage_policy, | 118 special_storage_policy, |
| 104 new DOMStorageWorkerPoolTaskRunner(std::move(primary_sequence), | 119 new DOMStorageWorkerPoolTaskRunner(std::move(primary_sequence), |
| 105 std::move(commit_sequence))); | 120 std::move(commit_sequence))); |
| 106 | 121 |
| 107 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 122 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 108 switches::kMojoLocalStorage)) { | 123 switches::kDisableMojoLocalStorage)) { |
| 109 base::FilePath storage_dir; | 124 base::FilePath storage_dir; |
| 110 if (!profile_path.empty()) | 125 if (!profile_path.empty()) |
| 111 storage_dir = local_partition_path.AppendASCII(kLocalStorageDirectory); | 126 storage_dir = local_partition_path.AppendASCII(kLocalStorageDirectory); |
| 112 // TODO(mek): Use a SequencedTaskRunner once mojo no longer requires a | 127 // TODO(mek): Use a SequencedTaskRunner once mojo no longer requires a |
| 113 // SingleThreadTaskRunner (http://crbug.com/678155). | 128 // SingleThreadTaskRunner (http://crbug.com/678155). |
| 114 mojo_task_runner_ = | 129 mojo_task_runner_ = |
| 115 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); | 130 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
| 116 mojo_state_ = new LocalStorageContextMojo( | 131 mojo_state_ = new LocalStorageContextMojo( |
| 117 connector, context_->task_runner(), | 132 connector, context_->task_runner(), |
| 118 data_path.empty() ? data_path | 133 data_path.empty() ? data_path |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 129 } | 144 } |
| 130 | 145 |
| 131 DOMStorageContextWrapper::~DOMStorageContextWrapper() { | 146 DOMStorageContextWrapper::~DOMStorageContextWrapper() { |
| 132 DCHECK(!mojo_state_) << "Shutdown should be called before destruction"; | 147 DCHECK(!mojo_state_) << "Shutdown should be called before destruction"; |
| 133 } | 148 } |
| 134 | 149 |
| 135 void DOMStorageContextWrapper::GetLocalStorageUsage( | 150 void DOMStorageContextWrapper::GetLocalStorageUsage( |
| 136 const GetLocalStorageUsageCallback& callback) { | 151 const GetLocalStorageUsageCallback& callback) { |
| 137 DCHECK(context_.get()); | 152 DCHECK(context_.get()); |
| 138 if (mojo_state_) { | 153 if (mojo_state_) { |
| 154 auto infos = base::MakeUnique<std::vector<LocalStorageUsageInfo>>(); | |
| 155 auto* infos_ptr = infos.get(); | |
| 156 base::RepeatingClosure got_local_storage_usage = base::BarrierClosure( | |
| 157 2, base::BindOnce(&InvokeLocalStorageUsageCallbackHelper, callback, | |
| 158 std::move(infos))); | |
| 159 | |
| 139 // base::Unretained is safe here, because the mojo_state_ won't be deleted | 160 // base::Unretained is safe here, because the mojo_state_ won't be deleted |
| 140 // until a ShutdownAndDelete task has been ran on the mojo_task_runner_, and | 161 // until a ShutdownAndDelete task has been ran on the mojo_task_runner_, and |
| 141 // as soon as that task is posted, mojo_state_ is set to null, preventing | 162 // as soon as that task is posted, mojo_state_ is set to null, preventing |
| 142 // further tasks from being queued. | 163 // further tasks from being queued. |
| 143 mojo_task_runner_->PostTask( | 164 mojo_task_runner_->PostTask( |
| 144 FROM_HERE, | 165 FROM_HERE, |
| 145 base::BindOnce( | 166 base::BindOnce( |
| 146 &LocalStorageContextMojo::GetStorageUsage, | 167 &LocalStorageContextMojo::GetStorageUsage, |
| 147 base::Unretained(mojo_state_), | 168 base::Unretained(mojo_state_), |
| 148 base::BindOnce( | 169 base::BindOnce(&GotMojoLocalStorageUsage, |
| 149 &DOMStorageContextWrapper::GotMojoLocalStorageUsage, this, | 170 base::ThreadTaskRunnerHandle::Get(), |
| 150 callback, | 171 base::Bind(CollectLocalStorageUsage, infos_ptr, |
| 151 base::RetainedRef(base::ThreadTaskRunnerHandle::Get())))); | 172 got_local_storage_usage)))); |
| 173 context_->task_runner()->PostShutdownBlockingTask( | |
| 174 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | |
| 175 base::Bind(&GetLocalStorageUsageHelper, | |
| 176 base::RetainedRef(base::ThreadTaskRunnerHandle::Get()), | |
| 177 base::RetainedRef(context_), | |
| 178 base::Bind(&CollectLocalStorageUsage, infos_ptr, | |
| 179 got_local_storage_usage))); | |
| 152 return; | 180 return; |
| 153 } | 181 } |
| 154 context_->task_runner()->PostShutdownBlockingTask( | 182 context_->task_runner()->PostShutdownBlockingTask( |
| 155 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 183 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
| 156 base::Bind(&GetLocalStorageUsageHelper, | 184 base::Bind(&GetLocalStorageUsageHelper, |
| 157 std::vector<LocalStorageUsageInfo>(), | |
| 158 base::RetainedRef(base::ThreadTaskRunnerHandle::Get()), | 185 base::RetainedRef(base::ThreadTaskRunnerHandle::Get()), |
| 159 base::RetainedRef(context_), callback)); | 186 base::RetainedRef(context_), callback)); |
| 160 } | 187 } |
| 161 | 188 |
| 162 void DOMStorageContextWrapper::GetSessionStorageUsage( | 189 void DOMStorageContextWrapper::GetSessionStorageUsage( |
| 163 const GetSessionStorageUsageCallback& callback) { | 190 const GetSessionStorageUsageCallback& callback) { |
| 164 DCHECK(context_.get()); | 191 DCHECK(context_.get()); |
| 165 context_->task_runner()->PostShutdownBlockingTask( | 192 context_->task_runner()->PostShutdownBlockingTask( |
| 166 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 193 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
| 167 base::Bind(&GetSessionStorageUsageHelper, | 194 base::Bind(&GetSessionStorageUsageHelper, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 264 mojo_state_ = nullptr; | 291 mojo_state_ = nullptr; |
| 265 } | 292 } |
| 266 memory_pressure_listener_.reset(); | 293 memory_pressure_listener_.reset(); |
| 267 context_->task_runner()->PostShutdownBlockingTask( | 294 context_->task_runner()->PostShutdownBlockingTask( |
| 268 FROM_HERE, | 295 FROM_HERE, |
| 269 DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 296 DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
| 270 base::Bind(&DOMStorageContextImpl::Shutdown, context_)); | 297 base::Bind(&DOMStorageContextImpl::Shutdown, context_)); |
| 271 if (base::FeatureList::IsEnabled(features::kMemoryCoordinator)) { | 298 if (base::FeatureList::IsEnabled(features::kMemoryCoordinator)) { |
| 272 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(this); | 299 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(this); |
| 273 } | 300 } |
| 274 | |
| 275 } | 301 } |
| 276 | 302 |
| 277 void DOMStorageContextWrapper::Flush() { | 303 void DOMStorageContextWrapper::Flush() { |
| 278 DCHECK(context_.get()); | 304 DCHECK(context_.get()); |
| 279 | 305 |
| 280 context_->task_runner()->PostShutdownBlockingTask( | 306 context_->task_runner()->PostShutdownBlockingTask( |
| 281 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 307 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
| 282 base::Bind(&DOMStorageContextImpl::Flush, context_)); | 308 base::Bind(&DOMStorageContextImpl::Flush, context_)); |
| 283 if (mojo_state_) { | 309 if (mojo_state_) { |
| 284 // base::Unretained is safe here, because the mojo_state_ won't be deleted | 310 // base::Unretained is safe here, because the mojo_state_ won't be deleted |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 299 // base::Unretained is safe here, because the mojo_state_ won't be deleted | 325 // base::Unretained is safe here, because the mojo_state_ won't be deleted |
| 300 // until a ShutdownAndDelete task has been ran on the mojo_task_runner_, and | 326 // until a ShutdownAndDelete task has been ran on the mojo_task_runner_, and |
| 301 // as soon as that task is posted, mojo_state_ is set to null, preventing | 327 // as soon as that task is posted, mojo_state_ is set to null, preventing |
| 302 // further tasks from being queued. | 328 // further tasks from being queued. |
| 303 mojo_task_runner_->PostTask( | 329 mojo_task_runner_->PostTask( |
| 304 FROM_HERE, base::BindOnce(&LocalStorageContextMojo::OpenLocalStorage, | 330 FROM_HERE, base::BindOnce(&LocalStorageContextMojo::OpenLocalStorage, |
| 305 base::Unretained(mojo_state_), origin, | 331 base::Unretained(mojo_state_), origin, |
| 306 std::move(request))); | 332 std::move(request))); |
| 307 } | 333 } |
| 308 | 334 |
| 335 void DOMStorageContextWrapper::SetLocalStorageDatabaseForTesting( | |
| 336 leveldb::mojom::LevelDBDatabaseAssociatedPtr database) { | |
| 337 if (!mojo_state_) | |
| 338 return; | |
| 339 // base::Unretained is safe here, because the mojo_state_ won't be deleted | |
| 340 // until a ShutdownAndDelete task has been ran on the mojo_task_runner_, and | |
| 341 // as soon as that task is posted, mojo_state_ is set to null, preventing | |
| 342 // further tasks from being queued. | |
| 343 mojo_task_runner_->PostTask( | |
| 344 FROM_HERE, | |
| 345 base::BindOnce(&LocalStorageContextMojo::SetDatabaseForTesting, | |
| 346 base::Unretained(mojo_state_), std::move(database))); | |
| 347 } | |
| 348 | |
| 309 void DOMStorageContextWrapper::OnMemoryPressure( | 349 void DOMStorageContextWrapper::OnMemoryPressure( |
| 310 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | 350 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 311 DOMStorageContextImpl::PurgeOption purge_option = | 351 DOMStorageContextImpl::PurgeOption purge_option = |
| 312 DOMStorageContextImpl::PURGE_UNOPENED; | 352 DOMStorageContextImpl::PURGE_UNOPENED; |
| 313 if (memory_pressure_level == | 353 if (memory_pressure_level == |
| 314 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { | 354 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
| 315 purge_option = DOMStorageContextImpl::PURGE_AGGRESSIVE; | 355 purge_option = DOMStorageContextImpl::PURGE_AGGRESSIVE; |
| 316 } | 356 } |
| 317 PurgeMemory(purge_option); | 357 PurgeMemory(purge_option); |
| 318 } | 358 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 330 // base::Unretained is safe here, because the mojo_state_ won't be deleted | 370 // base::Unretained is safe here, because the mojo_state_ won't be deleted |
| 331 // until a ShutdownAndDelete task has been ran on the mojo_task_runner_, and | 371 // until a ShutdownAndDelete task has been ran on the mojo_task_runner_, and |
| 332 // as soon as that task is posted, mojo_state_ is set to null, preventing | 372 // as soon as that task is posted, mojo_state_ is set to null, preventing |
| 333 // further tasks from being queued. | 373 // further tasks from being queued. |
| 334 mojo_task_runner_->PostTask( | 374 mojo_task_runner_->PostTask( |
| 335 FROM_HERE, base::BindOnce(&LocalStorageContextMojo::PurgeMemory, | 375 FROM_HERE, base::BindOnce(&LocalStorageContextMojo::PurgeMemory, |
| 336 base::Unretained(mojo_state_))); | 376 base::Unretained(mojo_state_))); |
| 337 } | 377 } |
| 338 } | 378 } |
| 339 | 379 |
| 340 void DOMStorageContextWrapper::GotMojoLocalStorageUsage( | |
| 341 GetLocalStorageUsageCallback callback, | |
| 342 base::SingleThreadTaskRunner* reply_task_runner, | |
| 343 std::vector<LocalStorageUsageInfo> usage) { | |
| 344 context_->task_runner()->PostShutdownBlockingTask( | |
| 345 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | |
| 346 base::Bind(&GetLocalStorageUsageHelper, base::Passed(&usage), | |
| 347 base::RetainedRef(reply_task_runner), | |
| 348 base::RetainedRef(context_), callback)); | |
| 349 } | |
| 350 | |
| 351 } // namespace content | 380 } // namespace content |
| OLD | NEW |