android-webview: Remove legacy crash handler (### Version)
[chromium-blink-merge.git] / chrome / browser / process_resource_usage.cc
blob27413c60f10b685431216f9ceccbd4b176cc6a3a
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/message_loop/message_loop_proxy.h"
11 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
13 class ProcessResourceUsage::ErrorHandler : public mojo::ErrorHandler {
14 public:
15 ErrorHandler(ProcessResourceUsage* usage) : usage_(usage) {}
17 // mojo::ErrorHandler implementation:
18 void OnConnectionError() override;
20 private:
21 ProcessResourceUsage* usage_; // Not owned.
24 void ProcessResourceUsage::ErrorHandler::OnConnectionError() {
25 usage_->RunPendingRefreshCallbacks();
28 ProcessResourceUsage::ProcessResourceUsage(ResourceUsageReporterPtr service)
29 : service_(service.Pass()),
30 update_in_progress_(false),
31 error_handler_(new ErrorHandler(this)) {
32 service_.set_error_handler(error_handler_.get());
35 ProcessResourceUsage::~ProcessResourceUsage() {
36 DCHECK(thread_checker_.CalledOnValidThread());
39 void ProcessResourceUsage::RunPendingRefreshCallbacks() {
40 DCHECK(thread_checker_.CalledOnValidThread());
41 auto message_loop = base::MessageLoopProxy::current();
42 for (const auto& callback : refresh_callbacks_)
43 message_loop->PostTask(FROM_HERE, callback);
44 refresh_callbacks_.clear();
47 void ProcessResourceUsage::Refresh(const base::Closure& callback) {
48 DCHECK(thread_checker_.CalledOnValidThread());
49 if (!service_ || service_.encountered_error()) {
50 if (!callback.is_null())
51 base::MessageLoopProxy::current()->PostTask(FROM_HERE, callback);
52 return;
55 if (!callback.is_null())
56 refresh_callbacks_.push_back(callback);
58 if (!update_in_progress_) {
59 update_in_progress_ = true;
60 service_->GetUsageData(base::Bind(&ProcessResourceUsage::OnRefreshDone,
61 base::Unretained(this)));
65 void ProcessResourceUsage::OnRefreshDone(ResourceUsageDataPtr data) {
66 DCHECK(thread_checker_.CalledOnValidThread());
67 update_in_progress_ = false;
68 stats_ = data.Pass();
69 RunPendingRefreshCallbacks();
72 bool ProcessResourceUsage::ReportsV8MemoryStats() const {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 if (stats_)
75 return stats_->reports_v8_stats;
76 return false;
79 size_t ProcessResourceUsage::GetV8MemoryAllocated() const {
80 DCHECK(thread_checker_.CalledOnValidThread());
81 if (stats_ && stats_->reports_v8_stats)
82 return stats_->v8_bytes_allocated;
83 return 0;
86 size_t ProcessResourceUsage::GetV8MemoryUsed() const {
87 DCHECK(thread_checker_.CalledOnValidThread());
88 if (stats_ && stats_->reports_v8_stats)
89 return stats_->v8_bytes_used;
90 return 0;