summaryrefslogtreecommitdiffstats
path: root/extensions/browser/load_monitoring_extension_host_queue.h
blob: 661e80fe6cacfedbbbf547910729f5876a1cdb43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_LOAD_MONITORING_EXTENSION_HOST_QUEUE_H_
#define EXTENSIONS_BROWSER_LOAD_MONITORING_EXTENSION_HOST_QUEUE_H_

#include <stddef.h>

#include <set>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h"
#include "base/time/time.h"
#include "extensions/browser/deferred_start_render_host_observer.h"
#include "extensions/browser/extension_host_queue.h"

namespace extensions {

// An ExtensionHostQueue which just monitors, and later reports, how many
// ExtensionHosts are being loaded for some period of time.
class LoadMonitoringExtensionHostQueue
    : public ExtensionHostQueue,
      public DeferredStartRenderHostObserver {
 public:
  // Construction for testing.
  // Allows overriding the default timeout and triggering a callback when
  // monitoring has finished (timeout has elapsed and UMA is logged).
  using FinishedCallback = base::Callback<void(size_t,  // num_queued
                                               size_t,  // max_loaded
                                               size_t,  // max_awaiting_loading
                                               size_t   // max_active_loading
                                               )>;
  LoadMonitoringExtensionHostQueue(scoped_ptr<ExtensionHostQueue> delegate,
                                   base::TimeDelta monitor_time,
                                   const FinishedCallback& finished_callback);

  // Production code should use this constructor.
  //
  // Monitoring will not start until the first Add()ed
  // DeferredStartRenderHost starts loading, or StartMonitoring() is called.
  explicit LoadMonitoringExtensionHostQueue(
      scoped_ptr<ExtensionHostQueue> delegate);

  ~LoadMonitoringExtensionHostQueue() override;

  // Starts monitoring.
  //
  // This can be called multiple times, but it has no effect if monitoring has
  // already started (or finished). Monitoring cannot be restarted.
  //
  // Note that monitoring will automatically start when Add() is called, so it
  // may not be necessary to call this at all.
  void StartMonitoring();

  // ExtensionHostQueue:
  void Add(DeferredStartRenderHost* host) override;
  void Remove(DeferredStartRenderHost* host) override;

  // DeferredStartRenderHostObserver, public to be triggered by tests:
  void OnDeferredStartRenderHostDidStartFirstLoad(
      const DeferredStartRenderHost* host) override;
  void OnDeferredStartRenderHostDidStopFirstLoad(
      const DeferredStartRenderHost* host) override;
  void OnDeferredStartRenderHostDestroyed(
      const DeferredStartRenderHost* host) override;

 private:
  // Starts/finishes monitoring |host|, though either will have no effect if
  // monitoring has already finished.
  void StartMonitoringHost(const DeferredStartRenderHost* host);
  void FinishMonitoringHost(const DeferredStartRenderHost* host);

  // Called when monitoring should finish. Metrics are recorded, and from this
  // point on no monitoring will take place.
  void FinishMonitoring();

  // Delegate actually loading DeferredStartRenderHosts to another queue.
  scoped_ptr<ExtensionHostQueue> delegate_;

  // The amount of time to monitor for. By default this is 1 minute, but it can
  // be overriden by tests.
  base::TimeDelta monitor_time_;

  // A callback to run when monitoring has finished. Intended for testing.
  FinishedCallback finished_callback_;

  // The hosts which are waiting to start loading.
  std::set<const DeferredStartRenderHost*> awaiting_loading_;
  // The hosts which are currently loading.
  std::set<const DeferredStartRenderHost*> active_loading_;

  // True if this has started monitoring.
  bool started_;

  // Metrics:
  // The total number of hosts that were added to the queue.
  size_t num_queued_;
  // The total number of hosts that started loading.
  size_t num_loaded_;
  // The maximum number of hosts waiting to load at the same time.
  size_t max_awaiting_loading_;
  // The maximum number of hosts that were loading at the same time.
  size_t max_active_loading_;

  base::WeakPtrFactory<LoadMonitoringExtensionHostQueue> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(LoadMonitoringExtensionHostQueue);
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_LOAD_MONITORING_EXTENSION_HOST_QUEUE_H_