Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(561)

Side by Side Diff: chrome/browser/media/router/event_page_request_manager.h

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

Powered by Google App Engine
This is Rietveld 408576698