Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / extensions / browser / load_monitoring_extension_host_queue.cc
blobe26070abbd85d84447dec9fef236b554d8ef4d71
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 "extensions/browser/load_monitoring_extension_host_queue.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/histogram_macros.h"
13 #include "content/public/browser/render_frame_host.h"
14 #include "extensions/browser/extension_host.h"
15 #include "extensions/browser/extension_host_observer.h"
16 #include "extensions/browser/serial_extension_host_queue.h"
18 namespace extensions {
20 LoadMonitoringExtensionHostQueue::LoadMonitoringExtensionHostQueue(
21 scoped_ptr<ExtensionHostQueue> delegate,
22 base::TimeDelta monitor_time,
23 const FinishedCallback& finished_callback)
24 : delegate_(delegate.Pass()),
25 monitor_time_(monitor_time),
26 finished_callback_(finished_callback),
27 started_(false),
28 num_queued_(0u),
29 num_loaded_(0u),
30 max_awaiting_loading_(0u),
31 max_active_loading_(0u),
32 weak_ptr_factory_(this) {
35 LoadMonitoringExtensionHostQueue::LoadMonitoringExtensionHostQueue(
36 scoped_ptr<ExtensionHostQueue> delegate)
37 : LoadMonitoringExtensionHostQueue(delegate.Pass(),
38 base::TimeDelta::FromMinutes(1),
39 FinishedCallback()) {
42 LoadMonitoringExtensionHostQueue::~LoadMonitoringExtensionHostQueue() {
45 void LoadMonitoringExtensionHostQueue::StartMonitoring() {
46 if (started_) {
47 return;
49 started_ = true;
50 base::MessageLoop::current()->PostDelayedTask(
51 FROM_HERE, base::Bind(&LoadMonitoringExtensionHostQueue::FinishMonitoring,
52 weak_ptr_factory_.GetWeakPtr()),
53 monitor_time_);
56 void LoadMonitoringExtensionHostQueue::Add(DeferredStartRenderHost* host) {
57 StartMonitoring();
58 delegate_->Add(host);
59 host->AddDeferredStartRenderHostObserver(this);
60 if (awaiting_loading_.insert(host).second) {
61 ++num_queued_;
62 max_awaiting_loading_ =
63 std::max(max_awaiting_loading_, awaiting_loading_.size());
67 void LoadMonitoringExtensionHostQueue::Remove(DeferredStartRenderHost* host) {
68 delegate_->Remove(host);
69 host->RemoveDeferredStartRenderHostObserver(this);
72 void LoadMonitoringExtensionHostQueue::
73 OnDeferredStartRenderHostDidStartFirstLoad(
74 const DeferredStartRenderHost* host) {
75 StartMonitoringHost(host);
78 void LoadMonitoringExtensionHostQueue::
79 OnDeferredStartRenderHostDidStopFirstLoad(
80 const DeferredStartRenderHost* host) {
81 FinishMonitoringHost(host);
84 void LoadMonitoringExtensionHostQueue::OnDeferredStartRenderHostDestroyed(
85 const DeferredStartRenderHost* host) {
86 FinishMonitoringHost(host);
89 void LoadMonitoringExtensionHostQueue::StartMonitoringHost(
90 const DeferredStartRenderHost* host) {
91 awaiting_loading_.erase(host);
92 if (active_loading_.insert(host).second) {
93 max_active_loading_ = std::max(max_active_loading_, active_loading_.size());
97 void LoadMonitoringExtensionHostQueue::FinishMonitoringHost(
98 const DeferredStartRenderHost* host) {
99 if (active_loading_.erase(host)) {
100 ++num_loaded_;
104 void LoadMonitoringExtensionHostQueue::FinishMonitoring() {
105 CHECK(started_);
106 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumQueued",
107 num_queued_);
108 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumLoaded",
109 num_loaded_);
110 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.MaxInQueue",
111 max_awaiting_loading_);
112 UMA_HISTOGRAM_COUNTS_100(
113 "Extensions.ExtensionHostMonitoring.MaxActiveLoading",
114 max_active_loading_);
115 if (!finished_callback_.is_null()) {
116 finished_callback_.Run(num_queued_, num_loaded_, max_awaiting_loading_,
117 max_active_loading_);
121 } // namespace extensions