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 |