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
),
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 if (in_queue_
.insert(host
).second
) {
61 max_in_queue_
= std::max(max_in_queue_
, in_queue_
.size());
62 host
->AddDeferredStartRenderHostObserver(this);
66 void LoadMonitoringExtensionHostQueue::Remove(DeferredStartRenderHost
* host
) {
67 delegate_
->Remove(host
);
68 RemoveFromQueue(host
);
71 void LoadMonitoringExtensionHostQueue::OnDeferredStartRenderHostDidStartLoading(
72 const DeferredStartRenderHost
* host
) {
73 StartMonitoringHost(host
);
76 void LoadMonitoringExtensionHostQueue::OnDeferredStartRenderHostDidStopLoading(
77 const DeferredStartRenderHost
* host
) {
78 FinishMonitoringHost(host
);
81 void LoadMonitoringExtensionHostQueue::OnDeferredStartRenderHostDestroyed(
82 const DeferredStartRenderHost
* host
) {
83 FinishMonitoringHost(host
);
86 void LoadMonitoringExtensionHostQueue::StartMonitoringHost(
87 const DeferredStartRenderHost
* host
) {
88 if (active_loading_
.insert(host
).second
) {
89 max_active_loading_
= std::max(max_active_loading_
, active_loading_
.size());
91 RemoveFromQueue(host
);
94 void LoadMonitoringExtensionHostQueue::FinishMonitoringHost(
95 const DeferredStartRenderHost
* host
) {
96 if (active_loading_
.erase(host
)) {
101 void LoadMonitoringExtensionHostQueue::RemoveFromQueue(
102 const DeferredStartRenderHost
* const_host
) {
103 // This odd code is needed because StartMonitoringHost() gives us a const
104 // host, but we need a non-const one for
105 // RemoveDeferredStartRenderHostObserver().
106 for (DeferredStartRenderHost
* host
: in_queue_
) {
107 if (host
== const_host
) {
108 host
->RemoveDeferredStartRenderHostObserver(this);
109 in_queue_
.erase(host
); // uhoh, iterator invalidated!
115 void LoadMonitoringExtensionHostQueue::FinishMonitoring() {
117 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumQueued",
119 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumLoaded",
121 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.MaxInQueue",
123 UMA_HISTOGRAM_COUNTS_100(
124 "Extensions.ExtensionHostMonitoring.MaxActiveLoading",
125 max_active_loading_
);
126 if (!finished_callback_
.is_null()) {
127 finished_callback_
.Run(num_queued_
, num_loaded_
, max_in_queue_
,
128 max_active_loading_
);
132 } // namespace extensions