Fix chrome.socket.tcp API to use all addresses received from DNS.
[chromium-blink-merge.git] / extensions / browser / load_monitoring_extension_host_queue.h
blob516ec5959838307052e52ff655f9badc0948901f
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_
8 #include <set>
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 {
26 public:
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
31 size_t, // max_loaded
32 size_t, // max_awaiting_loading
33 size_t // max_active_loading
34 )>;
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;
48 // Starts monitoring.
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 OnDeferredStartRenderHostDidStartFirstLoad(
63 const DeferredStartRenderHost* host) override;
64 void OnDeferredStartRenderHostDidStopFirstLoad(
65 const DeferredStartRenderHost* host) override;
66 void OnDeferredStartRenderHostDestroyed(
67 const DeferredStartRenderHost* host) override;
69 private:
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 // Called when monitoring should finish. Metrics are recorded, and from this
76 // point on no monitoring will take place.
77 void FinishMonitoring();
79 // Delegate actually loading DeferredStartRenderHosts to another queue.
80 scoped_ptr<ExtensionHostQueue> delegate_;
82 // The amount of time to monitor for. By default this is 1 minute, but it can
83 // be overriden by tests.
84 base::TimeDelta monitor_time_;
86 // A callback to run when monitoring has finished. Intended for testing.
87 FinishedCallback finished_callback_;
89 // The hosts which are waiting to start loading.
90 std::set<const DeferredStartRenderHost*> awaiting_loading_;
91 // The hosts which are currently loading.
92 std::set<const DeferredStartRenderHost*> active_loading_;
94 // True if this has started monitoring.
95 bool started_;
97 // Metrics:
98 // The total number of hosts that were added to the queue.
99 size_t num_queued_;
100 // The total number of hosts that started loading.
101 size_t num_loaded_;
102 // The maximum number of hosts waiting to load at the same time.
103 size_t max_awaiting_loading_;
104 // The maximum number of hosts that were loading at the same time.
105 size_t max_active_loading_;
107 base::WeakPtrFactory<LoadMonitoringExtensionHostQueue> weak_ptr_factory_;
109 DISALLOW_COPY_AND_ASSIGN(LoadMonitoringExtensionHostQueue);
112 } // namespace extensions
114 #endif // EXTENSIONS_BROWSER_LOAD_MONITORING_EXTENSION_HOST_QUEUE_H_