| 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/callback_android.h" | 5 #include "base/android/callback_android.h" |
| 6 #include "base/android/scoped_java_ref.h" | 6 #include "base/android/scoped_java_ref.h" |
| 7 #include "chrome/browser/android/offline_pages/background_scheduler_bridge.h" | 7 #include "chrome/browser/android/offline_pages/background_scheduler_bridge.h" |
| 8 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 8 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 9 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | 9 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/profiles/profile_manager.h" | 11 #include "chrome/browser/profiles/profile_manager.h" |
| 12 #include "components/offline_pages/background/device_conditions.h" |
| 12 #include "components/offline_pages/background/request_coordinator.h" | 13 #include "components/offline_pages/background/request_coordinator.h" |
| 13 #include "jni/BackgroundSchedulerBridge_jni.h" | 14 #include "jni/BackgroundSchedulerBridge_jni.h" |
| 14 | 15 |
| 15 using base::android::ScopedJavaGlobalRef; | 16 using base::android::ScopedJavaGlobalRef; |
| 16 | 17 |
| 17 namespace offline_pages { | 18 namespace offline_pages { |
| 18 namespace android { | 19 namespace android { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // C++ callback that delegates to Java callback. | 23 // C++ callback that delegates to Java callback. |
| 23 void ProcessingDoneCallback( | 24 void ProcessingDoneCallback( |
| 24 const ScopedJavaGlobalRef<jobject>& j_callback_obj, bool result) { | 25 const ScopedJavaGlobalRef<jobject>& j_callback_obj, bool result) { |
| 25 base::android::RunCallbackAndroid(j_callback_obj, result); | 26 base::android::RunCallbackAndroid(j_callback_obj, result); |
| 26 } | 27 } |
| 27 | 28 |
| 28 } // namespace | 29 } // namespace |
| 29 | 30 |
| 30 // JNI call to start request processing. | 31 // JNI call to start request processing. |
| 31 static jboolean StartProcessing( | 32 static jboolean StartProcessing(JNIEnv* env, |
| 32 JNIEnv* env, | 33 const JavaParamRef<jclass>& jcaller, |
| 33 const JavaParamRef<jclass>& jcaller, | 34 const jboolean j_power_connected, |
| 34 const JavaParamRef<jobject>& j_callback_obj) { | 35 const jint j_battery_percentage, |
| 36 const jint j_net_connection_type, |
| 37 const JavaParamRef<jobject>& j_callback_obj) { |
| 35 ScopedJavaGlobalRef<jobject> j_callback_ref; | 38 ScopedJavaGlobalRef<jobject> j_callback_ref; |
| 36 j_callback_ref.Reset(env, j_callback_obj); | 39 j_callback_ref.Reset(env, j_callback_obj); |
| 37 | 40 |
| 38 // Lookup/create RequestCoordinator KeyedService and call StartProcessing on | 41 // Lookup/create RequestCoordinator KeyedService and call StartProcessing on |
| 39 // it with bound j_callback_obj. | 42 // it with bound j_callback_obj. |
| 40 Profile* profile = ProfileManager::GetLastUsedProfile(); | 43 Profile* profile = ProfileManager::GetLastUsedProfile(); |
| 41 RequestCoordinator* coordinator = | 44 RequestCoordinator* coordinator = |
| 42 RequestCoordinatorFactory::GetInstance()-> | 45 RequestCoordinatorFactory::GetInstance()-> |
| 43 GetForBrowserContext(profile); | 46 GetForBrowserContext(profile); |
| 44 DVLOG(2) << "resource_coordinator: " << coordinator; | 47 DVLOG(2) << "resource_coordinator: " << coordinator; |
| 45 coordinator->StartProcessing( | 48 DeviceConditions device_conditions( |
| 46 base::Bind(&ProcessingDoneCallback, j_callback_ref)); | 49 j_power_connected, j_battery_percentage, |
| 47 | 50 static_cast<net::NetworkChangeNotifier::ConnectionType>( |
| 48 | 51 j_net_connection_type)); |
| 49 base::Bind(&ProcessingDoneCallback, j_callback_ref); | 52 return coordinator->StartProcessing( |
| 50 // TODO(dougarnett): lookup/create RequestCoordinator KeyedService | 53 device_conditions, base::Bind(&ProcessingDoneCallback, j_callback_ref)); |
| 51 // and call StartProcessing on it with bound j_callback_obj. | |
| 52 return false; | |
| 53 } | 54 } |
| 54 | 55 |
| 55 void BackgroundSchedulerBridge::Schedule( | 56 void BackgroundSchedulerBridge::Schedule( |
| 56 const TriggerCondition& trigger_condition) { | 57 const TriggerCondition& trigger_condition) { |
| 57 JNIEnv* env = base::android::AttachCurrentThread(); | 58 JNIEnv* env = base::android::AttachCurrentThread(); |
| 58 // TODO(dougarnett): pass trigger_condition. | 59 // TODO(dougarnett): pass trigger_condition. |
| 59 Java_BackgroundSchedulerBridge_schedule(env); | 60 Java_BackgroundSchedulerBridge_schedule(env); |
| 60 } | 61 } |
| 61 | 62 |
| 62 void BackgroundSchedulerBridge::Unschedule() { | 63 void BackgroundSchedulerBridge::Unschedule() { |
| 63 JNIEnv* env = base::android::AttachCurrentThread(); | 64 JNIEnv* env = base::android::AttachCurrentThread(); |
| 64 Java_BackgroundSchedulerBridge_unschedule(env); | 65 Java_BackgroundSchedulerBridge_unschedule(env); |
| 65 } | 66 } |
| 66 | 67 |
| 67 bool RegisterBackgroundSchedulerBridge(JNIEnv* env) { | 68 bool RegisterBackgroundSchedulerBridge(JNIEnv* env) { |
| 68 return RegisterNativesImpl(env); | 69 return RegisterNativesImpl(env); |
| 69 } | 70 } |
| 70 | 71 |
| 71 } // namespace android | 72 } // namespace android |
| 72 } // namespace offline_pages | 73 } // namespace offline_pages |
| OLD | NEW |