Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 CHROME_BROWSER_MEDIA_ROUTER_EVENT_PAGE_REQUEST_MANAGER_H_ | |
| 6 #define CHROME_BROWSER_MEDIA_ROUTER_EVENT_PAGE_REQUEST_MANAGER_H_ | |
| 7 | |
| 8 #include <deque> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/gtest_prod_util.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "components/keyed_service/core/keyed_service.h" | |
| 14 | |
| 15 namespace content { | |
| 16 class BrowserContext; | |
| 17 } | |
| 18 | |
| 19 namespace extensions { | |
| 20 class EventPageTracker; | |
| 21 } | |
| 22 | |
| 23 namespace media_router { | |
| 24 | |
| 25 enum class MediaRouteProviderWakeReason; | |
| 26 | |
| 27 // A class that is responsible for running closures that are requests to the | |
| 28 // Media Router component extension while the extension is awake, or queueing | |
| 29 // requests and waking the extension up if it's suspended. | |
| 30 class EventPageRequestManager : public KeyedService { | |
| 31 public: | |
| 32 ~EventPageRequestManager() override; | |
| 33 | |
| 34 void Shutdown() override; | |
| 35 | |
| 36 // Sets the reason why we are attempting to wake the extension. Since | |
| 37 // multiple tasks may be enqueued for execution each time the extension runs, | |
| 38 // we record the first such reason. | |
| 39 virtual void SetWakeReason(MediaRouteProviderWakeReason reason); | |
| 40 | |
| 41 // Sets the ID of the component extension. | |
| 42 virtual void SetExtensionId(const std::string& extension_id); | |
| 43 | |
| 44 // Runs a closure if the Mojo connections to the extensions are valid, or | |
| 45 // defers the execution until the connections have been established. | |
| 46 virtual void RunOrDefer(base::OnceClosure request); | |
| 47 | |
| 48 // Executes the Mojo requests queued in |pending_requests_|. | |
| 49 virtual void OnMojoConnectionsReady(); | |
| 50 | |
| 51 // Attempts to wake the component extension again if there are pending | |
| 52 // requests. Otherwise the extension will be woken up the next time a request | |
| 53 // is received. | |
| 54 virtual void OnMojoConnectionError(); | |
| 55 | |
| 56 const std::string& media_route_provider_extension_id() const { | |
| 57 return media_route_provider_extension_id_; | |
| 58 } | |
| 59 | |
| 60 protected: | |
| 61 explicit EventPageRequestManager(content::BrowserContext* context); | |
| 62 | |
| 63 private: | |
| 64 friend class EventPageRequestManagerFactory; | |
| 65 friend class EventPageRequestManagerTest; | |
| 66 FRIEND_TEST_ALL_PREFIXES(EventPageRequestManagerTest, | |
| 67 DropOldestPendingRequest); | |
| 68 FRIEND_TEST_ALL_PREFIXES(EventPageRequestManagerTest, | |
| 69 TooManyWakeupAttemptsDrainsQueue); | |
| 70 | |
| 71 // Max consecutive attempts to wake up the component extension before | |
| 72 // giving up and draining the pending request queue. | |
| 73 static const int kMaxWakeupAttemptCount = 3; | |
| 74 | |
| 75 // The max number of pending requests allowed. When number of pending requests | |
| 76 // exceeds this number, the oldest request will be dropped. | |
| 77 static const int kMaxPendingRequests = 30; | |
| 78 | |
| 79 // Enqueues a closure to be executed when the Mojo connections to the | |
| 80 // component extension are ready. | |
| 81 void EnqueueRequest(base::OnceClosure closure); | |
| 82 | |
| 83 // Drops all pending requests. Called when we have a connection error to | |
| 84 // component extension and further reattempts are unlikely to help. | |
| 85 void DrainPendingRequests(); | |
| 86 | |
| 87 // Whether the component extension event page is suspended. | |
| 88 bool IsEventPageSuspended(); | |
|
imcheng
2017/06/30 00:17:38
const?
takumif
2017/07/01 01:16:44
Done.
| |
| 89 | |
| 90 // Calls to |event_page_tracker_| to wake the component extension. | |
| 91 // |media_route_provider_extension_id_| must not be empty and the extension | |
| 92 // should be currently suspended. If there have already been too many wakeup | |
| 93 // attempts, give up and drain the pending request queue. | |
| 94 void AttemptWakeEventPage(); | |
| 95 | |
| 96 // Callback invoked by |event_page_tracker_| after an attempt to wake the | |
| 97 // component extension. If |success| is false, the pending request queue is | |
| 98 // drained. | |
| 99 void OnWakeComplete(bool success); | |
| 100 | |
| 101 // Clears the wake reason after the extension has been awoken. | |
| 102 void ClearWakeReason(); | |
| 103 | |
| 104 // 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.
| |
| 105 // via |event_page_tracker_|. | |
| 106 std::string media_route_provider_extension_id_; | |
| 107 | |
| 108 // Pending requests queued to be executed once component extension | |
| 109 // becomes ready. | |
| 110 std::deque<base::OnceClosure> pending_requests_; | |
| 111 | |
| 112 // Allows the extension to be monitored for suspend, and woken. | |
| 113 // This is a reference to a BrowserContext keyed service that outlives this | |
| 114 // instance. | |
| 115 extensions::EventPageTracker* event_page_tracker_; | |
| 116 | |
| 117 int wakeup_attempt_count_ = 0; | |
| 118 | |
| 119 bool mojo_connections_ready_ = false; | |
| 120 | |
| 121 // Records the current reason the extension is being woken up. Is set to | |
| 122 // MediaRouteProviderWakeReason::TOTAL_COUNT if there is no pending reason. | |
| 123 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.
| |
| 124 | |
| 125 base::WeakPtrFactory<EventPageRequestManager> weak_factory_; | |
| 126 }; | |
| 127 | |
| 128 } // namespace media_router | |
| 129 | |
| 130 #endif // CHROME_BROWSER_MEDIA_ROUTER_EVENT_PAGE_REQUEST_MANAGER_H_ | |
| OLD | NEW |