| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/memory/memory_coordinator_impl.h" | 5 #include "content/browser/memory/memory_coordinator_impl.h" |
| 6 | 6 |
| 7 #include "base/memory/memory_coordinator_client_registry.h" | 7 #include "base/memory/memory_coordinator_client_registry.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/process/process_handle.h" | 10 #include "base/process/process_handle.h" |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 // as an actual instance. | 147 // as an actual instance. |
| 148 struct MemoryCoordinatorImplSingletonTraits | 148 struct MemoryCoordinatorImplSingletonTraits |
| 149 : public base::LeakySingletonTraits<MemoryCoordinatorImpl> { | 149 : public base::LeakySingletonTraits<MemoryCoordinatorImpl> { |
| 150 static MemoryCoordinatorImpl* New() { | 150 static MemoryCoordinatorImpl* New() { |
| 151 return new MemoryCoordinatorImpl(base::ThreadTaskRunnerHandle::Get(), | 151 return new MemoryCoordinatorImpl(base::ThreadTaskRunnerHandle::Get(), |
| 152 CreateMemoryMonitor()); | 152 CreateMemoryMonitor()); |
| 153 } | 153 } |
| 154 }; | 154 }; |
| 155 | 155 |
| 156 // static | 156 // static |
| 157 MemoryCoordinator* MemoryCoordinator::GetInstance() { |
| 158 return MemoryCoordinatorImpl::GetInstance(); |
| 159 } |
| 160 |
| 161 // static |
| 157 MemoryCoordinatorImpl* MemoryCoordinatorImpl::GetInstance() { | 162 MemoryCoordinatorImpl* MemoryCoordinatorImpl::GetInstance() { |
| 158 if (!base::FeatureList::IsEnabled(features::kMemoryCoordinator)) | 163 if (!base::FeatureList::IsEnabled(features::kMemoryCoordinator)) |
| 159 return nullptr; | 164 return nullptr; |
| 160 return base::Singleton<MemoryCoordinatorImpl, | 165 return base::Singleton<MemoryCoordinatorImpl, |
| 161 MemoryCoordinatorImplSingletonTraits>::get(); | 166 MemoryCoordinatorImplSingletonTraits>::get(); |
| 162 } | 167 } |
| 163 | 168 |
| 164 MemoryCoordinatorImpl::MemoryCoordinatorImpl( | 169 MemoryCoordinatorImpl::MemoryCoordinatorImpl( |
| 165 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 170 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 166 std::unique_ptr<MemoryMonitor> memory_monitor) | 171 std::unique_ptr<MemoryMonitor> memory_monitor) |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 if (!process) | 295 if (!process) |
| 291 return; | 296 return; |
| 292 auto iter = children().find(process->GetID()); | 297 auto iter = children().find(process->GetID()); |
| 293 if (iter == children().end()) | 298 if (iter == children().end()) |
| 294 return; | 299 return; |
| 295 iter->second.is_visible = *Details<bool>(details).ptr(); | 300 iter->second.is_visible = *Details<bool>(details).ptr(); |
| 296 auto new_state = GetGlobalMemoryState(); | 301 auto new_state = GetGlobalMemoryState(); |
| 297 SetChildMemoryState(iter->first, new_state); | 302 SetChildMemoryState(iter->first, new_state); |
| 298 } | 303 } |
| 299 | 304 |
| 305 base::MemoryState MemoryCoordinatorImpl::GetStateForProcess( |
| 306 base::ProcessHandle handle) { |
| 307 DCHECK(CalledOnValidThread()); |
| 308 if (handle == base::kNullProcessHandle) |
| 309 return MemoryState::UNKNOWN; |
| 310 if (handle == base::GetCurrentProcessHandle()) |
| 311 return GetCurrentMemoryState(); |
| 312 |
| 313 for (auto& iter : children()) { |
| 314 auto* render_process_host = GetRenderProcessHost(iter.first); |
| 315 if (render_process_host && render_process_host->GetHandle() == handle) |
| 316 return iter.second.memory_state; |
| 317 } |
| 318 return MemoryState::UNKNOWN; |
| 319 } |
| 320 |
| 300 bool MemoryCoordinatorImpl::ChangeStateIfNeeded(base::MemoryState prev_state, | 321 bool MemoryCoordinatorImpl::ChangeStateIfNeeded(base::MemoryState prev_state, |
| 301 base::MemoryState next_state) { | 322 base::MemoryState next_state) { |
| 302 DCHECK(CalledOnValidThread()); | 323 DCHECK(CalledOnValidThread()); |
| 303 if (prev_state == next_state) | 324 if (prev_state == next_state) |
| 304 return false; | 325 return false; |
| 305 | 326 |
| 306 base::TimeTicks prev_last_state_change = last_state_change_; | 327 base::TimeTicks prev_last_state_change = last_state_change_; |
| 307 last_state_change_ = base::TimeTicks::Now(); | 328 last_state_change_ = base::TimeTicks::Now(); |
| 308 current_state_ = next_state; | 329 current_state_ = next_state; |
| 309 | 330 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 | 455 |
| 435 MemoryCoordinatorImpl::ChildInfo::ChildInfo() {} | 456 MemoryCoordinatorImpl::ChildInfo::ChildInfo() {} |
| 436 | 457 |
| 437 MemoryCoordinatorImpl::ChildInfo::ChildInfo(const ChildInfo& rhs) { | 458 MemoryCoordinatorImpl::ChildInfo::ChildInfo(const ChildInfo& rhs) { |
| 438 // This is a nop, but exists for compatibility with STL containers. | 459 // This is a nop, but exists for compatibility with STL containers. |
| 439 } | 460 } |
| 440 | 461 |
| 441 MemoryCoordinatorImpl::ChildInfo::~ChildInfo() {} | 462 MemoryCoordinatorImpl::ChildInfo::~ChildInfo() {} |
| 442 | 463 |
| 443 } // namespace content | 464 } // namespace content |
| OLD | NEW |