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

Unified Diff: content/browser/loader/wake_lock_resource_throttle.cc

Issue 2893873002: Convert PowerSaveBlockResourceThrottle to be client of WakeLock mojo service. (Closed)
Patch Set: Created 3 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/loader/wake_lock_resource_throttle.cc
diff --git a/content/browser/loader/wake_lock_resource_throttle.cc b/content/browser/loader/wake_lock_resource_throttle.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1fd3cafae250d914b4013e78a4bcdcd43ba75bd9
--- /dev/null
+++ b/content/browser/loader/wake_lock_resource_throttle.cc
@@ -0,0 +1,69 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
Charlie Harrison 2017/05/18 13:56:42 Copyright 2017
ke.he 2017/05/18 14:13:11 Actually this file is renamed from the original po
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/loader/wake_lock_resource_throttle.h"
+
+#include "content/browser/service_manager/service_manager_context.h"
+#include "content/public/browser/browser_thread.h"
+#include "device/wake_lock/public/interfaces/wake_lock_provider.mojom.h"
+#include "services/device/public/interfaces/constants.mojom.h"
+#include "services/service_manager/public/cpp/connector.h"
+
+namespace content {
+
+namespace {
+
+const int kWakeLockDelaySeconds = 30;
+
+} // namespace
+
+WakeLockResourceThrottle::WakeLockResourceThrottle(const std::string& host)
+ : host_(host) {}
+
+WakeLockResourceThrottle::~WakeLockResourceThrottle() {}
+
+void WakeLockResourceThrottle::WillStartRequest(bool* defer) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ // Delay wake lock activation to dismiss small requests.
+ timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kWakeLockDelaySeconds),
+ this, &WakeLockResourceThrottle::ActivateWakeLock);
+}
+
+void WakeLockResourceThrottle::WillProcessResponse(bool* defer) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ // Cancel wake lock after request finishes.
+ if (wake_lock_)
+ wake_lock_->CancelWakeLock();
+
+ timer_.Stop();
+}
+
+const char* WakeLockResourceThrottle::GetNameForLogging() const {
+ return "WakeLockResourceThrottle";
+}
+
+void WakeLockResourceThrottle::ActivateWakeLock() {
Charlie Harrison 2017/05/18 14:46:17 Maybe s/Activate/Request ?
ke.he 2017/05/18 15:35:15 Done.
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
ke.he 2017/05/18 13:40:09 I think the original PowerSaveBlockResourceThrottl
Charlie Harrison 2017/05/18 13:56:42 Yep, all ResourceThrottles run on the IO thread.
ke.he 2017/05/18 14:13:11 Thanks!
+ DCHECK(!wake_lock_);
ke.he 2017/05/18 13:40:09 This ActivateWakeLock() will never be called twice
Charlie Harrison 2017/05/18 13:56:42 Yep, WillStartRequest should only be called once.
+
+ service_manager::Connector* connector =
+ ServiceManagerContext::GetConnectorForIOThread();
+ // |connector| might be nullptr in some testing contexts, in which the
+ // service manager connection isn't initialized.
+ if (connector) {
+ device::mojom::WakeLockProviderPtr wake_lock_provider;
+ connector->BindInterface(device::mojom::kServiceName,
+ mojo::MakeRequest(&wake_lock_provider));
Charlie Harrison 2017/05/18 14:46:17 Do we need to include mojo/public/cpp/bindings/int
ke.he 2017/05/18 15:35:15 Done.
+ wake_lock_provider->GetWakeLockWithoutContext(
+ device::mojom::WakeLockType::PreventAppSuspension,
+ device::mojom::WakeLockReason::ReasonOther,
+ "Uploading data to " + host_, mojo::MakeRequest(&wake_lock_));
+
+ wake_lock_->RequestWakeLock();
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698