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

Side by Side Diff: content/browser/power_save_blocker_android.cc

Issue 2012853003: Reland of Pass SequencedTaskRunner to PowerSaveBlocker for ui/file ops (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: SingleThreadTaskRunner for _x11 Created 4 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/android/jni_android.h" 5 #include "base/android/jni_android.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "content/browser/android/content_view_core_impl.h" 8 #include "content/browser/android/content_view_core_impl.h"
9 #include "content/browser/power_save_blocker_impl.h" 9 #include "content/browser/power_save_blocker_impl.h"
10 #include "content/browser/web_contents/web_contents_impl.h" 10 #include "content/browser/web_contents/web_contents_impl.h"
11 #include "content/public/browser/android/content_view_core.h" 11 #include "content/public/browser/android/content_view_core.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/web_contents_observer.h" 12 #include "content/public/browser/web_contents_observer.h"
14 #include "jni/PowerSaveBlocker_jni.h" 13 #include "jni/PowerSaveBlocker_jni.h"
15 #include "ui/android/view_android.h" 14 #include "ui/android/view_android.h"
16 15
17 namespace content { 16 namespace content {
18 17
19 using base::android::AttachCurrentThread; 18 using base::android::AttachCurrentThread;
20 19
21 class PowerSaveBlockerImpl::Delegate 20 class PowerSaveBlockerImpl::Delegate
22 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate>, 21 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate>,
23 public WebContentsObserver { 22 public WebContentsObserver {
24 public: 23 public:
25 explicit Delegate(WebContents* web_contents); 24 Delegate(WebContents* web_contents,
25 scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
26 26
27 // Does the actual work to apply or remove the desired power save block. 27 // Does the actual work to apply or remove the desired power save block.
28 void ApplyBlock(); 28 void ApplyBlock();
29 void RemoveBlock(); 29 void RemoveBlock();
30 30
31 private: 31 private:
32 friend class base::RefCountedThreadSafe<Delegate>; 32 friend class base::RefCountedThreadSafe<Delegate>;
33 ~Delegate() override; 33 ~Delegate() override;
34 34
35 base::android::ScopedJavaLocalRef<jobject> GetContentViewCore(); 35 base::android::ScopedJavaLocalRef<jobject> GetContentViewCore();
36 36
37 base::android::ScopedJavaGlobalRef<jobject> java_power_save_blocker_; 37 base::android::ScopedJavaGlobalRef<jobject> java_power_save_blocker_;
38 38
39 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
40
39 DISALLOW_COPY_AND_ASSIGN(Delegate); 41 DISALLOW_COPY_AND_ASSIGN(Delegate);
40 }; 42 };
41 43
42 PowerSaveBlockerImpl::Delegate::Delegate(WebContents* web_contents) 44 PowerSaveBlockerImpl::Delegate::Delegate(
43 : WebContentsObserver(web_contents) { 45 WebContents* web_contents,
46 scoped_refptr<base::SequencedTaskRunner> ui_task_runner)
47 : WebContentsObserver(web_contents), ui_task_runner_(ui_task_runner) {
44 JNIEnv* env = AttachCurrentThread(); 48 JNIEnv* env = AttachCurrentThread();
45 java_power_save_blocker_.Reset(Java_PowerSaveBlocker_create(env)); 49 java_power_save_blocker_.Reset(Java_PowerSaveBlocker_create(env));
46 } 50 }
47 51
48 PowerSaveBlockerImpl::Delegate::~Delegate() { 52 PowerSaveBlockerImpl::Delegate::~Delegate() {
49 } 53 }
50 54
51 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { 55 void PowerSaveBlockerImpl::Delegate::ApplyBlock() {
52 DCHECK_CURRENTLY_ON(BrowserThread::UI); 56 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
53 base::android::ScopedJavaLocalRef<jobject> java_content_view_core = 57 base::android::ScopedJavaLocalRef<jobject> java_content_view_core =
54 GetContentViewCore(); 58 GetContentViewCore();
55 if (java_content_view_core.is_null()) 59 if (java_content_view_core.is_null())
56 return; 60 return;
57 61
58 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_); 62 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_);
59 JNIEnv* env = AttachCurrentThread(); 63 JNIEnv* env = AttachCurrentThread();
60 Java_PowerSaveBlocker_applyBlock(env, obj.obj(), 64 Java_PowerSaveBlocker_applyBlock(env, obj.obj(),
61 java_content_view_core.obj()); 65 java_content_view_core.obj());
62 } 66 }
63 67
64 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { 68 void PowerSaveBlockerImpl::Delegate::RemoveBlock() {
65 DCHECK_CURRENTLY_ON(BrowserThread::UI); 69 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
66 base::android::ScopedJavaLocalRef<jobject> java_content_view_core = 70 base::android::ScopedJavaLocalRef<jobject> java_content_view_core =
67 GetContentViewCore(); 71 GetContentViewCore();
68 if (java_content_view_core.is_null()) 72 if (java_content_view_core.is_null())
69 return; 73 return;
70 74
71 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_); 75 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_);
72 JNIEnv* env = AttachCurrentThread(); 76 JNIEnv* env = AttachCurrentThread();
73 Java_PowerSaveBlocker_removeBlock(env, obj.obj(), 77 Java_PowerSaveBlocker_removeBlock(env, obj.obj(),
74 java_content_view_core.obj()); 78 java_content_view_core.obj());
75 } 79 }
76 80
77 base::android::ScopedJavaLocalRef<jobject> 81 base::android::ScopedJavaLocalRef<jobject>
78 PowerSaveBlockerImpl::Delegate::GetContentViewCore() { 82 PowerSaveBlockerImpl::Delegate::GetContentViewCore() {
79 if (!web_contents()) 83 if (!web_contents())
80 return base::android::ScopedJavaLocalRef<jobject>(); 84 return base::android::ScopedJavaLocalRef<jobject>();
81 85
82 ContentViewCoreImpl* content_view_core_impl = 86 ContentViewCoreImpl* content_view_core_impl =
83 ContentViewCoreImpl::FromWebContents(web_contents()); 87 ContentViewCoreImpl::FromWebContents(web_contents());
84 if (!content_view_core_impl) 88 if (!content_view_core_impl)
85 return base::android::ScopedJavaLocalRef<jobject>(); 89 return base::android::ScopedJavaLocalRef<jobject>();
86 90
87 return content_view_core_impl->GetJavaObject(); 91 return content_view_core_impl->GetJavaObject();
88 } 92 }
89 93
90 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, 94 PowerSaveBlockerImpl::PowerSaveBlockerImpl(
91 Reason reason, 95 PowerSaveBlockerType type,
92 const std::string& description) { 96 Reason reason,
97 const std::string& description,
98 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
99 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner)
100 : ui_task_runner_(ui_task_runner),
101 blocking_task_runner_(blocking_task_runner) {
93 // Don't support kPowerSaveBlockPreventAppSuspension 102 // Don't support kPowerSaveBlockPreventAppSuspension
94 } 103 }
95 104
96 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { 105 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
97 if (delegate_.get()) { 106 if (delegate_.get()) {
98 BrowserThread::PostTask( 107 ui_task_runner_->PostTask(FROM_HERE,
99 BrowserThread::UI, FROM_HERE, 108 base::Bind(&Delegate::RemoveBlock, delegate_));
100 base::Bind(&Delegate::RemoveBlock, delegate_));
101 } 109 }
102 } 110 }
103 111
104 void PowerSaveBlockerImpl::InitDisplaySleepBlocker(WebContents* web_contents) { 112 void PowerSaveBlockerImpl::InitDisplaySleepBlocker(WebContents* web_contents) {
105 DCHECK_CURRENTLY_ON(BrowserThread::UI); 113 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
106 if (!web_contents) 114 if (!web_contents)
107 return; 115 return;
108 116
109 delegate_ = new Delegate(web_contents); 117 delegate_ = new Delegate(web_contents, ui_task_runner_);
110 delegate_->ApplyBlock(); 118 delegate_->ApplyBlock();
111 } 119 }
112 120
113 bool RegisterPowerSaveBlocker(JNIEnv* env) { 121 bool RegisterPowerSaveBlocker(JNIEnv* env) {
114 return RegisterNativesImpl(env); 122 return RegisterNativesImpl(env);
115 } 123 }
116 124
117 } // namespace content 125 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/media/webrtc/webrtc_internals.cc ('k') | content/browser/power_save_blocker_chromeos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698