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