extension_host_queue.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #ifndef EXTENSIONS_BROWSER_EXTENSION_HOST_QUEUE_H_
  5. #define EXTENSIONS_BROWSER_EXTENSION_HOST_QUEUE_H_
  6. #include <list>
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/time/time.h"
  9. namespace extensions {
  10. class DeferredStartRenderHost;
  11. // A queue of ExtensionHosts waiting for initialization. This initializes
  12. // DeferredStartRenderHosts in the order they're Add()ed, with simple rate
  13. // limiting logic that re-posts each task to the UI thread, to avoid clogging it
  14. // for a long period of time.
  15. class ExtensionHostQueue {
  16. public:
  17. ExtensionHostQueue();
  18. ~ExtensionHostQueue();
  19. ExtensionHostQueue(const ExtensionHostQueue& queue) = delete;
  20. ExtensionHostQueue& operator=(const ExtensionHostQueue& queue) = delete;
  21. // Returns the single global instance of the ExtensionHostQueue.
  22. static ExtensionHostQueue& GetInstance();
  23. // Adds a host to the queue for RenderView creation.
  24. void Add(DeferredStartRenderHost* host);
  25. // Removes a host from the queue (for example, it may be deleted before
  26. // having a chance to start)
  27. void Remove(DeferredStartRenderHost* host);
  28. // Adds a delay before starting the next ExtensionHost. This can be used for
  29. // testing purposes to help flush out flakes.
  30. void SetCustomDelayForTesting(base::TimeDelta delay) { delay_ = delay; }
  31. private:
  32. // Queues up a delayed task to process the next DeferredStartRenderHost in
  33. // the queue.
  34. void PostTask();
  35. // Creates the RenderView for the next host in the queue.
  36. void ProcessOneHost();
  37. // True if this queue is currently in the process of starting an
  38. // DeferredStartRenderHost.
  39. bool pending_create_;
  40. // The delay before starting the next host. By default, this is 0, meaning we
  41. // just wait until the event loop yields.
  42. base::TimeDelta delay_;
  43. // The list of DeferredStartRenderHosts waiting to be started.
  44. std::list<DeferredStartRenderHost*> queue_;
  45. base::WeakPtrFactory<ExtensionHostQueue> ptr_factory_{this};
  46. };
  47. } // namespace extensions
  48. #endif // EXTENSIONS_BROWSER_EXTENSION_HOST_QUEUE_H_