Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/android/offline_pages/evaluation/offline_page_evaluatio n_bridge.h" | |
| 6 | |
| 7 #include "base/android/callback_android.h" | |
| 8 #include "base/android/jni_android.h" | |
| 9 #include "base/android/jni_string.h" | |
| 10 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | |
| 11 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/profiles/profile_android.h" | |
| 14 #include "components/offline_pages/background/request_coordinator.h" | |
| 15 #include "components/offline_pages/background/request_notifier.h" | |
| 16 #include "components/offline_pages/background/save_page_request.h" | |
| 17 #include "components/offline_pages/offline_page_item.h" | |
| 18 #include "components/offline_pages/offline_page_model.h" | |
| 19 #include "content/public/browser/browser_context.h" | |
| 20 #include "jni/OfflinePageEvaluationBridge_jni.h" | |
| 21 #include "jni/SavePageRequest_jni.h" | |
| 22 | |
| 23 using base::android::ConvertJavaStringToUTF8; | |
| 24 using base::android::ConvertUTF8ToJavaString; | |
| 25 using base::android::JavaParamRef; | |
| 26 using base::android::ScopedJavaGlobalRef; | |
| 27 using base::android::ScopedJavaLocalRef; | |
| 28 | |
| 29 namespace offline_pages { | |
| 30 namespace android { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 void ToJavaOfflinePageList(JNIEnv* env, | |
| 35 jobject j_result_obj, | |
| 36 const std::vector<OfflinePageItem>& offline_pages) { | |
| 37 for (const OfflinePageItem& offline_page : offline_pages) { | |
| 38 Java_OfflinePageEvaluationBridge_createOfflinePageAndAddToList( | |
| 39 env, j_result_obj, | |
| 40 ConvertUTF8ToJavaString(env, offline_page.url.spec()), | |
| 41 offline_page.offline_id, | |
| 42 ConvertUTF8ToJavaString(env, offline_page.client_id.name_space), | |
| 43 ConvertUTF8ToJavaString(env, offline_page.client_id.id), | |
| 44 ConvertUTF8ToJavaString(env, offline_page.file_path.value()), | |
| 45 offline_page.file_size, offline_page.creation_time.ToJavaTime(), | |
| 46 offline_page.access_count, offline_page.last_access_time.ToJavaTime()); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 ScopedJavaLocalRef<jobject> ToJavaSavePageRequest( | |
| 51 JNIEnv* env, | |
| 52 const SavePageRequest& request) { | |
| 53 return Java_SavePageRequest_create( | |
| 54 env, static_cast<int>(request.request_state()), request.request_id(), | |
| 55 ConvertUTF8ToJavaString(env, request.url().spec()), | |
| 56 ConvertUTF8ToJavaString(env, request.client_id().name_space), | |
| 57 ConvertUTF8ToJavaString(env, request.client_id().id)); | |
| 58 } | |
| 59 | |
| 60 void GetAllPagesCallback( | |
| 61 const ScopedJavaGlobalRef<jobject>& j_result_obj, | |
| 62 const ScopedJavaGlobalRef<jobject>& j_callback_obj, | |
| 63 const OfflinePageModel::MultipleOfflinePageItemResult& result) { | |
| 64 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 65 ToJavaOfflinePageList(env, j_result_obj.obj(), result); | |
| 66 base::android::RunCallbackAndroid(j_callback_obj, j_result_obj); | |
| 67 } | |
| 68 | |
| 69 void OnPushRequestsDone(const ScopedJavaGlobalRef<jobject>& j_callback_obj, | |
| 70 bool result) { | |
| 71 base::android::RunCallbackAndroid(j_callback_obj, result); | |
| 72 } | |
| 73 | |
| 74 } // namespace | |
| 75 | |
| 76 static ScopedJavaLocalRef<jobject> GetBridgeForProfile( | |
| 77 JNIEnv* env, | |
| 78 const JavaParamRef<jclass>& jcaller, | |
| 79 const JavaParamRef<jobject>& j_profile) { | |
| 80 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | |
| 81 | |
| 82 OfflinePageModel* offline_page_model = | |
| 83 OfflinePageModelFactory::GetForBrowserContext(profile); | |
| 84 | |
| 85 RequestCoordinator* request_coordinator = | |
| 86 RequestCoordinatorFactory::GetForBrowserContext(profile); | |
| 87 | |
| 88 if (offline_page_model == nullptr || request_coordinator == nullptr) | |
| 89 return ScopedJavaLocalRef<jobject>(); | |
| 90 | |
| 91 OfflinePageEvaluationBridge* bridge = new OfflinePageEvaluationBridge( | |
| 92 env, profile, offline_page_model, request_coordinator); | |
| 93 | |
| 94 return ScopedJavaLocalRef<jobject>(bridge->java_ref()); | |
| 95 } | |
| 96 | |
| 97 // static | |
| 98 bool OfflinePageEvaluationBridge::Register(JNIEnv* env) { | |
| 99 return RegisterNativesImpl(env); | |
| 100 } | |
| 101 | |
| 102 OfflinePageEvaluationBridge::OfflinePageEvaluationBridge( | |
| 103 JNIEnv* env, | |
| 104 content::BrowserContext* browser_context, | |
| 105 OfflinePageModel* offline_page_model, | |
| 106 RequestCoordinator* request_coordinator) | |
| 107 : browser_context_(browser_context), | |
| 108 offline_page_model_(offline_page_model), | |
| 109 request_coordinator_(request_coordinator) { | |
| 110 ScopedJavaLocalRef<jobject> j_offline_page_evaluation_bridge = | |
|
gone
2016/10/13 17:56:58
You should just be able to feed this to .Reset() b
romax
2016/10/13 18:46:22
Done.
| |
| 111 Java_OfflinePageEvaluationBridge_create(env, | |
| 112 reinterpret_cast<jlong>(this)); | |
| 113 java_ref_.Reset(j_offline_page_evaluation_bridge); | |
| 114 | |
| 115 NotifyIfDoneLoading(); | |
| 116 offline_page_model_->AddObserver(this); | |
| 117 request_coordinator_->AddObserver(this); | |
| 118 } | |
| 119 | |
| 120 OfflinePageEvaluationBridge::~OfflinePageEvaluationBridge() { | |
| 121 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 122 Java_OfflinePageEvaluationBridge_offlinePageEvaluationBridgeDestroyed( | |
| 123 env, java_ref_); | |
| 124 } | |
| 125 | |
| 126 // Implement OfflinePageModel::Observer | |
| 127 void OfflinePageEvaluationBridge::OfflinePageModelLoaded( | |
| 128 OfflinePageModel* model) { | |
| 129 DCHECK_EQ(offline_page_model_, model); | |
| 130 NotifyIfDoneLoading(); | |
| 131 } | |
| 132 | |
| 133 void OfflinePageEvaluationBridge::OfflinePageModelChanged( | |
| 134 OfflinePageModel* model) {} | |
| 135 | |
| 136 void OfflinePageEvaluationBridge::OfflinePageDeleted( | |
| 137 int64_t offline_id, | |
| 138 const ClientId& client_id) {} | |
| 139 | |
| 140 // Implement RequestCoordinator::Observer | |
| 141 void OfflinePageEvaluationBridge::OnAdded(const SavePageRequest& request) { | |
| 142 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 143 Java_OfflinePageEvaluationBridge_savePageRequestAdded( | |
| 144 env, java_ref_, ToJavaSavePageRequest(env, request)); | |
| 145 } | |
| 146 | |
| 147 void OfflinePageEvaluationBridge::OnCompleted( | |
| 148 const SavePageRequest& request, | |
| 149 RequestNotifier::BackgroundSavePageResult status) { | |
| 150 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 151 Java_OfflinePageEvaluationBridge_savePageRequestCompleted( | |
| 152 env, java_ref_, ToJavaSavePageRequest(env, request), | |
| 153 static_cast<int>(status)); | |
| 154 } | |
| 155 | |
| 156 void OfflinePageEvaluationBridge::OnChanged(const SavePageRequest& request) { | |
| 157 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 158 Java_OfflinePageEvaluationBridge_savePageRequestChanged( | |
| 159 env, java_ref_, ToJavaSavePageRequest(env, request)); | |
| 160 } | |
| 161 | |
| 162 void OfflinePageEvaluationBridge::GetAllPages( | |
| 163 JNIEnv* env, | |
| 164 const JavaParamRef<jobject>& obj, | |
| 165 const JavaParamRef<jobject>& j_result_obj, | |
| 166 const JavaParamRef<jobject>& j_callback_obj) { | |
| 167 DCHECK(j_result_obj); | |
| 168 DCHECK(j_callback_obj); | |
| 169 | |
| 170 ScopedJavaGlobalRef<jobject> j_result_ref; | |
| 171 j_result_ref.Reset(env, j_result_obj); | |
|
gone
2016/10/13 17:56:58
One of the constructors for ScopedJavaGlobalRef do
romax
2016/10/13 18:46:23
Done.
| |
| 172 | |
| 173 ScopedJavaGlobalRef<jobject> j_callback_ref; | |
| 174 j_callback_ref.Reset(env, j_callback_obj); | |
| 175 | |
| 176 offline_page_model_->GetAllPages( | |
| 177 base::Bind(&GetAllPagesCallback, j_result_ref, j_callback_ref)); | |
| 178 } | |
| 179 | |
| 180 void OfflinePageEvaluationBridge::PushRequestProcessing( | |
| 181 JNIEnv* env, | |
| 182 const base::android::JavaParamRef<jobject>& obj, | |
|
gone
2016/10/13 17:56:59
You're already "using" this; just remove base::and
romax
2016/10/13 18:46:23
Done.
| |
| 183 const base::android::JavaParamRef<jobject>& j_callback_obj) { | |
| 184 ScopedJavaGlobalRef<jobject> j_callback_ref(j_callback_obj); | |
| 185 DCHECK(request_coordinator_); | |
| 186 base::android::RunCallbackAndroid(j_callback_obj, false); | |
| 187 | |
| 188 net::NetworkChangeNotifier::ConnectionType connection = | |
| 189 net::NetworkChangeNotifier::GetConnectionType(); | |
| 190 DeviceConditions device_conditions(false, 0, connection); | |
| 191 request_coordinator_->StartProcessing( | |
| 192 device_conditions, base::Bind(&OnPushRequestsDone, j_callback_ref)); | |
| 193 } | |
| 194 | |
| 195 void OfflinePageEvaluationBridge::SavePageLater( | |
| 196 JNIEnv* env, | |
| 197 const JavaParamRef<jobject>& obj, | |
| 198 const JavaParamRef<jstring>& j_url, | |
| 199 const JavaParamRef<jstring>& j_namespace, | |
| 200 const JavaParamRef<jstring>& j_client_id, | |
| 201 jboolean user_requested) { | |
| 202 offline_pages::ClientId client_id; | |
| 203 client_id.name_space = ConvertJavaStringToUTF8(env, j_namespace); | |
| 204 client_id.id = ConvertJavaStringToUTF8(env, j_client_id); | |
| 205 | |
| 206 request_coordinator_->SavePageLater( | |
| 207 GURL(ConvertJavaStringToUTF8(env, j_url)), client_id, | |
| 208 static_cast<bool>(user_requested), | |
| 209 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER); | |
| 210 } | |
| 211 | |
| 212 void OfflinePageEvaluationBridge::NotifyIfDoneLoading() const { | |
| 213 if (!offline_page_model_->is_loaded()) | |
| 214 return; | |
| 215 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 216 Java_OfflinePageEvaluationBridge_offlinePageModelLoaded(env, java_ref_); | |
| 217 } | |
| 218 | |
| 219 } // namespace android | |
| 220 } // namespace offline_pages | |
| OLD | NEW |