| 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 #include "chrome/browser/media/router/event_page_request_manager.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/macros.h" |
| 12 #include "extensions/browser/event_page_tracker.h" |
| 13 #include "extensions/browser/process_manager.h" |
| 14 #include "extensions/browser/process_manager_factory.h" |
| 15 |
| 16 namespace media_router { |
| 17 |
| 18 EventPageRequestManager::~EventPageRequestManager() = default; |
| 19 |
| 20 void EventPageRequestManager::Shutdown() { |
| 21 event_page_tracker_ = nullptr; |
| 22 } |
| 23 |
| 24 void EventPageRequestManager::SetExtensionId(const std::string& extension_id) { |
| 25 media_route_provider_extension_id_ = extension_id; |
| 26 } |
| 27 |
| 28 void EventPageRequestManager::RunOrDefer( |
| 29 base::OnceClosure request, |
| 30 MediaRouteProviderWakeReason wake_reason) { |
| 31 if (mojo_connections_ready_) { |
| 32 DCHECK(!media_route_provider_extension_id_.empty()); |
| 33 std::move(request).Run(); |
| 34 } else { |
| 35 EnqueueRequest(std::move(request)); |
| 36 if (IsEventPageSuspended()) { |
| 37 SetWakeReason(wake_reason); |
| 38 AttemptWakeEventPage(); |
| 39 } |
| 40 } |
| 41 } |
| 42 |
| 43 void EventPageRequestManager::OnMojoConnectionsReady() { |
| 44 if (IsEventPageSuspended()) { |
| 45 DVLOG(1) |
| 46 << "OnMojoConnectionsReady was called while extension is suspended."; |
| 47 SetWakeReason(MediaRouteProviderWakeReason::REGISTER_MEDIA_ROUTE_PROVIDER); |
| 48 AttemptWakeEventPage(); |
| 49 return; |
| 50 } |
| 51 |
| 52 mojo_connections_ready_ = true; |
| 53 for (auto& next_request : pending_requests_) |
| 54 std::move(next_request).Run(); |
| 55 pending_requests_.clear(); |
| 56 wakeup_attempt_count_ = 0; |
| 57 } |
| 58 |
| 59 void EventPageRequestManager::OnMojoConnectionError() { |
| 60 mojo_connections_ready_ = false; |
| 61 |
| 62 // If this method is invoked while there are pending requests, then |
| 63 // it means we tried to wake the extension, but weren't able to complete the |
| 64 // connection to media route provider. Since we do not know whether the error |
| 65 // is transient, reattempt the wakeup. |
| 66 if (!pending_requests_.empty()) { |
| 67 DLOG(ERROR) << "A connection error while there are pending requests."; |
| 68 SetWakeReason(MediaRouteProviderWakeReason::CONNECTION_ERROR); |
| 69 AttemptWakeEventPage(); |
| 70 } |
| 71 } |
| 72 |
| 73 EventPageRequestManager::EventPageRequestManager( |
| 74 content::BrowserContext* context) |
| 75 : event_page_tracker_(extensions::ProcessManager::Get(context)), |
| 76 weak_factory_(this) {} |
| 77 |
| 78 void EventPageRequestManager::EnqueueRequest(base::OnceClosure request) { |
| 79 pending_requests_.push_back(std::move(request)); |
| 80 if (pending_requests_.size() > kMaxPendingRequests) { |
| 81 DLOG(ERROR) << "Reached max queue size. Dropping oldest request."; |
| 82 pending_requests_.pop_front(); |
| 83 } |
| 84 DVLOG(2) << "EnqueueRequest (queue-length=" << pending_requests_.size() |
| 85 << ")"; |
| 86 } |
| 87 |
| 88 void EventPageRequestManager::DrainPendingRequests() { |
| 89 DLOG(ERROR) << "Draining request queue. (queue-length=" |
| 90 << pending_requests_.size() << ")"; |
| 91 pending_requests_.clear(); |
| 92 } |
| 93 |
| 94 void EventPageRequestManager::SetWakeReason( |
| 95 MediaRouteProviderWakeReason reason) { |
| 96 DCHECK(reason != MediaRouteProviderWakeReason::TOTAL_COUNT); |
| 97 if (current_wake_reason_ == MediaRouteProviderWakeReason::TOTAL_COUNT) |
| 98 current_wake_reason_ = reason; |
| 99 } |
| 100 |
| 101 bool EventPageRequestManager::IsEventPageSuspended() const { |
| 102 return !event_page_tracker_ || event_page_tracker_->IsEventPageSuspended( |
| 103 media_route_provider_extension_id_); |
| 104 } |
| 105 |
| 106 void EventPageRequestManager::AttemptWakeEventPage() { |
| 107 ++wakeup_attempt_count_; |
| 108 if (wakeup_attempt_count_ > kMaxWakeupAttemptCount) { |
| 109 DLOG(ERROR) << "Attempted too many times to wake up event page."; |
| 110 DrainPendingRequests(); |
| 111 wakeup_attempt_count_ = 0; |
| 112 MediaRouterMojoMetrics::RecordMediaRouteProviderWakeup( |
| 113 MediaRouteProviderWakeup::ERROR_TOO_MANY_RETRIES); |
| 114 return; |
| 115 } |
| 116 |
| 117 DVLOG(1) << "Attempting to wake up event page: attempt " |
| 118 << wakeup_attempt_count_; |
| 119 if (!event_page_tracker_) { |
| 120 DLOG(ERROR) << "Attempted to wake up event page without a valid event page" |
| 121 "tracker"; |
| 122 return; |
| 123 } |
| 124 |
| 125 // This return false if the extension is already awake. |
| 126 // Callback is bound using WeakPtr because |event_page_tracker_| outlives |
| 127 // |this|. |
| 128 if (!event_page_tracker_->WakeEventPage( |
| 129 media_route_provider_extension_id_, |
| 130 base::Bind(&EventPageRequestManager::OnWakeComplete, |
| 131 weak_factory_.GetWeakPtr()))) { |
| 132 DLOG(ERROR) << "Failed to schedule a wakeup for event page."; |
| 133 } |
| 134 } |
| 135 |
| 136 void EventPageRequestManager::OnWakeComplete(bool success) { |
| 137 if (success) { |
| 138 MediaRouterMojoMetrics::RecordMediaRouteProviderWakeReason( |
| 139 current_wake_reason_); |
| 140 ClearWakeReason(); |
| 141 MediaRouterMojoMetrics::RecordMediaRouteProviderWakeup( |
| 142 MediaRouteProviderWakeup::SUCCESS); |
| 143 return; |
| 144 } |
| 145 |
| 146 // This is likely an non-retriable error. Drop the pending requests. |
| 147 DLOG(ERROR) << "An error encountered while waking the event page."; |
| 148 ClearWakeReason(); |
| 149 DrainPendingRequests(); |
| 150 MediaRouterMojoMetrics::RecordMediaRouteProviderWakeup( |
| 151 MediaRouteProviderWakeup::ERROR_UNKNOWN); |
| 152 } |
| 153 |
| 154 void EventPageRequestManager::ClearWakeReason() { |
| 155 DCHECK(current_wake_reason_ != MediaRouteProviderWakeReason::TOTAL_COUNT); |
| 156 current_wake_reason_ = MediaRouteProviderWakeReason::TOTAL_COUNT; |
| 157 } |
| 158 |
| 159 } // namespace media_router |
| OLD | NEW |