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

Side by Side Diff: chrome/browser/android/offline_pages/prefetch/prefetch_background_task.cc

Issue 2820263002: [Offline Prefetch] Create a new JobScheduler task to wake up for net activity. (Closed)
Patch Set: tommycli@ comments. 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 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 #include "chrome/browser/android/offline_pages/prefetch/prefetch_background_task .h"
6
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/profiles/profile_android.h"
9 #include "components/offline_pages/content/prefetch_service_factory.h"
10 #include "content/public/browser/browser_context.h"
11 #include "jni/PrefetchBackgroundTask_jni.h"
12
13 using base::android::JavaParamRef;
14 using base::android::ScopedJavaGlobalRef;
15 using base::android::ScopedJavaLocalRef;
16
17 namespace offline_pages {
18 namespace prefetch {
19
20 // JNI call to start request processing in scheduled mode.
21 static jboolean StartPrefetchTask(JNIEnv* env,
22 const JavaParamRef<jobject>& jcaller,
23 const JavaParamRef<jobject>& jprofile) {
24 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
25 DCHECK(profile);
26
27 PrefetchService* prefetch_service =
28 PrefetchServiceFactory::GetForBrowserContext(profile);
29 if (!prefetch_service)
30 return false;
31
32 prefetch_service->BeginBackgroundTask(
33 base::MakeUnique<PrefetchBackgroundTask>(env, jcaller, prefetch_service));
34 return true;
35 }
36
37 } // namespace prefetch
38
39 // static
40 void PrefetchBackgroundTask::Schedule() {
41 JNIEnv* env = base::android::AttachCurrentThread();
42 return prefetch::Java_PrefetchBackgroundTask_scheduleTask(env);
43 }
44
45 // static
46 void PrefetchBackgroundTask::Cancel() {
47 JNIEnv* env = base::android::AttachCurrentThread();
48 return prefetch::Java_PrefetchBackgroundTask_cancelTask(env);
49 }
50
51 PrefetchBackgroundTask::PrefetchBackgroundTask(
52 JNIEnv* env,
53 const JavaParamRef<jobject>& java_prefetch_background_task,
54 PrefetchService* service)
55 : java_prefetch_background_task_(java_prefetch_background_task),
56 service_(service) {
57 // Give the Java side a pointer to the new background task object.
58 prefetch::Java_PrefetchBackgroundTask_setNativeTask(
59 env, java_prefetch_background_task_, reinterpret_cast<jlong>(this));
60 }
61
62 PrefetchBackgroundTask::~PrefetchBackgroundTask() {
63 JNIEnv* env = base::android::AttachCurrentThread();
64 prefetch::Java_PrefetchBackgroundTask_doneProcessing(
65 env, java_prefetch_background_task_.obj(), needs_reschedule_);
66 }
67
68 bool PrefetchBackgroundTask::OnStopTask(JNIEnv* env,
69 const JavaParamRef<jobject>& jcaller) {
70 DCHECK(jcaller.obj() == java_prefetch_background_task_.obj());
71 service_->StopBackgroundTask(this);
72 return needs_reschedule_;
73 }
74
75 void PrefetchBackgroundTask::SetNeedsReschedule(bool reschedule) {
76 needs_reschedule_ = reschedule;
77 }
78
79 bool RegisterPrefetchBackgroundTask(JNIEnv* env) {
80 return prefetch::RegisterNativesImpl(env);
81 }
82
83 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698