Chromium Code Reviews| 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 "chrome/browser/android/offline_pages/downloads/offline_page_download_b ridge.h" | 5 #include "chrome/browser/android/offline_pages/downloads/offline_page_download_b ridge.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/bind.h" | |
| 11 #include "base/callback.h" | |
| 10 #include "base/guid.h" | 12 #include "base/guid.h" |
| 13 #include "base/logging.h" | |
| 11 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 12 #include "chrome/browser/android/offline_pages/downloads/offline_page_notificati on_bridge.h" | 15 #include "chrome/browser/android/offline_pages/downloads/offline_page_notificati on_bridge.h" |
| 13 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 16 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 17 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 18 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | |
| 15 #include "chrome/browser/android/tab_android.h" | 19 #include "chrome/browser/android/tab_android.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/profiles/profile_android.h" | 21 #include "chrome/browser/profiles/profile_android.h" |
| 22 #include "components/offline_pages/background/request_coordinator.h" | |
| 18 #include "components/offline_pages/client_namespace_constants.h" | 23 #include "components/offline_pages/client_namespace_constants.h" |
| 19 #include "components/offline_pages/downloads/download_ui_item.h" | 24 #include "components/offline_pages/downloads/download_ui_item.h" |
| 20 #include "components/offline_pages/offline_page_model.h" | 25 #include "components/offline_pages/offline_page_model.h" |
| 26 #include "content/public/browser/browser_context.h" | |
| 21 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 22 #include "jni/OfflinePageDownloadBridge_jni.h" | 28 #include "jni/OfflinePageDownloadBridge_jni.h" |
| 23 #include "net/base/filename_util.h" | 29 #include "net/base/filename_util.h" |
| 24 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 25 | 31 |
| 26 using base::android::AttachCurrentThread; | 32 using base::android::AttachCurrentThread; |
| 27 using base::android::ConvertJavaStringToUTF8; | 33 using base::android::ConvertJavaStringToUTF8; |
| 28 using base::android::ConvertUTF8ToJavaString; | 34 using base::android::ConvertUTF8ToJavaString; |
| 29 using base::android::ConvertUTF16ToJavaString; | 35 using base::android::ConvertUTF16ToJavaString; |
| 30 using base::android::JavaParamRef; | 36 using base::android::JavaParamRef; |
| 31 using base::android::ScopedJavaGlobalRef; | 37 using base::android::ScopedJavaGlobalRef; |
| 32 using base::android::ScopedJavaLocalRef; | 38 using base::android::ScopedJavaLocalRef; |
| 33 | 39 |
| 34 namespace offline_pages { | 40 namespace offline_pages { |
| 35 namespace android { | 41 namespace android { |
| 36 | 42 |
| 37 namespace { | 43 namespace { |
| 38 | 44 |
| 45 using RequestContinuationCallback = | |
| 46 base::Callback<void(const std::vector<int64_t>&)>; | |
| 47 | |
| 39 void ToJavaOfflinePageDownloadItemList( | 48 void ToJavaOfflinePageDownloadItemList( |
| 40 JNIEnv* env, | 49 JNIEnv* env, |
| 41 jobject j_result_obj, | 50 jobject j_result_obj, |
| 42 const std::vector<const DownloadUIItem*>& items) { | 51 const std::vector<const DownloadUIItem*>& items) { |
| 43 for (const auto item : items) { | 52 for (const auto item : items) { |
| 44 Java_OfflinePageDownloadBridge_createDownloadItemAndAddToList( | 53 Java_OfflinePageDownloadBridge_createDownloadItemAndAddToList( |
| 45 env, j_result_obj, ConvertUTF8ToJavaString(env, item->guid), | 54 env, j_result_obj, ConvertUTF8ToJavaString(env, item->guid), |
| 46 ConvertUTF8ToJavaString(env, item->url.spec()), | 55 ConvertUTF8ToJavaString(env, item->url.spec()), |
| 47 ConvertUTF16ToJavaString(env, item->title), | 56 ConvertUTF16ToJavaString(env, item->title), |
| 48 ConvertUTF8ToJavaString(env, item->target_path.value()), | 57 ConvertUTF8ToJavaString(env, item->target_path.value()), |
| 49 item->start_time.ToJavaTime(), item->total_bytes); | 58 item->start_time.ToJavaTime(), item->total_bytes); |
| 50 } | 59 } |
| 51 } | 60 } |
| 52 | 61 |
| 53 ScopedJavaLocalRef<jobject> ToJavaOfflinePageDownloadItem( | 62 ScopedJavaLocalRef<jobject> ToJavaOfflinePageDownloadItem( |
| 54 JNIEnv* env, | 63 JNIEnv* env, |
| 55 const DownloadUIItem& item) { | 64 const DownloadUIItem& item) { |
| 56 return Java_OfflinePageDownloadBridge_createDownloadItem( | 65 return Java_OfflinePageDownloadBridge_createDownloadItem( |
| 57 env, ConvertUTF8ToJavaString(env, item.guid), | 66 env, ConvertUTF8ToJavaString(env, item.guid), |
| 58 ConvertUTF8ToJavaString(env, item.url.spec()), | 67 ConvertUTF8ToJavaString(env, item.url.spec()), |
| 59 ConvertUTF16ToJavaString(env, item.title), | 68 ConvertUTF16ToJavaString(env, item.title), |
| 60 ConvertUTF8ToJavaString(env, item.target_path.value()), | 69 ConvertUTF8ToJavaString(env, item.target_path.value()), |
| 61 item.start_time.ToJavaTime(), item.total_bytes); | 70 item.start_time.ToJavaTime(), item.total_bytes); |
| 62 } | 71 } |
| 72 | |
| 73 void FilterRequestsByGuid(const std::string& guid, | |
| 74 const RequestContinuationCallback& callback, | |
|
dewittj
2016/08/25 17:26:57
Using a callback here is fancy but maybe less read
fgorski
2016/08/25 18:09:01
Done.
| |
| 75 const std::vector<SavePageRequest>& requests) { | |
| 76 std::vector<int64_t> request_ids; | |
| 77 for (const SavePageRequest& request : requests) { | |
| 78 if (request.client_id().id == guid && | |
| 79 (request.client_id().name_space == kDownloadNamespace || | |
|
dewittj
2016/08/25 17:26:57
Any chance you can make a function NamespaceIsUsed
fgorski
2016/08/25 18:09:01
Good idea, but I'd rather have it in separate patc
| |
| 80 request.client_id().name_space == kAsyncNamespace)) { | |
| 81 request_ids.push_back(request.request_id()); | |
| 82 } | |
| 83 } | |
| 84 callback.Run(request_ids); | |
| 85 } | |
| 86 | |
| 87 void CancelRequestCallback(const RequestQueue::UpdateMultipleRequestResults&) { | |
| 88 // Results ignored here, as UI uses observer to update itself. | |
| 89 } | |
| 90 | |
| 91 void CancelRequestsContinuation(RequestCoordinator* coordinator, | |
|
dewittj
2016/08/25 17:26:57
As we discussed offline, I'd recommend passing a B
fgorski
2016/08/25 18:09:01
Done.
| |
| 92 const std::vector<int64_t>& request_ids) { | |
| 93 coordinator->RemoveRequests(request_ids, base::Bind(&CancelRequestCallback)); | |
| 94 } | |
| 95 | |
| 96 void PauseRequestsContinuation(RequestCoordinator* coordinator, | |
| 97 const std::vector<int64_t>& request_ids) { | |
| 98 coordinator->PauseRequests(request_ids); | |
| 99 } | |
| 100 | |
| 101 void ResumeRequestsContinuation(RequestCoordinator* coordinator, | |
| 102 const std::vector<int64_t>& request_ids) { | |
| 103 coordinator->ResumeRequests(request_ids); | |
| 104 } | |
| 105 | |
| 63 } // namespace | 106 } // namespace |
| 64 | 107 |
| 65 OfflinePageDownloadBridge::OfflinePageDownloadBridge( | 108 OfflinePageDownloadBridge::OfflinePageDownloadBridge( |
| 66 JNIEnv* env, | 109 JNIEnv* env, |
| 67 const JavaParamRef<jobject>& obj, | 110 const JavaParamRef<jobject>& obj, |
| 68 DownloadUIAdapter* download_ui_adapter) | 111 DownloadUIAdapter* download_ui_adapter, |
| 112 content::BrowserContext* browser_context) | |
| 69 : weak_java_ref_(env, obj), | 113 : weak_java_ref_(env, obj), |
| 70 download_ui_adapter_(download_ui_adapter) { | 114 download_ui_adapter_(download_ui_adapter), |
| 115 browser_context_(browser_context) { | |
| 71 DCHECK(download_ui_adapter_); | 116 DCHECK(download_ui_adapter_); |
| 72 download_ui_adapter_->AddObserver(this); | 117 download_ui_adapter_->AddObserver(this); |
| 73 } | 118 } |
| 74 | 119 |
| 75 OfflinePageDownloadBridge::~OfflinePageDownloadBridge() {} | 120 OfflinePageDownloadBridge::~OfflinePageDownloadBridge() {} |
| 76 | 121 |
| 77 // static | 122 // static |
| 78 bool OfflinePageDownloadBridge::Register(JNIEnv* env) { | 123 bool OfflinePageDownloadBridge::Register(JNIEnv* env) { |
| 79 return RegisterNativesImpl(env); | 124 return RegisterNativesImpl(env); |
| 80 } | 125 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); | 188 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); |
| 144 if (!tab) | 189 if (!tab) |
| 145 return; | 190 return; |
| 146 | 191 |
| 147 content::WebContents* web_contents = tab->web_contents(); | 192 content::WebContents* web_contents = tab->web_contents(); |
| 148 if (!web_contents) | 193 if (!web_contents) |
| 149 return; | 194 return; |
| 150 | 195 |
| 151 offline_pages::OfflinePageModel* offline_page_model = | 196 offline_pages::OfflinePageModel* offline_page_model = |
| 152 OfflinePageModelFactory::GetForBrowserContext( | 197 OfflinePageModelFactory::GetForBrowserContext( |
| 153 tab->GetProfile()->GetOriginalProfile()); | 198 tab->GetProfile()->GetOriginalProfile()); |
| 154 if (!offline_page_model) | 199 if (!offline_page_model) |
| 155 return; | 200 return; |
| 156 | 201 |
| 157 GURL url = web_contents->GetLastCommittedURL(); | 202 GURL url = web_contents->GetLastCommittedURL(); |
| 158 auto archiver = | 203 auto archiver = |
| 159 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); | 204 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); |
| 160 | 205 |
| 161 offline_pages::ClientId client_id; | 206 offline_pages::ClientId client_id; |
| 162 client_id.name_space = offline_pages::kDownloadNamespace; | 207 client_id.name_space = offline_pages::kDownloadNamespace; |
| 163 client_id.id = base::GenerateGUID(); | 208 client_id.id = base::GenerateGUID(); |
| 164 | 209 |
| 165 DownloadUIItem item; | 210 DownloadUIItem item; |
| 166 item.guid = client_id.id; | 211 item.guid = client_id.id; |
| 167 item.url = url; | 212 item.url = url; |
| 168 | 213 |
| 169 OfflinePageNotificationBridge bridge; | 214 OfflinePageNotificationBridge bridge; |
| 170 bridge.NotifyDownloadProgress(item); | 215 bridge.NotifyDownloadProgress(item); |
| 171 | 216 |
| 172 offline_page_model->SavePage( | 217 offline_page_model->SavePage( |
| 173 url, client_id, 0ul, std::move(archiver), | 218 url, client_id, 0ul, std::move(archiver), |
| 174 base::Bind(&OfflinePageDownloadBridge::SavePageCallback, item)); | 219 base::Bind(&OfflinePageDownloadBridge::SavePageCallback, item)); |
| 175 } | 220 } |
| 176 | 221 |
| 222 void OfflinePageDownloadBridge::CancelDownload( | |
| 223 JNIEnv* env, | |
| 224 const JavaParamRef<jobject>& obj, | |
| 225 const JavaParamRef<jstring>& j_guid) { | |
| 226 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | |
| 227 RequestCoordinator* request_coordinator = | |
| 228 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); | |
| 229 | |
| 230 request_coordinator->GetAllRequests( | |
| 231 base::Bind(&FilterRequestsByGuid, guid, | |
| 232 base::Bind(&CancelRequestsContinuation, request_coordinator))); | |
| 233 } | |
| 234 | |
| 235 void OfflinePageDownloadBridge::PauseDownload( | |
| 236 JNIEnv* env, | |
| 237 const JavaParamRef<jobject>& obj, | |
| 238 const JavaParamRef<jstring>& j_guid) { | |
| 239 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | |
| 240 RequestCoordinator* request_coordinator = | |
| 241 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); | |
| 242 | |
| 243 request_coordinator->GetAllRequests( | |
| 244 base::Bind(&FilterRequestsByGuid, guid, | |
| 245 base::Bind(&PauseRequestsContinuation, request_coordinator))); | |
| 246 } | |
| 247 | |
| 248 void OfflinePageDownloadBridge::ResumeDownload( | |
| 249 JNIEnv* env, | |
| 250 const JavaParamRef<jobject>& obj, | |
| 251 const JavaParamRef<jstring>& j_guid) { | |
| 252 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | |
| 253 RequestCoordinator* request_coordinator = | |
| 254 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); | |
| 255 | |
| 256 request_coordinator->GetAllRequests( | |
| 257 base::Bind(&FilterRequestsByGuid, guid, | |
| 258 base::Bind(&ResumeRequestsContinuation, request_coordinator))); | |
| 259 } | |
| 260 | |
| 177 void OfflinePageDownloadBridge::ItemsLoaded() { | 261 void OfflinePageDownloadBridge::ItemsLoaded() { |
| 178 JNIEnv* env = AttachCurrentThread(); | 262 JNIEnv* env = AttachCurrentThread(); |
| 179 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 263 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 180 if (obj.is_null()) | 264 if (obj.is_null()) |
| 181 return; | 265 return; |
| 182 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj); | 266 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj); |
| 183 } | 267 } |
| 184 | 268 |
| 185 void OfflinePageDownloadBridge::ItemAdded(const DownloadUIItem& item) { | 269 void OfflinePageDownloadBridge::ItemAdded(const DownloadUIItem& item) { |
| 186 JNIEnv* env = AttachCurrentThread(); | 270 JNIEnv* env = AttachCurrentThread(); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 205 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 289 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 206 if (obj.is_null()) | 290 if (obj.is_null()) |
| 207 return; | 291 return; |
| 208 Java_OfflinePageDownloadBridge_downloadItemUpdated( | 292 Java_OfflinePageDownloadBridge_downloadItemUpdated( |
| 209 env, obj, ToJavaOfflinePageDownloadItem(env, item)); | 293 env, obj, ToJavaOfflinePageDownloadItem(env, item)); |
| 210 } | 294 } |
| 211 | 295 |
| 212 static jlong Init(JNIEnv* env, | 296 static jlong Init(JNIEnv* env, |
| 213 const JavaParamRef<jobject>& obj, | 297 const JavaParamRef<jobject>& obj, |
| 214 const JavaParamRef<jobject>& j_profile) { | 298 const JavaParamRef<jobject>& j_profile) { |
| 215 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 299 content::BrowserContext* browser_context = |
| 300 ProfileAndroid::FromProfileAndroid(j_profile); | |
| 301 | |
| 216 OfflinePageModel* offline_page_model = | 302 OfflinePageModel* offline_page_model = |
| 217 OfflinePageModelFactory::GetForBrowserContext(profile); | 303 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 218 | 304 |
| 219 DownloadUIAdapter* adapter = | 305 DownloadUIAdapter* adapter = |
| 220 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); | 306 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); |
| 221 | 307 |
| 222 return reinterpret_cast<jlong>( | 308 return reinterpret_cast<jlong>( |
| 223 new OfflinePageDownloadBridge(env, obj, adapter)); | 309 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); |
| 224 } | 310 } |
| 225 | 311 |
| 226 } // namespace android | 312 } // namespace android |
| 227 } // namespace offline_pages | 313 } // namespace offline_pages |
| OLD | NEW |