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 #include "extensions/browser/serial_extension_host_queue.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/time/time.h"
11 #include "components/variations/variations_associated_data.h"
12 #include "extensions/browser/deferred_start_render_host.h"
14 namespace extensions
{
18 // Gets the number of milliseconds to delay between loading ExtensionHosts. By
19 // default this is 0, but it can be overridden by field trials.
21 // A sanity check for the maximum delay, to guard against a bad field trial
22 // config being pushed that delays loading too much (e.g. using wrong units).
23 static const int kMaxDelayMs
= 30 * 1000;
24 static int delay_ms
= -1;
26 std::string delay_ms_param
=
27 variations::GetVariationParamValue("ExtensionSpeed", "SerialEHQDelay");
28 if (delay_ms_param
.empty()) {
30 } else if (!base::StringToInt(delay_ms_param
, &delay_ms
)) {
31 LOG(ERROR
) << "Could not parse SerialEHQDelay: " << delay_ms_param
;
33 } else if (delay_ms
< 0 || delay_ms
> kMaxDelayMs
) {
34 LOG(ERROR
) << "SerialEHQDelay out of range: " << delay_ms
;
43 SerialExtensionHostQueue::SerialExtensionHostQueue()
44 : pending_create_(false), ptr_factory_(this) {
47 SerialExtensionHostQueue::~SerialExtensionHostQueue() {
50 void SerialExtensionHostQueue::Add(DeferredStartRenderHost
* host
) {
51 queue_
.push_back(host
);
55 void SerialExtensionHostQueue::Remove(DeferredStartRenderHost
* host
) {
56 std::list
<DeferredStartRenderHost
*>::iterator it
=
57 std::find(queue_
.begin(), queue_
.end(), host
);
58 if (it
!= queue_
.end())
62 void SerialExtensionHostQueue::PostTask() {
63 if (!pending_create_
) {
64 base::MessageLoop::current()->PostDelayedTask(
65 FROM_HERE
, base::Bind(&SerialExtensionHostQueue::ProcessOneHost
,
66 ptr_factory_
.GetWeakPtr()),
67 base::TimeDelta::FromMilliseconds(GetDelayMs()));
68 pending_create_
= true;
72 void SerialExtensionHostQueue::ProcessOneHost() {
73 pending_create_
= false;
75 return; // can happen on shutdown
77 queue_
.front()->CreateRenderViewNow();
84 } // namespace extensions