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"
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
);
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;
55 RunPendingRefreshCallbacks();
58 bool ProcessResourceUsage::ReportsV8MemoryStats() const {
59 DCHECK(thread_checker_
.CalledOnValidThread());
61 return stats_
->reports_v8_stats
;
65 size_t ProcessResourceUsage::GetV8MemoryAllocated() const {
66 DCHECK(thread_checker_
.CalledOnValidThread());
67 if (stats_
&& stats_
->reports_v8_stats
)
68 return stats_
->v8_bytes_allocated
;
72 size_t ProcessResourceUsage::GetV8MemoryUsed() const {
73 DCHECK(thread_checker_
.CalledOnValidThread());
74 if (stats_
&& stats_
->reports_v8_stats
)
75 return stats_
->v8_bytes_used
;
79 blink::WebCache::ResourceTypeStats
ProcessResourceUsage::GetWebCoreCacheStats()
81 DCHECK(thread_checker_
.CalledOnValidThread());
82 if (stats_
&& stats_
->web_cache_stats
)
83 return stats_
->web_cache_stats
->To
<blink::WebCache::ResourceTypeStats
>();