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/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/command_line.h" | |
| 12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 13 #include "base/location.h" | 14 #include "base/location.h" |
| 14 #include "base/memory/memory_coordinator_client_registry.h" | 15 #include "base/memory/memory_coordinator_client_registry.h" |
| 15 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 17 #include "base/sequenced_task_runner.h" | 18 #include "base/sequenced_task_runner.h" |
| 18 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/task_scheduler/post_task.h" | 21 #include "base/task_scheduler/post_task.h" |
| 21 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
| 22 #include "content/browser/dom_storage/dom_storage_area.h" | 23 #include "content/browser/dom_storage/dom_storage_area.h" |
| 23 #include "content/browser/dom_storage/dom_storage_context_impl.h" | 24 #include "content/browser/dom_storage/dom_storage_context_impl.h" |
| 24 #include "content/browser/dom_storage/dom_storage_task_runner.h" | 25 #include "content/browser/dom_storage/dom_storage_task_runner.h" |
| 25 #include "content/browser/dom_storage/local_storage_context_mojo.h" | 26 #include "content/browser/dom_storage/local_storage_context_mojo.h" |
| 26 #include "content/browser/dom_storage/session_storage_namespace_impl.h" | 27 #include "content/browser/dom_storage/session_storage_namespace_impl.h" |
| 27 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/content_browser_client.h" | 29 #include "content/public/browser/content_browser_client.h" |
| 29 #include "content/public/browser/local_storage_usage_info.h" | 30 #include "content/public/browser/local_storage_usage_info.h" |
| 30 #include "content/public/browser/session_storage_usage_info.h" | 31 #include "content/public/browser/session_storage_usage_info.h" |
| 31 #include "content/public/common/content_client.h" | 32 #include "content/public/common/content_client.h" |
| 32 #include "content/public/common/content_features.h" | 33 #include "content/public/common/content_features.h" |
| 34 #include "content/public/common/content_switches.h" | |
| 33 | 35 |
| 34 namespace content { | 36 namespace content { |
| 35 namespace { | 37 namespace { |
| 36 | 38 |
| 37 const char kLocalStorageDirectory[] = "Local Storage"; | 39 const char kLocalStorageDirectory[] = "Local Storage"; |
| 38 const char kSessionStorageDirectory[] = "Session Storage"; | 40 const char kSessionStorageDirectory[] = "Session Storage"; |
| 39 | 41 |
| 40 void InvokeLocalStorageUsageCallbackHelper( | 42 void InvokeLocalStorageUsageCallbackHelper( |
| 41 const DOMStorageContext::GetLocalStorageUsageCallback& callback, | 43 const DOMStorageContext::GetLocalStorageUsageCallback& callback, |
| 42 const std::vector<LocalStorageUsageInfo>* infos) { | 44 const std::vector<LocalStorageUsageInfo>* infos) { |
| 43 callback.Run(*infos); | 45 callback.Run(*infos); |
| 44 } | 46 } |
| 45 | 47 |
| 46 void GetLocalStorageUsageHelper( | 48 void GetLocalStorageUsageHelper( |
| 49 std::vector<LocalStorageUsageInfo> mojo_usage, | |
| 47 base::SingleThreadTaskRunner* reply_task_runner, | 50 base::SingleThreadTaskRunner* reply_task_runner, |
| 48 DOMStorageContextImpl* context, | 51 DOMStorageContextImpl* context, |
| 49 const DOMStorageContext::GetLocalStorageUsageCallback& callback) { | 52 const DOMStorageContext::GetLocalStorageUsageCallback& callback) { |
| 50 std::vector<LocalStorageUsageInfo>* infos = | 53 std::vector<LocalStorageUsageInfo>* infos = |
| 51 new std::vector<LocalStorageUsageInfo>; | 54 new std::vector<LocalStorageUsageInfo>(std::move(mojo_usage)); |
| 52 context->GetLocalStorageUsage(infos, true); | 55 context->GetLocalStorageUsage(infos, true); |
| 53 reply_task_runner->PostTask( | 56 reply_task_runner->PostTask( |
| 54 FROM_HERE, base::Bind(&InvokeLocalStorageUsageCallbackHelper, callback, | 57 FROM_HERE, base::Bind(&InvokeLocalStorageUsageCallbackHelper, callback, |
| 55 base::Owned(infos))); | 58 base::Owned(infos))); |
| 56 } | 59 } |
| 57 | 60 |
| 58 void InvokeSessionStorageUsageCallbackHelper( | 61 void InvokeSessionStorageUsageCallbackHelper( |
| 59 const DOMStorageContext::GetSessionStorageUsageCallback& callback, | 62 const DOMStorageContext::GetSessionStorageUsageCallback& callback, |
| 60 const std::vector<SessionStorageUsageInfo>* infos) { | 63 const std::vector<SessionStorageUsageInfo>* infos) { |
| 61 callback.Run(*infos); | 64 callback.Run(*infos); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 73 base::Owned(infos))); | 76 base::Owned(infos))); |
| 74 } | 77 } |
| 75 | 78 |
| 76 } // namespace | 79 } // namespace |
| 77 | 80 |
| 78 DOMStorageContextWrapper::DOMStorageContextWrapper( | 81 DOMStorageContextWrapper::DOMStorageContextWrapper( |
| 79 service_manager::Connector* connector, | 82 service_manager::Connector* connector, |
| 80 const base::FilePath& profile_path, | 83 const base::FilePath& profile_path, |
| 81 const base::FilePath& local_partition_path, | 84 const base::FilePath& local_partition_path, |
| 82 storage::SpecialStoragePolicy* special_storage_policy) { | 85 storage::SpecialStoragePolicy* special_storage_policy) { |
| 83 base::FilePath storage_dir; | 86 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 84 if (!profile_path.empty()) | 87 switches::kMojoLocalStorage)) { |
| 85 storage_dir = local_partition_path.AppendASCII(kLocalStorageDirectory); | 88 base::FilePath storage_dir; |
| 86 // TODO(michaeln): Enable writing to disk when db is versioned, | 89 if (!profile_path.empty()) |
| 87 // for now using an empty subdirectory to use an in-memory db. | 90 storage_dir = local_partition_path.AppendASCII(kLocalStorageDirectory); |
| 88 // subdirectory_(subdirectory), | 91 // TODO(michaeln): Enable writing to disk when db is versioned, |
| 89 mojo_state_.reset(new LocalStorageContextMojo( | 92 // for now using an empty subdirectory to use an in-memory db. |
| 90 connector, base::FilePath() /* storage_dir */)); | 93 // subdirectory_(subdirectory), |
| 94 mojo_state_.reset(new LocalStorageContextMojo( | |
| 95 connector, base::FilePath() /* storage_dir */)); | |
| 96 } | |
| 91 | 97 |
| 92 base::FilePath data_path; | 98 base::FilePath data_path; |
| 93 if (!profile_path.empty()) | 99 if (!profile_path.empty()) |
| 94 data_path = profile_path.Append(local_partition_path); | 100 data_path = profile_path.Append(local_partition_path); |
| 95 | 101 |
| 96 scoped_refptr<base::SequencedTaskRunner> primary_sequence; | 102 scoped_refptr<base::SequencedTaskRunner> primary_sequence; |
| 97 scoped_refptr<base::SequencedTaskRunner> commit_sequence; | 103 scoped_refptr<base::SequencedTaskRunner> commit_sequence; |
| 98 if (GetContentClient()->browser()->ShouldRedirectDOMStorageTaskRunner()) { | 104 if (GetContentClient()->browser()->ShouldRedirectDOMStorageTaskRunner()) { |
| 99 // TaskPriority::USER_BLOCKING as an experiment because this is currently | 105 // TaskPriority::USER_BLOCKING as an experiment because this is currently |
| 100 // believed to be blocking synchronous IPCs from the renderers: | 106 // believed to be blocking synchronous IPCs from the renderers: |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 memory_pressure_listener_.reset(new base::MemoryPressureListener( | 140 memory_pressure_listener_.reset(new base::MemoryPressureListener( |
| 135 base::Bind(&DOMStorageContextWrapper::OnMemoryPressure, this))); | 141 base::Bind(&DOMStorageContextWrapper::OnMemoryPressure, this))); |
| 136 } | 142 } |
| 137 } | 143 } |
| 138 | 144 |
| 139 DOMStorageContextWrapper::~DOMStorageContextWrapper() {} | 145 DOMStorageContextWrapper::~DOMStorageContextWrapper() {} |
| 140 | 146 |
| 141 void DOMStorageContextWrapper::GetLocalStorageUsage( | 147 void DOMStorageContextWrapper::GetLocalStorageUsage( |
| 142 const GetLocalStorageUsageCallback& callback) { | 148 const GetLocalStorageUsageCallback& callback) { |
| 143 DCHECK(context_.get()); | 149 DCHECK(context_.get()); |
| 150 if (mojo_state_) { | |
| 151 mojo_state_->GetStorageUsage(base::BindOnce( | |
| 152 &DOMStorageContextWrapper::GotMojoLocalStorageUsage, this, callback)); | |
| 153 return; | |
| 154 } | |
| 144 context_->task_runner()->PostShutdownBlockingTask( | 155 context_->task_runner()->PostShutdownBlockingTask( |
| 145 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 156 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
| 146 base::Bind(&GetLocalStorageUsageHelper, | 157 base::Bind(&GetLocalStorageUsageHelper, |
| 158 std::vector<LocalStorageUsageInfo>(), | |
| 147 base::RetainedRef(base::ThreadTaskRunnerHandle::Get()), | 159 base::RetainedRef(base::ThreadTaskRunnerHandle::Get()), |
| 148 base::RetainedRef(context_), callback)); | 160 base::RetainedRef(context_), callback)); |
| 149 } | 161 } |
| 150 | 162 |
| 151 void DOMStorageContextWrapper::GetSessionStorageUsage( | 163 void DOMStorageContextWrapper::GetSessionStorageUsage( |
| 152 const GetSessionStorageUsageCallback& callback) { | 164 const GetSessionStorageUsageCallback& callback) { |
| 153 DCHECK(context_.get()); | 165 DCHECK(context_.get()); |
| 154 context_->task_runner()->PostShutdownBlockingTask( | 166 context_->task_runner()->PostShutdownBlockingTask( |
| 155 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 167 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
| 156 base::Bind(&GetSessionStorageUsageHelper, | 168 base::Bind(&GetSessionStorageUsageHelper, |
| 157 base::RetainedRef(base::ThreadTaskRunnerHandle::Get()), | 169 base::RetainedRef(base::ThreadTaskRunnerHandle::Get()), |
| 158 base::RetainedRef(context_), callback)); | 170 base::RetainedRef(context_), callback)); |
| 159 } | 171 } |
| 160 | 172 |
| 161 void DOMStorageContextWrapper::DeleteLocalStorageForPhysicalOrigin( | 173 void DOMStorageContextWrapper::DeleteLocalStorageForPhysicalOrigin( |
| 162 const GURL& origin) { | 174 const GURL& origin) { |
| 163 DCHECK(context_.get()); | 175 DCHECK(context_.get()); |
| 164 context_->task_runner()->PostShutdownBlockingTask( | 176 context_->task_runner()->PostShutdownBlockingTask( |
| 165 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 177 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
| 166 base::Bind(&DOMStorageContextImpl::DeleteLocalStorageForPhysicalOrigin, | 178 base::Bind(&DOMStorageContextImpl::DeleteLocalStorageForPhysicalOrigin, |
| 167 context_, origin)); | 179 context_, origin)); |
| 180 if (mojo_state_) | |
| 181 mojo_state_->DeleteStorageForPhysicalOrigin(url::Origin(origin)); | |
| 168 } | 182 } |
| 169 | 183 |
| 170 void DOMStorageContextWrapper::DeleteLocalStorage(const GURL& origin) { | 184 void DOMStorageContextWrapper::DeleteLocalStorage(const GURL& origin) { |
| 171 DCHECK(context_.get()); | 185 DCHECK(context_.get()); |
| 172 context_->task_runner()->PostShutdownBlockingTask( | 186 context_->task_runner()->PostShutdownBlockingTask( |
| 173 FROM_HERE, | 187 FROM_HERE, |
| 174 DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 188 DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
| 175 base::Bind(&DOMStorageContextImpl::DeleteLocalStorage, context_, origin)); | 189 base::Bind(&DOMStorageContextImpl::DeleteLocalStorage, context_, origin)); |
| 190 if (mojo_state_) | |
| 191 mojo_state_->DeleteStorage(url::Origin(origin)); | |
| 176 } | 192 } |
| 177 | 193 |
| 178 void DOMStorageContextWrapper::DeleteSessionStorage( | 194 void DOMStorageContextWrapper::DeleteSessionStorage( |
| 179 const SessionStorageUsageInfo& usage_info) { | 195 const SessionStorageUsageInfo& usage_info) { |
| 180 DCHECK(context_.get()); | 196 DCHECK(context_.get()); |
| 181 context_->task_runner()->PostShutdownBlockingTask( | 197 context_->task_runner()->PostShutdownBlockingTask( |
| 182 FROM_HERE, | 198 FROM_HERE, |
| 183 DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 199 DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
| 184 base::Bind(&DOMStorageContextImpl::DeleteSessionStorage, | 200 base::Bind(&DOMStorageContextImpl::DeleteSessionStorage, |
| 185 context_, usage_info)); | 201 context_, usage_info)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(this); | 242 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(this); |
| 227 } | 243 } |
| 228 | 244 |
| 229 } | 245 } |
| 230 | 246 |
| 231 void DOMStorageContextWrapper::Flush() { | 247 void DOMStorageContextWrapper::Flush() { |
| 232 DCHECK(context_.get()); | 248 DCHECK(context_.get()); |
| 233 context_->task_runner()->PostShutdownBlockingTask( | 249 context_->task_runner()->PostShutdownBlockingTask( |
| 234 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 250 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
| 235 base::Bind(&DOMStorageContextImpl::Flush, context_)); | 251 base::Bind(&DOMStorageContextImpl::Flush, context_)); |
| 252 if (mojo_state_) | |
| 253 mojo_state_->Flush(); | |
| 236 } | 254 } |
| 237 | 255 |
| 238 void DOMStorageContextWrapper::OpenLocalStorage( | 256 void DOMStorageContextWrapper::OpenLocalStorage( |
| 239 const url::Origin& origin, | 257 const url::Origin& origin, |
| 240 mojom::LevelDBWrapperRequest request) { | 258 mojom::LevelDBWrapperRequest request) { |
| 259 if (!mojo_state_) | |
| 260 return; | |
| 241 mojo_state_->OpenLocalStorage(origin, std::move(request)); | 261 mojo_state_->OpenLocalStorage(origin, std::move(request)); |
| 242 } | 262 } |
| 243 | 263 |
| 244 void DOMStorageContextWrapper::OnMemoryPressure( | 264 void DOMStorageContextWrapper::OnMemoryPressure( |
| 245 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | 265 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 246 DOMStorageContextImpl::PurgeOption purge_option = | 266 DOMStorageContextImpl::PurgeOption purge_option = |
| 247 DOMStorageContextImpl::PURGE_UNOPENED; | 267 DOMStorageContextImpl::PURGE_UNOPENED; |
| 248 if (memory_pressure_level == | 268 if (memory_pressure_level == |
| 249 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { | 269 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
| 250 purge_option = DOMStorageContextImpl::PURGE_AGGRESSIVE; | 270 purge_option = DOMStorageContextImpl::PURGE_AGGRESSIVE; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 274 } | 294 } |
| 275 } | 295 } |
| 276 | 296 |
| 277 void DOMStorageContextWrapper::PurgeMemory(DOMStorageContextImpl::PurgeOption | 297 void DOMStorageContextWrapper::PurgeMemory(DOMStorageContextImpl::PurgeOption |
| 278 purge_option) { | 298 purge_option) { |
| 279 context_->task_runner()->PostTask( | 299 context_->task_runner()->PostTask( |
| 280 FROM_HERE, | 300 FROM_HERE, |
| 281 base::Bind(&DOMStorageContextImpl::PurgeMemory, context_, purge_option)); | 301 base::Bind(&DOMStorageContextImpl::PurgeMemory, context_, purge_option)); |
| 282 } | 302 } |
| 283 | 303 |
| 304 void DOMStorageContextWrapper::GotMojoLocalStorageUsage( | |
| 305 GetLocalStorageUsageCallback callback, | |
| 306 std::vector<LocalStorageUsageInfo> usage) { | |
| 307 context_->task_runner()->PostShutdownBlockingTask( | |
|
michaeln
2017/01/04 21:14:44
Not sure you want to handle this in the current CL
Marijn Kruisselbrink
2017/01/04 21:17:36
Yeah, that's true. And that results in two "locals
| |
| 308 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | |
| 309 base::Bind(&GetLocalStorageUsageHelper, base::Passed(&usage), | |
| 310 base::RetainedRef(base::ThreadTaskRunnerHandle::Get()), | |
| 311 base::RetainedRef(context_), callback)); | |
| 312 } | |
| 313 | |
| 284 } // namespace content | 314 } // namespace content |
| OLD | NEW |