summaryrefslogtreecommitdiffstats
path: root/extensions/browser/load_monitoring_extension_host_queue.cc
blob: 47e8557abc11936550ab3df6f74674b6ce5d8d4e (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
117
118
119
120
// 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.

#include "extensions/browser/load_monitoring_extension_host_queue.h"

#include <algorithm>
#include <utility>

#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram_macros.h"
#include "content/public/browser/render_frame_host.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_host_observer.h"
#include "extensions/browser/serial_extension_host_queue.h"

namespace extensions {

LoadMonitoringExtensionHostQueue::LoadMonitoringExtensionHostQueue(
    scoped_ptr<ExtensionHostQueue> delegate,
    base::TimeDelta monitor_time,
    const FinishedCallback& finished_callback)
    : delegate_(std::move(delegate)),
      monitor_time_(monitor_time),
      finished_callback_(finished_callback),
      started_(false),
      num_queued_(0u),
      num_loaded_(0u),
      max_awaiting_loading_(0u),
      max_active_loading_(0u),
      weak_ptr_factory_(this) {}

LoadMonitoringExtensionHostQueue::LoadMonitoringExtensionHostQueue(
    scoped_ptr<ExtensionHostQueue> delegate)
    : LoadMonitoringExtensionHostQueue(std::move(delegate),
                                       base::TimeDelta::FromMinutes(1),
                                       FinishedCallback()) {}

LoadMonitoringExtensionHostQueue::~LoadMonitoringExtensionHostQueue() {
}

void LoadMonitoringExtensionHostQueue::StartMonitoring() {
  if (started_) {
    return;
  }
  started_ = true;
  base::MessageLoop::current()->PostDelayedTask(
      FROM_HERE, base::Bind(&LoadMonitoringExtensionHostQueue::FinishMonitoring,
                            weak_ptr_factory_.GetWeakPtr()),
      monitor_time_);
}

void LoadMonitoringExtensionHostQueue::Add(DeferredStartRenderHost* host) {
  StartMonitoring();
  delegate_->Add(host);
  host->AddDeferredStartRenderHostObserver(this);
  if (awaiting_loading_.insert(host).second) {
    ++num_queued_;
    max_awaiting_loading_ =
        std::max(max_awaiting_loading_, awaiting_loading_.size());
  }
}

void LoadMonitoringExtensionHostQueue::Remove(DeferredStartRenderHost* host) {
  delegate_->Remove(host);
  host->RemoveDeferredStartRenderHostObserver(this);
}

void LoadMonitoringExtensionHostQueue::
    OnDeferredStartRenderHostDidStartFirstLoad(
        const DeferredStartRenderHost* host) {
  StartMonitoringHost(host);
}

void LoadMonitoringExtensionHostQueue::
    OnDeferredStartRenderHostDidStopFirstLoad(
        const DeferredStartRenderHost* host) {
  FinishMonitoringHost(host);
}

void LoadMonitoringExtensionHostQueue::OnDeferredStartRenderHostDestroyed(
    const DeferredStartRenderHost* host) {
  FinishMonitoringHost(host);
}

void LoadMonitoringExtensionHostQueue::StartMonitoringHost(
    const DeferredStartRenderHost* host) {
  awaiting_loading_.erase(host);
  if (active_loading_.insert(host).second) {
    max_active_loading_ = std::max(max_active_loading_, active_loading_.size());
  }
}

void LoadMonitoringExtensionHostQueue::FinishMonitoringHost(
    const DeferredStartRenderHost* host) {
  if (active_loading_.erase(host)) {
    ++num_loaded_;
  }
}

void LoadMonitoringExtensionHostQueue::FinishMonitoring() {
  CHECK(started_);
  UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumQueued",
                           num_queued_);
  UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumLoaded",
                           num_loaded_);
  UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.MaxInQueue",
                           max_awaiting_loading_);
  UMA_HISTOGRAM_COUNTS_100(
      "Extensions.ExtensionHostMonitoring.MaxActiveLoading",
      max_active_loading_);
  if (!finished_callback_.is_null()) {
    finished_callback_.Run(num_queued_, num_loaded_, max_awaiting_loading_,
                           max_active_loading_);
  }
}

}  // namespace extensions