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; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 53 ScopedJavaLocalRef<jobject> ToJavaOfflinePageDownloadItem( | 59 ScopedJavaLocalRef<jobject> ToJavaOfflinePageDownloadItem( |
| 54 JNIEnv* env, | 60 JNIEnv* env, |
| 55 const DownloadUIItem& item) { | 61 const DownloadUIItem& item) { |
| 56 return Java_OfflinePageDownloadBridge_createDownloadItem( | 62 return Java_OfflinePageDownloadBridge_createDownloadItem( |
| 57 env, ConvertUTF8ToJavaString(env, item.guid), | 63 env, ConvertUTF8ToJavaString(env, item.guid), |
| 58 ConvertUTF8ToJavaString(env, item.url.spec()), | 64 ConvertUTF8ToJavaString(env, item.url.spec()), |
| 59 ConvertUTF16ToJavaString(env, item.title), | 65 ConvertUTF16ToJavaString(env, item.title), |
| 60 ConvertUTF8ToJavaString(env, item.target_path.value()), | 66 ConvertUTF8ToJavaString(env, item.target_path.value()), |
| 61 item.start_time.ToJavaTime(), item.total_bytes); | 67 item.start_time.ToJavaTime(), item.total_bytes); |
| 62 } | 68 } |
| 69 | |
| 70 std::vector<int64_t> FilterRequestsByGuid( | |
| 71 const std::vector<SavePageRequest>& requests, | |
| 72 const std::string& guid) { | |
| 73 std::vector<int64_t> request_ids; | |
| 74 for (const SavePageRequest& request : requests) { | |
| 75 if (request.client_id().id == guid && | |
| 76 (request.client_id().name_space == kDownloadNamespace || | |
| 77 request.client_id().name_space == kAsyncNamespace)) { | |
|
Pete Williamson
2016/08/25 18:30:41
Like DewittJ suggested elsewhere, should we have a
fgorski
2016/08/25 18:51:34
Correct.
crbug.com/641053
| |
| 78 request_ids.push_back(request.request_id()); | |
| 79 } | |
| 80 } | |
| 81 return request_ids; | |
| 82 } | |
| 83 | |
| 84 void CancelRequestCallback(const RequestQueue::UpdateMultipleRequestResults&) { | |
| 85 // Results ignored here, as UI uses observer to update itself. | |
| 86 } | |
| 87 | |
| 88 void CancelRequestsContinuation(content::BrowserContext* browser_context, | |
| 89 const std::string& guid, | |
| 90 const std::vector<SavePageRequest>& requests) { | |
| 91 RequestCoordinator* coordinator = | |
| 92 RequestCoordinatorFactory::GetForBrowserContext(browser_context); | |
| 93 if (coordinator) { | |
| 94 std::vector<int64_t> request_ids = FilterRequestsByGuid(requests, guid); | |
| 95 coordinator->RemoveRequests(request_ids, | |
| 96 base::Bind(&CancelRequestCallback)); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 void PauseRequestsContinuation(content::BrowserContext* browser_context, | |
| 101 const std::string& guid, | |
|
Pete Williamson
2016/08/25 18:30:41
nit - indentation off
fgorski
2016/08/25 18:51:34
Done.
| |
| 102 const std::vector<SavePageRequest>& requests) { | |
| 103 RequestCoordinator* coordinator = | |
| 104 RequestCoordinatorFactory::GetForBrowserContext(browser_context); | |
| 105 if (coordinator) | |
| 106 coordinator->PauseRequests(FilterRequestsByGuid(requests, guid)); | |
| 107 } | |
| 108 | |
| 109 void ResumeRequestsContinuation(content::BrowserContext* browser_context, | |
| 110 const std::string& guid, | |
| 111 const std::vector<SavePageRequest>& requests) { | |
| 112 RequestCoordinator* coordinator = | |
| 113 RequestCoordinatorFactory::GetForBrowserContext(browser_context); | |
| 114 if (coordinator) | |
| 115 coordinator->ResumeRequests(FilterRequestsByGuid(requests, guid)); | |
| 116 } | |
| 117 | |
| 63 } // namespace | 118 } // namespace |
| 64 | 119 |
| 65 OfflinePageDownloadBridge::OfflinePageDownloadBridge( | 120 OfflinePageDownloadBridge::OfflinePageDownloadBridge( |
| 66 JNIEnv* env, | 121 JNIEnv* env, |
| 67 const JavaParamRef<jobject>& obj, | 122 const JavaParamRef<jobject>& obj, |
| 68 DownloadUIAdapter* download_ui_adapter) | 123 DownloadUIAdapter* download_ui_adapter, |
| 124 content::BrowserContext* browser_context) | |
| 69 : weak_java_ref_(env, obj), | 125 : weak_java_ref_(env, obj), |
| 70 download_ui_adapter_(download_ui_adapter) { | 126 download_ui_adapter_(download_ui_adapter), |
| 127 browser_context_(browser_context) { | |
| 71 DCHECK(download_ui_adapter_); | 128 DCHECK(download_ui_adapter_); |
| 72 download_ui_adapter_->AddObserver(this); | 129 download_ui_adapter_->AddObserver(this); |
| 73 } | 130 } |
| 74 | 131 |
| 75 OfflinePageDownloadBridge::~OfflinePageDownloadBridge() {} | 132 OfflinePageDownloadBridge::~OfflinePageDownloadBridge() {} |
| 76 | 133 |
| 77 // static | 134 // static |
| 78 bool OfflinePageDownloadBridge::Register(JNIEnv* env) { | 135 bool OfflinePageDownloadBridge::Register(JNIEnv* env) { |
| 79 return RegisterNativesImpl(env); | 136 return RegisterNativesImpl(env); |
| 80 } | 137 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); | 200 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); |
| 144 if (!tab) | 201 if (!tab) |
| 145 return; | 202 return; |
| 146 | 203 |
| 147 content::WebContents* web_contents = tab->web_contents(); | 204 content::WebContents* web_contents = tab->web_contents(); |
| 148 if (!web_contents) | 205 if (!web_contents) |
| 149 return; | 206 return; |
| 150 | 207 |
| 151 offline_pages::OfflinePageModel* offline_page_model = | 208 offline_pages::OfflinePageModel* offline_page_model = |
| 152 OfflinePageModelFactory::GetForBrowserContext( | 209 OfflinePageModelFactory::GetForBrowserContext( |
| 153 tab->GetProfile()->GetOriginalProfile()); | 210 tab->GetProfile()->GetOriginalProfile()); |
| 154 if (!offline_page_model) | 211 if (!offline_page_model) |
| 155 return; | 212 return; |
| 156 | 213 |
| 157 GURL url = web_contents->GetLastCommittedURL(); | 214 GURL url = web_contents->GetLastCommittedURL(); |
| 158 auto archiver = | 215 auto archiver = |
| 159 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); | 216 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); |
| 160 | 217 |
| 161 offline_pages::ClientId client_id; | 218 offline_pages::ClientId client_id; |
| 162 client_id.name_space = offline_pages::kDownloadNamespace; | 219 client_id.name_space = offline_pages::kDownloadNamespace; |
| 163 client_id.id = base::GenerateGUID(); | 220 client_id.id = base::GenerateGUID(); |
| 164 | 221 |
| 165 DownloadUIItem item; | 222 DownloadUIItem item; |
| 166 item.guid = client_id.id; | 223 item.guid = client_id.id; |
| 167 item.url = url; | 224 item.url = url; |
| 168 | 225 |
| 169 OfflinePageNotificationBridge bridge; | 226 OfflinePageNotificationBridge bridge; |
| 170 bridge.NotifyDownloadProgress(item); | 227 bridge.NotifyDownloadProgress(item); |
| 171 | 228 |
| 172 offline_page_model->SavePage( | 229 offline_page_model->SavePage( |
| 173 url, client_id, 0ul, std::move(archiver), | 230 url, client_id, 0ul, std::move(archiver), |
| 174 base::Bind(&OfflinePageDownloadBridge::SavePageCallback, item)); | 231 base::Bind(&OfflinePageDownloadBridge::SavePageCallback, item)); |
| 175 } | 232 } |
| 176 | 233 |
| 234 void OfflinePageDownloadBridge::CancelDownload( | |
| 235 JNIEnv* env, | |
| 236 const JavaParamRef<jobject>& obj, | |
| 237 const JavaParamRef<jstring>& j_guid) { | |
| 238 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | |
| 239 RequestCoordinator* request_coordinator = | |
| 240 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); | |
| 241 | |
| 242 if (request_coordinator) { | |
|
Pete Williamson
2016/08/25 18:30:41
Should we log a warning if RC is not found? (Here
fgorski
2016/08/25 18:51:34
Done.
| |
| 243 request_coordinator->GetAllRequests( | |
| 244 base::Bind(&CancelRequestsContinuation, browser_context_, guid)); | |
| 245 } | |
| 246 } | |
| 247 | |
| 248 void OfflinePageDownloadBridge::PauseDownload( | |
| 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 if (request_coordinator) { | |
| 257 request_coordinator->GetAllRequests( | |
| 258 base::Bind(&PauseRequestsContinuation, browser_context_, guid)); | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 void OfflinePageDownloadBridge::ResumeDownload( | |
| 263 JNIEnv* env, | |
| 264 const JavaParamRef<jobject>& obj, | |
| 265 const JavaParamRef<jstring>& j_guid) { | |
| 266 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | |
| 267 RequestCoordinator* request_coordinator = | |
| 268 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); | |
| 269 | |
| 270 if (request_coordinator) { | |
| 271 request_coordinator->GetAllRequests( | |
| 272 base::Bind(&ResumeRequestsContinuation, browser_context_, guid)); | |
| 273 } | |
| 274 } | |
| 275 | |
| 177 void OfflinePageDownloadBridge::ItemsLoaded() { | 276 void OfflinePageDownloadBridge::ItemsLoaded() { |
| 178 JNIEnv* env = AttachCurrentThread(); | 277 JNIEnv* env = AttachCurrentThread(); |
| 179 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 278 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 180 if (obj.is_null()) | 279 if (obj.is_null()) |
| 181 return; | 280 return; |
| 182 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj); | 281 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj); |
| 183 } | 282 } |
| 184 | 283 |
| 185 void OfflinePageDownloadBridge::ItemAdded(const DownloadUIItem& item) { | 284 void OfflinePageDownloadBridge::ItemAdded(const DownloadUIItem& item) { |
| 186 JNIEnv* env = AttachCurrentThread(); | 285 JNIEnv* env = AttachCurrentThread(); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 205 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 304 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 206 if (obj.is_null()) | 305 if (obj.is_null()) |
| 207 return; | 306 return; |
| 208 Java_OfflinePageDownloadBridge_downloadItemUpdated( | 307 Java_OfflinePageDownloadBridge_downloadItemUpdated( |
| 209 env, obj, ToJavaOfflinePageDownloadItem(env, item)); | 308 env, obj, ToJavaOfflinePageDownloadItem(env, item)); |
| 210 } | 309 } |
| 211 | 310 |
| 212 static jlong Init(JNIEnv* env, | 311 static jlong Init(JNIEnv* env, |
| 213 const JavaParamRef<jobject>& obj, | 312 const JavaParamRef<jobject>& obj, |
| 214 const JavaParamRef<jobject>& j_profile) { | 313 const JavaParamRef<jobject>& j_profile) { |
| 215 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 314 content::BrowserContext* browser_context = |
| 315 ProfileAndroid::FromProfileAndroid(j_profile); | |
| 316 | |
| 216 OfflinePageModel* offline_page_model = | 317 OfflinePageModel* offline_page_model = |
| 217 OfflinePageModelFactory::GetForBrowserContext(profile); | 318 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 218 | 319 |
| 219 DownloadUIAdapter* adapter = | 320 DownloadUIAdapter* adapter = |
| 220 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); | 321 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); |
| 221 | 322 |
| 222 return reinterpret_cast<jlong>( | 323 return reinterpret_cast<jlong>( |
| 223 new OfflinePageDownloadBridge(env, obj, adapter)); | 324 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); |
| 224 } | 325 } |
| 225 | 326 |
| 226 } // namespace android | 327 } // namespace android |
| 227 } // namespace offline_pages | 328 } // namespace offline_pages |
| OLD | NEW |