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"
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
),
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),
42 LoadMonitoringExtensionHostQueue::~LoadMonitoringExtensionHostQueue() {
45 void LoadMonitoringExtensionHostQueue::StartMonitoring() {
50 base::MessageLoop::current()->PostDelayedTask(
51 FROM_HERE
, base::Bind(&LoadMonitoringExtensionHostQueue::FinishMonitoring
,
52 weak_ptr_factory_
.GetWeakPtr()),
56 void LoadMonitoringExtensionHostQueue::Add(DeferredStartRenderHost
* host
) {
59 host
->AddDeferredStartRenderHostObserver(this);
60 if (awaiting_loading_
.insert(host
).second
) {
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
)) {
104 void LoadMonitoringExtensionHostQueue::FinishMonitoring() {
106 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumQueued",
108 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumLoaded",
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