blob: b15ce769559f32e15ca4a1ccc530588b485c79f2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/process_resource_usage.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "chrome/common/resource_usage_reporter_type_converters.h"
ProcessResourceUsage::ProcessResourceUsage(ResourceUsageReporterPtr service)
: service_(service.Pass()), update_in_progress_(false) {
service_.set_connection_error_handler(
base::Bind(&ProcessResourceUsage::RunPendingRefreshCallbacks,
base::Unretained(this)));
}
ProcessResourceUsage::~ProcessResourceUsage() {
DCHECK(thread_checker_.CalledOnValidThread());
}
void ProcessResourceUsage::RunPendingRefreshCallbacks() {
DCHECK(thread_checker_.CalledOnValidThread());
auto task_runner = base::ThreadTaskRunnerHandle::Get();
for (const auto& callback : refresh_callbacks_)
task_runner->PostTask(FROM_HERE, callback);
refresh_callbacks_.clear();
}
void ProcessResourceUsage::Refresh(const base::Closure& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!service_ || service_.encountered_error()) {
if (!callback.is_null())
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
return;
}
if (!callback.is_null())
refresh_callbacks_.push_back(callback);
if (!update_in_progress_) {
update_in_progress_ = true;
service_->GetUsageData(base::Bind(&ProcessResourceUsage::OnRefreshDone,
base::Unretained(this)));
}
}
void ProcessResourceUsage::OnRefreshDone(ResourceUsageDataPtr data) {
DCHECK(thread_checker_.CalledOnValidThread());
update_in_progress_ = false;
stats_ = data.Pass();
RunPendingRefreshCallbacks();
}
bool ProcessResourceUsage::ReportsV8MemoryStats() const {
DCHECK(thread_checker_.CalledOnValidThread());
if (stats_)
return stats_->reports_v8_stats;
return false;
}
size_t ProcessResourceUsage::GetV8MemoryAllocated() const {
DCHECK(thread_checker_.CalledOnValidThread());
if (stats_ && stats_->reports_v8_stats)
return stats_->v8_bytes_allocated;
return 0;
}
size_t ProcessResourceUsage::GetV8MemoryUsed() const {
DCHECK(thread_checker_.CalledOnValidThread());
if (stats_ && stats_->reports_v8_stats)
return stats_->v8_bytes_used;
return 0;
}
blink::WebCache::ResourceTypeStats ProcessResourceUsage::GetWebCoreCacheStats()
const {
DCHECK(thread_checker_.CalledOnValidThread());
if (stats_ && stats_->web_cache_stats)
return stats_->web_cache_stats->To<blink::WebCache::ResourceTypeStats>();
return {};
}
|