| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/load_monitoring_extension_host_queue.h" | 5 #include "extensions/browser/load_monitoring_extension_host_queue.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "content/public/browser/render_frame_host.h" | 13 #include "content/public/browser/render_frame_host.h" |
| 14 #include "extensions/browser/extension_host.h" | 14 #include "extensions/browser/extension_host.h" |
| 15 #include "extensions/browser/extension_host_observer.h" | 15 #include "extensions/browser/extension_host_observer.h" |
| 16 #include "extensions/browser/serial_extension_host_queue.h" | 16 #include "extensions/browser/serial_extension_host_queue.h" |
| 17 | 17 |
| 18 namespace extensions { | 18 namespace extensions { |
| 19 | 19 |
| 20 LoadMonitoringExtensionHostQueue::LoadMonitoringExtensionHostQueue( | 20 LoadMonitoringExtensionHostQueue::LoadMonitoringExtensionHostQueue( |
| 21 scoped_ptr<ExtensionHostQueue> delegate, | 21 scoped_ptr<ExtensionHostQueue> delegate, |
| 22 base::TimeDelta monitor_time, | 22 base::TimeDelta monitor_time, |
| 23 const FinishedCallback& finished_callback) | 23 const FinishedCallback& finished_callback) |
| 24 : delegate_(delegate.Pass()), | 24 : delegate_(delegate.Pass()), |
| 25 monitor_time_(monitor_time), | 25 monitor_time_(monitor_time), |
| 26 finished_callback_(finished_callback), | 26 finished_callback_(finished_callback), |
| 27 started_(false), | 27 started_(false), |
| 28 num_queued_(0u), | 28 num_queued_(0u), |
| 29 num_loaded_(0u), | 29 num_loaded_(0u), |
| 30 max_in_queue_(0u), | 30 max_awaiting_loading_(0u), |
| 31 max_active_loading_(0u), | 31 max_active_loading_(0u), |
| 32 weak_ptr_factory_(this) { | 32 weak_ptr_factory_(this) { |
| 33 } | 33 } |
| 34 | 34 |
| 35 LoadMonitoringExtensionHostQueue::LoadMonitoringExtensionHostQueue( | 35 LoadMonitoringExtensionHostQueue::LoadMonitoringExtensionHostQueue( |
| 36 scoped_ptr<ExtensionHostQueue> delegate) | 36 scoped_ptr<ExtensionHostQueue> delegate) |
| 37 : LoadMonitoringExtensionHostQueue(delegate.Pass(), | 37 : LoadMonitoringExtensionHostQueue(delegate.Pass(), |
| 38 base::TimeDelta::FromMinutes(1), | 38 base::TimeDelta::FromMinutes(1), |
| 39 FinishedCallback()) { | 39 FinishedCallback()) { |
| 40 } | 40 } |
| 41 | 41 |
| 42 LoadMonitoringExtensionHostQueue::~LoadMonitoringExtensionHostQueue() { | 42 LoadMonitoringExtensionHostQueue::~LoadMonitoringExtensionHostQueue() { |
| 43 } | 43 } |
| 44 | 44 |
| 45 void LoadMonitoringExtensionHostQueue::StartMonitoring() { | 45 void LoadMonitoringExtensionHostQueue::StartMonitoring() { |
| 46 if (started_) { | 46 if (started_) { |
| 47 return; | 47 return; |
| 48 } | 48 } |
| 49 started_ = true; | 49 started_ = true; |
| 50 base::MessageLoop::current()->PostDelayedTask( | 50 base::MessageLoop::current()->PostDelayedTask( |
| 51 FROM_HERE, base::Bind(&LoadMonitoringExtensionHostQueue::FinishMonitoring, | 51 FROM_HERE, base::Bind(&LoadMonitoringExtensionHostQueue::FinishMonitoring, |
| 52 weak_ptr_factory_.GetWeakPtr()), | 52 weak_ptr_factory_.GetWeakPtr()), |
| 53 monitor_time_); | 53 monitor_time_); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void LoadMonitoringExtensionHostQueue::Add(DeferredStartRenderHost* host) { | 56 void LoadMonitoringExtensionHostQueue::Add(DeferredStartRenderHost* host) { |
| 57 StartMonitoring(); | 57 StartMonitoring(); |
| 58 delegate_->Add(host); | 58 delegate_->Add(host); |
| 59 if (in_queue_.insert(host).second) { | 59 host->AddDeferredStartRenderHostObserver(this); |
| 60 if (awaiting_loading_.insert(host).second) { |
| 60 ++num_queued_; | 61 ++num_queued_; |
| 61 max_in_queue_ = std::max(max_in_queue_, in_queue_.size()); | 62 max_awaiting_loading_ = |
| 62 host->AddDeferredStartRenderHostObserver(this); | 63 std::max(max_awaiting_loading_, awaiting_loading_.size()); |
| 63 } | 64 } |
| 64 } | 65 } |
| 65 | 66 |
| 66 void LoadMonitoringExtensionHostQueue::Remove(DeferredStartRenderHost* host) { | 67 void LoadMonitoringExtensionHostQueue::Remove(DeferredStartRenderHost* host) { |
| 67 delegate_->Remove(host); | 68 delegate_->Remove(host); |
| 68 RemoveFromQueue(host); | 69 host->RemoveDeferredStartRenderHostObserver(this); |
| 69 } | 70 } |
| 70 | 71 |
| 71 void LoadMonitoringExtensionHostQueue::OnDeferredStartRenderHostDidStartLoading( | 72 void LoadMonitoringExtensionHostQueue:: |
| 72 const DeferredStartRenderHost* host) { | 73 OnDeferredStartRenderHostDidStartFirstLoad( |
| 74 const DeferredStartRenderHost* host) { |
| 73 StartMonitoringHost(host); | 75 StartMonitoringHost(host); |
| 74 } | 76 } |
| 75 | 77 |
| 76 void LoadMonitoringExtensionHostQueue::OnDeferredStartRenderHostDidStopLoading( | 78 void LoadMonitoringExtensionHostQueue:: |
| 77 const DeferredStartRenderHost* host) { | 79 OnDeferredStartRenderHostDidStopFirstLoad( |
| 80 const DeferredStartRenderHost* host) { |
| 78 FinishMonitoringHost(host); | 81 FinishMonitoringHost(host); |
| 79 } | 82 } |
| 80 | 83 |
| 81 void LoadMonitoringExtensionHostQueue::OnDeferredStartRenderHostDestroyed( | 84 void LoadMonitoringExtensionHostQueue::OnDeferredStartRenderHostDestroyed( |
| 82 const DeferredStartRenderHost* host) { | 85 const DeferredStartRenderHost* host) { |
| 83 FinishMonitoringHost(host); | 86 FinishMonitoringHost(host); |
| 84 } | 87 } |
| 85 | 88 |
| 86 void LoadMonitoringExtensionHostQueue::StartMonitoringHost( | 89 void LoadMonitoringExtensionHostQueue::StartMonitoringHost( |
| 87 const DeferredStartRenderHost* host) { | 90 const DeferredStartRenderHost* host) { |
| 91 awaiting_loading_.erase(host); |
| 88 if (active_loading_.insert(host).second) { | 92 if (active_loading_.insert(host).second) { |
| 89 max_active_loading_ = std::max(max_active_loading_, active_loading_.size()); | 93 max_active_loading_ = std::max(max_active_loading_, active_loading_.size()); |
| 90 } | 94 } |
| 91 RemoveFromQueue(host); | |
| 92 } | 95 } |
| 93 | 96 |
| 94 void LoadMonitoringExtensionHostQueue::FinishMonitoringHost( | 97 void LoadMonitoringExtensionHostQueue::FinishMonitoringHost( |
| 95 const DeferredStartRenderHost* host) { | 98 const DeferredStartRenderHost* host) { |
| 96 if (active_loading_.erase(host)) { | 99 if (active_loading_.erase(host)) { |
| 97 ++num_loaded_; | 100 ++num_loaded_; |
| 98 } | 101 } |
| 99 } | 102 } |
| 100 | 103 |
| 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! | |
| 110 break; | |
| 111 } | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void LoadMonitoringExtensionHostQueue::FinishMonitoring() { | 104 void LoadMonitoringExtensionHostQueue::FinishMonitoring() { |
| 116 CHECK(started_); | 105 CHECK(started_); |
| 117 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumQueued", | 106 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumQueued", |
| 118 num_queued_); | 107 num_queued_); |
| 119 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumLoaded", | 108 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.NumLoaded", |
| 120 num_loaded_); | 109 num_loaded_); |
| 121 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.MaxInQueue", | 110 UMA_HISTOGRAM_COUNTS_100("Extensions.ExtensionHostMonitoring.MaxInQueue", |
| 122 max_in_queue_); | 111 max_awaiting_loading_); |
| 123 UMA_HISTOGRAM_COUNTS_100( | 112 UMA_HISTOGRAM_COUNTS_100( |
| 124 "Extensions.ExtensionHostMonitoring.MaxActiveLoading", | 113 "Extensions.ExtensionHostMonitoring.MaxActiveLoading", |
| 125 max_active_loading_); | 114 max_active_loading_); |
| 126 if (!finished_callback_.is_null()) { | 115 if (!finished_callback_.is_null()) { |
| 127 finished_callback_.Run(num_queued_, num_loaded_, max_in_queue_, | 116 finished_callback_.Run(num_queued_, num_loaded_, max_awaiting_loading_, |
| 128 max_active_loading_); | 117 max_active_loading_); |
| 129 } | 118 } |
| 130 } | 119 } |
| 131 | 120 |
| 132 } // namespace extensions | 121 } // namespace extensions |
| OLD | NEW |