Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / extensions / browser / serial_extension_host_queue.cc
blobc305c93240fd5b9e37b3b1bd8d488a1d12be751f
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"
7 #include "base/bind.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 {
16 namespace {
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.
20 int GetDelayMs() {
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;
25 if (delay_ms == -1) {
26 std::string delay_ms_param =
27 variations::GetVariationParamValue("ExtensionSpeed", "SerialEHQDelay");
28 if (delay_ms_param.empty()) {
29 delay_ms = 0;
30 } else if (!base::StringToInt(delay_ms_param, &delay_ms)) {
31 LOG(ERROR) << "Could not parse SerialEHQDelay: " << delay_ms_param;
32 delay_ms = 0;
33 } else if (delay_ms < 0 || delay_ms > kMaxDelayMs) {
34 LOG(ERROR) << "SerialEHQDelay out of range: " << delay_ms;
35 delay_ms = 0;
38 return delay_ms;
41 } // namespace
43 SerialExtensionHostQueue::SerialExtensionHostQueue()
44 : pending_create_(false), ptr_factory_(this) {
47 SerialExtensionHostQueue::~SerialExtensionHostQueue() {
50 void SerialExtensionHostQueue::Add(DeferredStartRenderHost* host) {
51 queue_.push_back(host);
52 PostTask();
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())
59 queue_.erase(it);
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;
74 if (queue_.empty())
75 return; // can happen on shutdown
77 queue_.front()->CreateRenderViewNow();
78 queue_.pop_front();
80 if (!queue_.empty())
81 PostTask();
84 } // namespace extensions