summaryrefslogtreecommitdiffstats
path: root/extensions/browser/serial_extension_host_queue.cc
blob: 7986523c69d6d76a806d5b1cecc3738d7039ceeb (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
// 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/serial_extension_host_queue.h"

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "extensions/browser/deferred_start_render_host.h"

namespace extensions {

SerialExtensionHostQueue::SerialExtensionHostQueue()
    : pending_create_(false), ptr_factory_(this) {
}

SerialExtensionHostQueue::~SerialExtensionHostQueue() {
}

void SerialExtensionHostQueue::Add(DeferredStartRenderHost* host) {
  queue_.push_back(host);
  PostTask();
}

void SerialExtensionHostQueue::Remove(DeferredStartRenderHost* host) {
  std::list<DeferredStartRenderHost*>::iterator it =
      std::find(queue_.begin(), queue_.end(), host);
  if (it != queue_.end())
    queue_.erase(it);
}

void SerialExtensionHostQueue::PostTask() {
  if (!pending_create_) {
    base::MessageLoop::current()->PostTask(
        FROM_HERE, base::Bind(&SerialExtensionHostQueue::ProcessOneHost,
                              ptr_factory_.GetWeakPtr()));
    pending_create_ = true;
  }
}

void SerialExtensionHostQueue::ProcessOneHost() {
  pending_create_ = false;
  if (queue_.empty())
    return;  // can happen on shutdown

  queue_.front()->CreateRenderViewNow();
  queue_.pop_front();

  if (!queue_.empty())
    PostTask();
}

}  // namespace extensions