blob: 358e8dd9d755cec0b3c84383264da37d37335753 [file] [log] [blame]
kalmanfcece452015-02-18 18:20:421// 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.
4
5#ifndef EXTENSIONS_BROWSER_EXTENSION_HOST_QUEUE_H_
6#define EXTENSIONS_BROWSER_EXTENSION_HOST_QUEUE_H_
7
Devlin Cronincad04d72019-11-26 00:39:578#include <list>
kalmanfcece452015-02-18 18:20:429
Devlin Cronincad04d72019-11-26 00:39:5710#include "base/memory/weak_ptr.h"
Devlin Croninae071f72020-03-07 00:21:3511#include "base/time/time.h"
Devlin Cronincad04d72019-11-26 00:39:5712
13namespace extensions {
yozd61dfe192015-02-21 01:30:3714class DeferredStartRenderHost;
kalmanfcece452015-02-18 18:20:4215
Devlin Cronincad04d72019-11-26 00:39:5716// A queue of ExtensionHosts waiting for initialization. This initializes
17// DeferredStartRenderHosts in the order they're Add()ed, with simple rate
18// limiting logic that re-posts each task to the UI thread, to avoid clogging it
19// for a long period of time.
kalmanfcece452015-02-18 18:20:4220class ExtensionHostQueue {
21 public:
Devlin Cronincad04d72019-11-26 00:39:5722 ExtensionHostQueue();
23 ~ExtensionHostQueue();
24
25 ExtensionHostQueue(const ExtensionHostQueue& queue) = delete;
26 ExtensionHostQueue& operator=(const ExtensionHostQueue& queue) = delete;
kalmanfcece452015-02-18 18:20:4227
Devlin Cronin94389d22019-11-26 23:15:2828 // Returns the single global instance of the ExtensionHostQueue.
29 static ExtensionHostQueue& GetInstance();
30
kalmanfcece452015-02-18 18:20:4231 // Adds a host to the queue for RenderView creation.
Devlin Cronincad04d72019-11-26 00:39:5732 void Add(DeferredStartRenderHost* host);
kalmanfcece452015-02-18 18:20:4233
34 // Removes a host from the queue (for example, it may be deleted before
Devlin Cronincad04d72019-11-26 00:39:5735 // having a chance to start)
36 void Remove(DeferredStartRenderHost* host);
37
Devlin Croninae071f72020-03-07 00:21:3538 // Adds a delay before starting the next ExtensionHost. This can be used for
39 // testing purposes to help flush out flakes.
40 void SetCustomDelayForTesting(base::TimeDelta delay) { delay_ = delay; }
41
Devlin Cronincad04d72019-11-26 00:39:5742 private:
43 // Queues up a delayed task to process the next DeferredStartRenderHost in
44 // the queue.
45 void PostTask();
46
47 // Creates the RenderView for the next host in the queue.
48 void ProcessOneHost();
49
50 // True if this queue is currently in the process of starting an
51 // DeferredStartRenderHost.
52 bool pending_create_;
53
Devlin Croninae071f72020-03-07 00:21:3554 // The delay before starting the next host. By default, this is 0, meaning we
55 // just wait until the event loop yields.
56 base::TimeDelta delay_;
57
Devlin Cronincad04d72019-11-26 00:39:5758 // The list of DeferredStartRenderHosts waiting to be started.
59 std::list<DeferredStartRenderHost*> queue_;
60
61 base::WeakPtrFactory<ExtensionHostQueue> ptr_factory_{this};
kalmanfcece452015-02-18 18:20:4262};
63
64} // namespace extensions
65
66#endif // EXTENSIONS_BROWSER_EXTENSION_HOST_QUEUE_H_