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

Side by Side Diff: chrome/browser/android/offline_pages/background_scheduler_bridge.cc

Issue 2659813006: Always get device conditions from Java for every attempt. (Closed)
Patch Set: Created 3 years, 10 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 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"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 GetForBrowserContext(profile); 49 GetForBrowserContext(profile);
50 DVLOG(2) << "resource_coordinator: " << coordinator; 50 DVLOG(2) << "resource_coordinator: " << coordinator;
51 DeviceConditions device_conditions( 51 DeviceConditions device_conditions(
52 j_power_connected, j_battery_percentage, 52 j_power_connected, j_battery_percentage,
53 static_cast<net::NetworkChangeNotifier::ConnectionType>( 53 static_cast<net::NetworkChangeNotifier::ConnectionType>(
54 j_net_connection_type)); 54 j_net_connection_type));
55 return coordinator->StartScheduledProcessing( 55 return coordinator->StartScheduledProcessing(
56 device_conditions, base::Bind(&ProcessingDoneCallback, j_callback_ref)); 56 device_conditions, base::Bind(&ProcessingDoneCallback, j_callback_ref));
57 } 57 }
58 58
59 BackgroundSchedulerBridge::BackgroundSchedulerBridge() {}
60
61 BackgroundSchedulerBridge::~BackgroundSchedulerBridge() {}
62
59 void BackgroundSchedulerBridge::Schedule( 63 void BackgroundSchedulerBridge::Schedule(
60 const TriggerConditions& trigger_conditions) { 64 const TriggerConditions& trigger_conditions) {
61 JNIEnv* env = base::android::AttachCurrentThread(); 65 JNIEnv* env = base::android::AttachCurrentThread();
62 ScopedJavaLocalRef<jobject> j_conditions = 66 ScopedJavaLocalRef<jobject> j_conditions =
63 CreateTriggerConditions(env, trigger_conditions.require_power_connected, 67 CreateTriggerConditions(env, trigger_conditions.require_power_connected,
64 trigger_conditions.minimum_battery_percentage, 68 trigger_conditions.minimum_battery_percentage,
65 trigger_conditions.require_unmetered_network); 69 trigger_conditions.require_unmetered_network);
66 Java_BackgroundSchedulerBridge_schedule(env, j_conditions); 70 Java_BackgroundSchedulerBridge_schedule(env, j_conditions);
67 } 71 }
68 72
(...skipping 16 matching lines...) Expand all
85 ScopedJavaLocalRef<jobject> BackgroundSchedulerBridge::CreateTriggerConditions( 89 ScopedJavaLocalRef<jobject> BackgroundSchedulerBridge::CreateTriggerConditions(
86 JNIEnv* env, 90 JNIEnv* env,
87 bool require_power_connected, 91 bool require_power_connected,
88 int minimum_battery_percentage, 92 int minimum_battery_percentage,
89 bool require_unmetered_network) const { 93 bool require_unmetered_network) const {
90 return Java_BackgroundSchedulerBridge_createTriggerConditions( 94 return Java_BackgroundSchedulerBridge_createTriggerConditions(
91 env, require_power_connected, minimum_battery_percentage, 95 env, require_power_connected, minimum_battery_percentage,
92 require_unmetered_network); 96 require_unmetered_network);
93 } 97 }
94 98
99 DeviceConditions& BackgroundSchedulerBridge::GetCurrentDeviceConditions() {
100 JNIEnv* env = base::android::AttachCurrentThread();
101 // Convert return value from Java to C++
dougarnett 2017/01/27 23:15:56 nit - period
102 // Call the Java function, put the returned object into a scoped local ref.
103 const ScopedJavaLocalRef<jobject>& java_conditions_object =
104 Java_BackgroundSchedulerBridge_getCurrentDeviceConditions(env);
105 if (java_conditions_object.is_null()) {
dewittj 2017/01/27 23:03:43 nit: no braces
dougarnett 2017/01/27 23:15:56 nit: keep braces if comment stays
Pete Williamson 2017/01/28 00:24:46 Moved comment above if statement, and lost the bra
Pete Williamson 2017/01/28 00:24:46 Done.
106 // JNI call failed somehow, so use the last known conditions.
107 return *(device_conditions_.get());
108 }
109 jobject java_conditions =
110 reinterpret_cast<jobject>(java_conditions_object.obj());
dewittj 2017/01/27 23:03:43 this cast is not necessary.
Pete Williamson 2017/01/28 00:24:46 Done.
111
112 // Get the corrensponding class object so we can do introspection.
113 ScopedJavaLocalRef<jclass> device_conditions_clazz = base::android::GetClass(
114 env, "org/chromium/chrome/browser/offlinepages/DeviceConditions");
115
116 // Find the field ID to use to get the member variables.
117 jfieldID power_field_id =
118 env->GetFieldID(device_conditions_clazz.obj(), "mPowerConnected", "Z");
dewittj 2017/01/27 23:03:43 I'd like to recommend you use a more standard Chro
Pete Williamson 2017/01/28 00:24:46 Done.
119 jfieldID battery_field_id =
120 env->GetFieldID(device_conditions_clazz.obj(), "mBatteryPercentage", "I");
121 jfieldID network_field_id =
122 env->GetFieldID(device_conditions_clazz.obj(), "mNetConnectionType", "I");
123
124 // Using the field ID, get the member variables from the object.
125 jboolean jpower = env->GetBooleanField(java_conditions, power_field_id);
126 jint jbattery = env->GetIntField(java_conditions, battery_field_id);
127 jint jnetwork = env->GetIntField(java_conditions, network_field_id);
128
129 // Cast the java types back to the types we use.
130 bool power = static_cast<bool>(jpower);
131 int battery = static_cast<int>(jbattery);
132 int network_int = static_cast<int>(jnetwork);
dewittj 2017/01/27 23:03:43 just do the single cast to ConnectionType
Pete Williamson 2017/01/28 00:24:46 Done.
133 net::NetworkChangeNotifier::ConnectionType network_connection_type =
134 static_cast<net::NetworkChangeNotifier::ConnectionType>(network_int);
135
136 // Now return the current conditions.
137 DeviceConditions device_conditions(power, battery, network_connection_type);
138 device_conditions_.reset(new DeviceConditions(device_conditions));
dewittj 2017/01/27 23:03:43 device_conditions_ = base::MakeUnique<DeviceCondit
Pete Williamson 2017/01/28 00:24:46 Done.
139 return *(device_conditions_.get());
140 }
141
95 bool RegisterBackgroundSchedulerBridge(JNIEnv* env) { 142 bool RegisterBackgroundSchedulerBridge(JNIEnv* env) {
96 return RegisterNativesImpl(env); 143 return RegisterNativesImpl(env);
97 } 144 }
98 145
99 } // namespace android 146 } // namespace android
100 } // namespace offline_pages 147 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698