kalman | fcece45 | 2015-02-18 18:20:42 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef EXTENSIONS_BROWSER_EXTENSION_HOST_QUEUE_H_ |
| 6 | #define EXTENSIONS_BROWSER_EXTENSION_HOST_QUEUE_H_ |
| 7 | |
Devlin Cronin | cad04d7 | 2019-11-26 00:39:57 | [diff] [blame] | 8 | #include <list> |
kalman | fcece45 | 2015-02-18 18:20:42 | [diff] [blame] | 9 | |
Devlin Cronin | cad04d7 | 2019-11-26 00:39:57 | [diff] [blame] | 10 | #include "base/memory/weak_ptr.h" |
Devlin Cronin | ae071f7 | 2020-03-07 00:21:35 | [diff] [blame] | 11 | #include "base/time/time.h" |
Devlin Cronin | cad04d7 | 2019-11-26 00:39:57 | [diff] [blame] | 12 | |
| 13 | namespace extensions { |
yoz | d61dfe19 | 2015-02-21 01:30:37 | [diff] [blame] | 14 | class DeferredStartRenderHost; |
kalman | fcece45 | 2015-02-18 18:20:42 | [diff] [blame] | 15 | |
Devlin Cronin | cad04d7 | 2019-11-26 00:39:57 | [diff] [blame] | 16 | // 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. |
kalman | fcece45 | 2015-02-18 18:20:42 | [diff] [blame] | 20 | class ExtensionHostQueue { |
| 21 | public: |
Devlin Cronin | cad04d7 | 2019-11-26 00:39:57 | [diff] [blame] | 22 | ExtensionHostQueue(); |
| 23 | ~ExtensionHostQueue(); |
| 24 | |
| 25 | ExtensionHostQueue(const ExtensionHostQueue& queue) = delete; |
| 26 | ExtensionHostQueue& operator=(const ExtensionHostQueue& queue) = delete; |
kalman | fcece45 | 2015-02-18 18:20:42 | [diff] [blame] | 27 | |
Devlin Cronin | 94389d2 | 2019-11-26 23:15:28 | [diff] [blame] | 28 | // Returns the single global instance of the ExtensionHostQueue. |
| 29 | static ExtensionHostQueue& GetInstance(); |
| 30 | |
kalman | fcece45 | 2015-02-18 18:20:42 | [diff] [blame] | 31 | // Adds a host to the queue for RenderView creation. |
Devlin Cronin | cad04d7 | 2019-11-26 00:39:57 | [diff] [blame] | 32 | void Add(DeferredStartRenderHost* host); |
kalman | fcece45 | 2015-02-18 18:20:42 | [diff] [blame] | 33 | |
| 34 | // Removes a host from the queue (for example, it may be deleted before |
Devlin Cronin | cad04d7 | 2019-11-26 00:39:57 | [diff] [blame] | 35 | // having a chance to start) |
| 36 | void Remove(DeferredStartRenderHost* host); |
| 37 | |
Devlin Cronin | ae071f7 | 2020-03-07 00:21:35 | [diff] [blame] | 38 | // 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 Cronin | cad04d7 | 2019-11-26 00:39:57 | [diff] [blame] | 42 | 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 Cronin | ae071f7 | 2020-03-07 00:21:35 | [diff] [blame] | 54 | // 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 Cronin | cad04d7 | 2019-11-26 00:39:57 | [diff] [blame] | 58 | // The list of DeferredStartRenderHosts waiting to be started. |
| 59 | std::list<DeferredStartRenderHost*> queue_; |
| 60 | |
| 61 | base::WeakPtrFactory<ExtensionHostQueue> ptr_factory_{this}; |
kalman | fcece45 | 2015-02-18 18:20:42 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | } // namespace extensions |
| 65 | |
| 66 | #endif // EXTENSIONS_BROWSER_EXTENSION_HOST_QUEUE_H_ |