Chromium Code Reviews| Index: chrome/browser/media/router/event_page_request_manager.h |
| diff --git a/chrome/browser/media/router/event_page_request_manager.h b/chrome/browser/media/router/event_page_request_manager.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f12f55023051bf5c47a910d055a9f1c237391ebb |
| --- /dev/null |
| +++ b/chrome/browser/media/router/event_page_request_manager.h |
| @@ -0,0 +1,130 @@ |
| +// Copyright 2017 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. |
| + |
| +#ifndef CHROME_BROWSER_MEDIA_ROUTER_EVENT_PAGE_REQUEST_MANAGER_H_ |
| +#define CHROME_BROWSER_MEDIA_ROUTER_EVENT_PAGE_REQUEST_MANAGER_H_ |
| + |
| +#include <deque> |
| +#include <string> |
| + |
| +#include "base/gtest_prod_util.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "components/keyed_service/core/keyed_service.h" |
| + |
| +namespace content { |
| +class BrowserContext; |
| +} |
| + |
| +namespace extensions { |
| +class EventPageTracker; |
| +} |
| + |
| +namespace media_router { |
| + |
| +enum class MediaRouteProviderWakeReason; |
| + |
| +// A class that is responsible for running closures that are requests to the |
| +// Media Router component extension while the extension is awake, or queueing |
| +// requests and waking the extension up if it's suspended. |
| +class EventPageRequestManager : public KeyedService { |
| + public: |
| + ~EventPageRequestManager() override; |
| + |
| + void Shutdown() override; |
| + |
| + // Sets the reason why we are attempting to wake the extension. Since |
| + // multiple tasks may be enqueued for execution each time the extension runs, |
| + // we record the first such reason. |
| + virtual void SetWakeReason(MediaRouteProviderWakeReason reason); |
| + |
| + // Sets the ID of the component extension. |
| + virtual void SetExtensionId(const std::string& extension_id); |
| + |
| + // Runs a closure if the Mojo connections to the extensions are valid, or |
| + // defers the execution until the connections have been established. |
| + virtual void RunOrDefer(base::OnceClosure request); |
| + |
| + // Executes the Mojo requests queued in |pending_requests_|. |
| + virtual void OnMojoConnectionsReady(); |
| + |
| + // Attempts to wake the component extension again if there are pending |
| + // requests. Otherwise the extension will be woken up the next time a request |
| + // is received. |
| + virtual void OnMojoConnectionError(); |
| + |
| + const std::string& media_route_provider_extension_id() const { |
| + return media_route_provider_extension_id_; |
| + } |
| + |
| + protected: |
| + explicit EventPageRequestManager(content::BrowserContext* context); |
| + |
| + private: |
| + friend class EventPageRequestManagerFactory; |
| + friend class EventPageRequestManagerTest; |
| + FRIEND_TEST_ALL_PREFIXES(EventPageRequestManagerTest, |
| + DropOldestPendingRequest); |
| + FRIEND_TEST_ALL_PREFIXES(EventPageRequestManagerTest, |
| + TooManyWakeupAttemptsDrainsQueue); |
| + |
| + // Max consecutive attempts to wake up the component extension before |
| + // giving up and draining the pending request queue. |
| + static const int kMaxWakeupAttemptCount = 3; |
| + |
| + // The max number of pending requests allowed. When number of pending requests |
| + // exceeds this number, the oldest request will be dropped. |
| + static const int kMaxPendingRequests = 30; |
| + |
| + // Enqueues a closure to be executed when the Mojo connections to the |
| + // component extension are ready. |
| + void EnqueueRequest(base::OnceClosure closure); |
| + |
| + // Drops all pending requests. Called when we have a connection error to |
| + // component extension and further reattempts are unlikely to help. |
| + void DrainPendingRequests(); |
| + |
| + // Whether the component extension event page is suspended. |
| + bool IsEventPageSuspended(); |
|
imcheng
2017/06/30 00:17:38
const?
takumif
2017/07/01 01:16:44
Done.
|
| + |
| + // Calls to |event_page_tracker_| to wake the component extension. |
| + // |media_route_provider_extension_id_| must not be empty and the extension |
| + // should be currently suspended. If there have already been too many wakeup |
| + // attempts, give up and drain the pending request queue. |
| + void AttemptWakeEventPage(); |
| + |
| + // Callback invoked by |event_page_tracker_| after an attempt to wake the |
| + // component extension. If |success| is false, the pending request queue is |
| + // drained. |
| + void OnWakeComplete(bool success); |
| + |
| + // Clears the wake reason after the extension has been awoken. |
| + void ClearWakeReason(); |
| + |
| + // Id of the component extension. Used for managing its suspend/wake state |
|
imcheng
2017/06/30 00:17:38
s/Id/ID
takumif
2017/07/01 01:16:44
Done.
|
| + // via |event_page_tracker_|. |
| + std::string media_route_provider_extension_id_; |
| + |
| + // Pending requests queued to be executed once component extension |
| + // becomes ready. |
| + std::deque<base::OnceClosure> pending_requests_; |
| + |
| + // Allows the extension to be monitored for suspend, and woken. |
| + // This is a reference to a BrowserContext keyed service that outlives this |
| + // instance. |
| + extensions::EventPageTracker* event_page_tracker_; |
| + |
| + int wakeup_attempt_count_ = 0; |
| + |
| + bool mojo_connections_ready_ = false; |
| + |
| + // Records the current reason the extension is being woken up. Is set to |
| + // MediaRouteProviderWakeReason::TOTAL_COUNT if there is no pending reason. |
| + MediaRouteProviderWakeReason current_wake_reason_; |
|
imcheng
2017/06/30 00:17:38
nit: set to MediaRouteProviderWakeReason::TOTAL_CO
takumif
2017/07/01 01:16:44
Done.
|
| + |
| + base::WeakPtrFactory<EventPageRequestManager> weak_factory_; |
| +}; |
| + |
| +} // namespace media_router |
| + |
| +#endif // CHROME_BROWSER_MEDIA_ROUTER_EVENT_PAGE_REQUEST_MANAGER_H_ |