Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / process_resource_usage.cc
blobb15ce769559f32e15ca4a1ccc530588b485c79f2
1 // Copyright 2015 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 #include "chrome/browser/process_resource_usage.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/common/resource_usage_reporter_type_converters.h"
14 ProcessResourceUsage::ProcessResourceUsage(ResourceUsageReporterPtr service)
15 : service_(service.Pass()), update_in_progress_(false) {
16 service_.set_connection_error_handler(
17 base::Bind(&ProcessResourceUsage::RunPendingRefreshCallbacks,
18 base::Unretained(this)));
21 ProcessResourceUsage::~ProcessResourceUsage() {
22 DCHECK(thread_checker_.CalledOnValidThread());
25 void ProcessResourceUsage::RunPendingRefreshCallbacks() {
26 DCHECK(thread_checker_.CalledOnValidThread());
27 auto task_runner = base::ThreadTaskRunnerHandle::Get();
28 for (const auto& callback : refresh_callbacks_)
29 task_runner->PostTask(FROM_HERE, callback);
30 refresh_callbacks_.clear();
33 void ProcessResourceUsage::Refresh(const base::Closure& callback) {
34 DCHECK(thread_checker_.CalledOnValidThread());
35 if (!service_ || service_.encountered_error()) {
36 if (!callback.is_null())
37 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
38 return;
41 if (!callback.is_null())
42 refresh_callbacks_.push_back(callback);
44 if (!update_in_progress_) {
45 update_in_progress_ = true;
46 service_->GetUsageData(base::Bind(&ProcessResourceUsage::OnRefreshDone,
47 base::Unretained(this)));
51 void ProcessResourceUsage::OnRefreshDone(ResourceUsageDataPtr data) {
52 DCHECK(thread_checker_.CalledOnValidThread());
53 update_in_progress_ = false;
54 stats_ = data.Pass();
55 RunPendingRefreshCallbacks();
58 bool ProcessResourceUsage::ReportsV8MemoryStats() const {
59 DCHECK(thread_checker_.CalledOnValidThread());
60 if (stats_)
61 return stats_->reports_v8_stats;
62 return false;
65 size_t ProcessResourceUsage::GetV8MemoryAllocated() const {
66 DCHECK(thread_checker_.CalledOnValidThread());
67 if (stats_ && stats_->reports_v8_stats)
68 return stats_->v8_bytes_allocated;
69 return 0;
72 size_t ProcessResourceUsage::GetV8MemoryUsed() const {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 if (stats_ && stats_->reports_v8_stats)
75 return stats_->v8_bytes_used;
76 return 0;
79 blink::WebCache::ResourceTypeStats ProcessResourceUsage::GetWebCoreCacheStats()
80 const {
81 DCHECK(thread_checker_.CalledOnValidThread());
82 if (stats_ && stats_->web_cache_stats)
83 return stats_->web_cache_stats->To<blink::WebCache::ResourceTypeStats>();
84 return {};