OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/scoped_java_ref.h" | 5 #include "base/android/scoped_java_ref.h" |
6 #include "chrome/browser/android/offline_pages/background_scheduler_bridge.h" | 6 #include "chrome/browser/android/offline_pages/background_scheduler_bridge.h" |
7 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | |
8 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | |
7 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/profiles/profile_android.h" | 10 #include "chrome/browser/profiles/profile_android.h" |
11 #include "components/offline_pages/background/request_coordinator.h" | |
9 #include "jni/BackgroundSchedulerBridge_jni.h" | 12 #include "jni/BackgroundSchedulerBridge_jni.h" |
10 | 13 |
11 using base::android::ScopedJavaGlobalRef; | 14 using base::android::ScopedJavaGlobalRef; |
12 | 15 |
13 namespace offline_pages { | 16 namespace offline_pages { |
14 namespace android { | 17 namespace android { |
15 | 18 |
16 namespace { | 19 namespace { |
17 | 20 |
18 // C++ callback that delegates to Java callback. | 21 // C++ callback that delegates to Java callback. |
19 void ProcessingDoneCallback( | 22 void ProcessingDoneCallback( |
20 const ScopedJavaGlobalRef<jobject>& j_callback_obj, jboolean result) { | 23 const ScopedJavaGlobalRef<jobject>& j_callback_obj, jboolean result) { |
21 JNIEnv* env = base::android::AttachCurrentThread(); | 24 JNIEnv* env = base::android::AttachCurrentThread(); |
22 Java_ProcessingDoneCallback_onProcessingDone( | 25 Java_ProcessingDoneCallback_onProcessingDone( |
23 env, j_callback_obj.obj(), result); | 26 env, j_callback_obj.obj(), result); |
24 } | 27 } |
25 | 28 |
26 } // namespace | 29 } // namespace |
27 | 30 |
31 // This static function acts as a factory to create the scheduler bridge if it | |
32 // does not already exist, and re-use it if it does. It also attaches the | |
33 // Java side of the bridge to the java_ref_ variable, and returns the c++ side | |
34 // pointer for the java side to attach to. It is called from Java. | |
35 // TODO(petewil): Is it better to use ScopedJavaLocalRef or ScopedJavaGlobalRef? | |
36 static ScopedJavaLocalRef<jobject> GetBackgroundSchedulerBridgeForProfile( | |
37 JNIEnv* env, | |
38 const JavaParamRef<jclass>& jcaller, | |
39 const JavaParamRef<jobject>& j_profile) { | |
40 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | |
41 | |
42 // The bridge pointer is owned by the request coordinator. | |
43 RequestCoordinator* request_coordinator = | |
44 RequestCoordinatorFactory::GetForBrowserContext(profile); | |
dewittj
2016/05/20 21:52:31
needs a null check here, we don't support incognit
Pete Williamson
2016/05/23 19:36:31
Done. (but the check is further down, not here)
| |
45 | |
46 // We use a reinterpret cast here so that the type returned by the | |
47 // GetScheduler call can remain an interface, which we can mock differently | |
48 // for testing. | |
49 BackgroundSchedulerBridge* bridge = | |
50 reinterpret_cast<BackgroundSchedulerBridge*>( | |
51 request_coordinator->GetScheduler()); | |
52 if (!bridge) { | |
53 bridge = new BackgroundSchedulerBridge(); | |
54 } | |
55 bridge->AttachBridgeToJavaSide(env); | |
56 return ScopedJavaLocalRef<jobject>(bridge->java_ref()); | |
57 } | |
58 | |
28 // JNI call to start request processing. | 59 // JNI call to start request processing. |
29 static jboolean StartProcessing( | 60 static jboolean StartProcessing( |
30 JNIEnv* env, | 61 JNIEnv* env, |
31 const JavaParamRef<jclass>& jcaller, | 62 const JavaParamRef<jclass>& jcaller, |
32 const JavaParamRef<jobject>& j_profile, | 63 const JavaParamRef<jobject>& j_context, |
33 const JavaParamRef<jobject>& j_callback_obj) { | 64 const JavaParamRef<jobject>& j_callback_obj) { |
34 ScopedJavaGlobalRef<jobject> j_callback_ref; | 65 ScopedJavaGlobalRef<jobject> j_callback_ref; |
35 j_callback_ref.Reset(env, j_callback_obj); | 66 j_callback_ref.Reset(env, j_callback_obj); |
36 base::Bind(&ProcessingDoneCallback, j_callback_ref); | 67 base::Bind(&ProcessingDoneCallback, j_callback_ref); |
37 // TODO(dougarnett): lookup/create RequestCoordinator KeyedService | 68 // TODO(dougarnett): lookup/create RequestCoordinator KeyedService |
38 // and call StartProcessing on it with bound j_callback_obj. | 69 // and call StartProcessing on it with bound j_callback_obj. |
39 return false; | 70 return false; |
40 } | 71 } |
41 | 72 |
42 BackgroundSchedulerBridge::BackgroundSchedulerBridge() { | 73 // We split the construction into two steps, build and init. The Ctor will be |
74 // called from the RequestCoordinatorFactory, and the init function will be | |
75 // called from the Java side when a BackgroundTask arrives. | |
76 // TODO(petewil): We eventually need some piece of Java code to call | |
77 // BackgroundSchedulerBridge.getForProfile(); so "schedule()" will work | |
78 // if BackgroundTask.incomingTask has not been called this session. | |
79 BackgroundSchedulerBridge::BackgroundSchedulerBridge() {} | |
80 | |
81 BackgroundSchedulerBridge::~BackgroundSchedulerBridge() { | |
82 JNIEnv* env = base::android::AttachCurrentThread(); | |
83 | |
84 // Native shutdown causes the destruction of |this|. | |
85 if (java_ref_.obj() != nullptr) { | |
86 Java_BackgroundSchedulerBridge_backgroundSchedulerBridgeDestroyed( | |
87 env, java_ref_.obj()); | |
88 } | |
43 } | 89 } |
44 | 90 |
45 BackgroundSchedulerBridge::~BackgroundSchedulerBridge() { | 91 // Attach to the Java side of the bridge if it is not already attached. |
92 void BackgroundSchedulerBridge::AttachBridgeToJavaSide(JNIEnv* env) { | |
93 if (java_ref().is_null()) { | |
94 ScopedJavaLocalRef<jobject> j_offline_page_bridge = | |
95 Java_BackgroundSchedulerBridge_create( | |
96 env, reinterpret_cast<jlong>(this)); | |
97 java_ref_.Reset(j_offline_page_bridge); | |
98 } | |
46 } | 99 } |
47 | 100 |
48 void BackgroundSchedulerBridge::Schedule( | 101 void BackgroundSchedulerBridge::Schedule( |
49 const TriggerCondition& trigger_condition) { | 102 const TriggerCondition& trigger_condition) { |
50 JNIEnv* env = base::android::AttachCurrentThread(); | 103 JNIEnv* env = base::android::AttachCurrentThread(); |
51 // TODO(dougarnett): pass trigger_condition. | 104 // TODO(dougarnett): pass trigger_condition. |
52 Java_BackgroundSchedulerBridge_schedule(env); | 105 Java_BackgroundSchedulerBridge_schedule(env); |
53 } | 106 } |
54 | 107 |
55 void BackgroundSchedulerBridge::Unschedule() { | 108 void BackgroundSchedulerBridge::Unschedule() { |
56 JNIEnv* env = base::android::AttachCurrentThread(); | 109 JNIEnv* env = base::android::AttachCurrentThread(); |
57 Java_BackgroundSchedulerBridge_unschedule(env); | 110 Java_BackgroundSchedulerBridge_unschedule(env); |
58 } | 111 } |
59 | 112 |
60 bool RegisterBackgroundSchedulerBridge(JNIEnv* env) { | 113 bool RegisterBackgroundSchedulerBridge(JNIEnv* env) { |
61 return RegisterNativesImpl(env); | 114 return RegisterNativesImpl(env); |
62 } | 115 } |
63 | 116 |
64 } // namespace android | 117 } // namespace android |
65 } // namespace offline_pages | 118 } // namespace offline_pages |
OLD | NEW |