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 #ifndef EXTENSIONS_BROWSER_LOAD_MONITORING_EXTENSION_HOST_QUEUE_H_
6 #define EXTENSIONS_BROWSER_LOAD_MONITORING_EXTENSION_HOST_QUEUE_H_
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/scoped_observer.h"
15 #include "base/time/time.h"
16 #include "extensions/browser/deferred_start_render_host_observer.h"
17 #include "extensions/browser/extension_host_queue.h"
19 namespace extensions
{
21 // An ExtensionHostQueue which just monitors, and later reports, how many
22 // ExtensionHosts are being loaded for some period of time.
23 class LoadMonitoringExtensionHostQueue
24 : public ExtensionHostQueue
,
25 public DeferredStartRenderHostObserver
{
27 // Construction for testing.
28 // Allows overriding the default timeout and triggering a callback when
29 // monitoring has finished (timeout has elapsed and UMA is logged).
30 using FinishedCallback
= base::Callback
<void(size_t, // num_queued
32 size_t, // max_in_queue
33 size_t // max_active_loading
35 LoadMonitoringExtensionHostQueue(scoped_ptr
<ExtensionHostQueue
> delegate
,
36 base::TimeDelta monitor_time
,
37 const FinishedCallback
& finished_callback
);
39 // Production code should use this constructor.
41 // Monitoring will not start until the first Add()ed
42 // DeferredStartRenderHost starts loading, or StartMonitoring() is called.
43 explicit LoadMonitoringExtensionHostQueue(
44 scoped_ptr
<ExtensionHostQueue
> delegate
);
46 ~LoadMonitoringExtensionHostQueue() override
;
50 // This can be called multiple times, but it has no effect if monitoring has
51 // already started (or finished). Monitoring cannot be restarted.
53 // Note that monitoring will automatically start when Add() is called, so it
54 // may not be necessary to call this at all.
55 void StartMonitoring();
57 // ExtensionHostQueue:
58 void Add(DeferredStartRenderHost
* host
) override
;
59 void Remove(DeferredStartRenderHost
* host
) override
;
61 // DeferredStartRenderHostObserver, public to be triggered by tests:
62 void OnDeferredStartRenderHostDidStartLoading(
63 const DeferredStartRenderHost
* host
) override
;
64 void OnDeferredStartRenderHostDidStopLoading(
65 const DeferredStartRenderHost
* host
) override
;
66 void OnDeferredStartRenderHostDestroyed(
67 const DeferredStartRenderHost
* host
) override
;
70 // Starts/finishes monitoring |host|, though either will have no effect if
71 // monitoring has already finished.
72 void StartMonitoringHost(const DeferredStartRenderHost
* host
);
73 void FinishMonitoringHost(const DeferredStartRenderHost
* host
);
75 // Removes |host| from the internal |in_queue_| tracking.
76 void RemoveFromQueue(const DeferredStartRenderHost
* host
);
78 // Called when monitoring should finish. Metrics are recorded, and from this
79 // point on no monitoring will take place.
80 void FinishMonitoring();
82 // Delegate actually loading DeferredStartRenderHosts to another queue.
83 scoped_ptr
<ExtensionHostQueue
> delegate_
;
85 // The amount of time to monitor for. By default this is 1 minute, but it can
86 // be overriden by tests.
87 base::TimeDelta monitor_time_
;
89 // A callback to run when monitoring has finished. Intended for testing.
90 FinishedCallback finished_callback_
;
92 // The hosts which are in the delegate's queue.
93 std::set
<DeferredStartRenderHost
*> in_queue_
;
94 // The hosts which are currently loading.
95 std::set
<const DeferredStartRenderHost
*> active_loading_
;
97 // True if this has started monitoring.
101 // The total number of hosts that were added to the queue.
103 // The total number of hosts that started loading.
105 // The maximum number of hosts in the queue at any time.
106 size_t max_in_queue_
;
107 // The maximum number of hosts that were loading at the same time.
108 size_t max_active_loading_
;
110 base::WeakPtrFactory
<LoadMonitoringExtensionHostQueue
> weak_ptr_factory_
;
112 DISALLOW_COPY_AND_ASSIGN(LoadMonitoringExtensionHostQueue
);
115 } // namespace extensions
117 #endif // EXTENSIONS_BROWSER_LOAD_MONITORING_EXTENSION_HOST_QUEUE_H_