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" | 10 #include "base/bind.h" |
| 11 #include "base/debug/stack_trace.h" | |
|
fgorski
2016/10/10 18:08:20
Do we need this or was it for debugging?
dewittj
2016/10/10 18:37:02
Removed.
| |
| 11 #include "base/guid.h" | 12 #include "base/guid.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 14 #include "chrome/browser/android/offline_pages/downloads/offline_page_infobar_de legate.h" | 15 #include "chrome/browser/android/offline_pages/downloads/offline_page_infobar_de legate.h" |
| 15 #include "chrome/browser/android/offline_pages/downloads/offline_page_notificati on_bridge.h" | 16 #include "chrome/browser/android/offline_pages/downloads/offline_page_notificati on_bridge.h" |
| 16 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 17 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 17 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 18 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 18 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | 19 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 19 #include "chrome/browser/android/offline_pages/recent_tab_helper.h" | 20 #include "chrome/browser/android/offline_pages/recent_tab_helper.h" |
| 20 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | 21 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 content::WebContents* GetWebContentsFromJavaTab( | 60 content::WebContents* GetWebContentsFromJavaTab( |
| 60 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | 61 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| 61 JNIEnv* env = AttachCurrentThread(); | 62 JNIEnv* env = AttachCurrentThread(); |
| 62 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab_ref); | 63 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab_ref); |
| 63 if (!tab) | 64 if (!tab) |
| 64 return nullptr; | 65 return nullptr; |
| 65 | 66 |
| 66 return tab->web_contents(); | 67 return tab->web_contents(); |
| 67 } | 68 } |
| 68 | 69 |
| 70 OfflinePageModel* GetOfflinePageModelFromJavaTab( | |
|
fgorski
2016/10/10 18:08:20
General comment about the code in this file. I thi
dewittj
2016/10/10 18:37:02
Acknowledged.
| |
| 71 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | |
| 72 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | |
| 73 if (!web_contents) | |
| 74 return nullptr; | |
| 75 | |
| 76 Profile* profile = | |
| 77 Profile::FromBrowserContext(web_contents->GetBrowserContext()) | |
| 78 ->GetOriginalProfile(); | |
| 79 | |
| 80 return OfflinePageModelFactory::GetForBrowserContext(profile); | |
| 81 } | |
| 82 | |
| 69 void SavePageIfNavigatedToURL(const GURL& query_url, | 83 void SavePageIfNavigatedToURL(const GURL& query_url, |
| 70 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | 84 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| 71 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | 85 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| 72 if (!web_contents) | 86 if (!web_contents) |
| 73 return; | 87 return; |
| 74 | 88 |
| 75 // This doesn't detect navigations to the same URL, only that we are looking | 89 // This doesn't detect navigations to the same URL, only that we are looking |
| 76 // at a completely different page. | 90 // at a completely different page. |
| 77 GURL url = web_contents->GetLastCommittedURL(); | 91 GURL url = web_contents->GetLastCommittedURL(); |
| 78 if (!OfflinePageUtils::EqualsIgnoringFragment(url, query_url)) | 92 if (!OfflinePageUtils::EqualsIgnoringFragment(url, query_url)) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 if (!offline_page_model) | 127 if (!offline_page_model) |
| 114 return; | 128 return; |
| 115 | 129 |
| 116 auto archiver = | 130 auto archiver = |
| 117 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); | 131 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); |
| 118 | 132 |
| 119 DownloadUIItem item; | 133 DownloadUIItem item; |
| 120 item.guid = client_id.id; | 134 item.guid = client_id.id; |
| 121 item.url = url; | 135 item.url = url; |
| 122 | 136 |
| 123 OfflinePageNotificationBridge bridge; | 137 notification_bridge.NotifyDownloadProgress(item); |
|
fgorski
2016/10/10 18:08:21
Good catch.
dewittj
2016/10/10 18:37:03
Acknowledged.
| |
| 124 bridge.NotifyDownloadProgress(item); | |
| 125 | 138 |
| 126 notification_bridge.ShowDownloadingToast(); | 139 notification_bridge.ShowDownloadingToast(); |
| 127 offline_page_model->SavePage(url, client_id, 0l, std::move(archiver), | 140 offline_page_model->SavePage(url, client_id, 0l, std::move(archiver), |
| 128 base::Bind(&SavePageCallback, item)); | 141 base::Bind(&SavePageCallback, item)); |
| 129 } | 142 } |
| 130 | 143 |
| 144 void OnDeletePagesForInfoBar(const GURL& query_url, | |
| 145 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | |
| 146 DeletePageResult result) { | |
| 147 SavePageIfNavigatedToURL(query_url, j_tab_ref); | |
| 148 } | |
| 149 | |
| 150 void OnGetPagesForDeleteDone(const GURL& query_url, | |
|
fgorski
2016/10/10 18:08:20
nit: How about naming it something related to what
dewittj
2016/10/10 18:37:03
how about DeletePagesForOverwrite?
| |
| 151 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | |
| 152 const MultipleOfflinePageItemResult& pages) { | |
| 153 OfflinePageModel* model = GetOfflinePageModelFromJavaTab(j_tab_ref); | |
| 154 if (!model) | |
| 155 return; | |
| 156 | |
| 157 std::vector<int64_t> offline_ids; | |
| 158 for (auto& page : pages) { | |
| 159 if (page.client_id.name_space == kDownloadNamespace || | |
| 160 page.client_id.name_space == kAsyncNamespace) { | |
| 161 offline_ids.emplace_back(page.offline_id); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 model->DeletePagesByOfflineId( | |
| 166 offline_ids, base::Bind(&OnDeletePagesForInfoBar, query_url, j_tab_ref)); | |
|
fgorski
2016/10/10 18:08:21
nit: In the spirit of the above nit, why not simpl
dewittj
2016/10/10 18:37:02
Wanted that, but there is an extra parameter and I
| |
| 167 } | |
| 168 | |
| 169 void OnInfoBarAction(const GURL& query_url, | |
| 170 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | |
| 171 OfflinePageInfoBarDelegate::Action action) { | |
| 172 if (action == OfflinePageInfoBarDelegate::Action::CONFIRM) { | |
| 173 SavePageIfNavigatedToURL(query_url, j_tab_ref); | |
| 174 return; | |
| 175 } | |
| 176 | |
| 177 OfflinePageModel* offline_page_model = | |
| 178 GetOfflinePageModelFromJavaTab(j_tab_ref); | |
| 179 if (!offline_page_model) | |
|
fgorski
2016/10/10 18:08:21
we should probably discuss with PMs the fact that
dewittj
2016/10/10 18:37:02
I don't really expect this to be possible, since w
| |
| 180 return; | |
| 181 | |
|
fgorski
2016/10/10 18:08:20
please add a dcheck that the action is OVERWRITE a
dewittj
2016/10/10 18:37:02
Done (used switch for better compile-time checking
| |
| 182 offline_page_model->GetPagesByOnlineURL( | |
| 183 query_url, base::Bind(&OnGetPagesForDeleteDone, query_url, j_tab_ref)); | |
| 184 } | |
| 185 | |
| 131 void RequestQueueDuplicateCheckDone( | 186 void RequestQueueDuplicateCheckDone( |
| 132 const GURL& query_url, | 187 const GURL& query_url, |
| 133 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | 188 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 134 bool has_duplicates) { | 189 bool has_duplicates) { |
| 135 if (has_duplicates) { | 190 if (has_duplicates) { |
| 136 // TODO(fgorski): Additionally we could update existing request's expiration | 191 // TODO(fgorski): Additionally we could update existing request's expiration |
| 137 // period, as it is still important. Alternative would be to actually take a | 192 // period, as it is still important. Alternative would be to actually take a |
| 138 // snapshot on the spot, but that would only work if the page is loaded | 193 // snapshot on the spot, but that would only work if the page is loaded |
| 139 // enough. | 194 // enough. |
| 140 // This simply toasts that the item is downloading. | 195 // This simply toasts that the item is downloading. |
| 141 OfflinePageNotificationBridge notification_bridge; | 196 OfflinePageNotificationBridge notification_bridge; |
| 142 notification_bridge.ShowDownloadingToast(); | 197 notification_bridge.ShowDownloadingToast(); |
| 143 return; | 198 return; |
| 144 } | 199 } |
| 145 | 200 |
| 146 SavePageIfNavigatedToURL(query_url, j_tab_ref); | 201 SavePageIfNavigatedToURL(query_url, j_tab_ref); |
| 147 } | 202 } |
| 148 | 203 |
| 149 void ModelDuplicateCheckDone(const GURL& query_url, | 204 void ModelDuplicateCheckDone(const GURL& query_url, |
| 150 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | 205 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 206 const std::string& downloads, | |
| 151 bool has_duplicates) { | 207 bool has_duplicates) { |
| 152 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | 208 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| 153 if (!web_contents) | 209 if (!web_contents) |
| 154 return; | 210 return; |
| 155 | 211 |
| 156 if (has_duplicates) { | 212 if (has_duplicates) { |
| 157 OfflinePageInfoBarDelegate::Create( | 213 OfflinePageInfoBarDelegate::Create( |
| 158 base::Bind(&SavePageIfNavigatedToURL, query_url, j_tab_ref), | 214 base::Bind(&OnInfoBarAction, query_url, j_tab_ref), downloads, |
| 159 query_url.spec(), web_contents); | 215 query_url.spec(), web_contents); |
| 160 return; | 216 return; |
| 161 } | 217 } |
| 162 | 218 |
| 163 OfflinePageUtils::CheckExistenceOfRequestsWithURL( | 219 OfflinePageUtils::CheckExistenceOfRequestsWithURL( |
| 164 Profile::FromBrowserContext(web_contents->GetBrowserContext()) | 220 Profile::FromBrowserContext(web_contents->GetBrowserContext()) |
| 165 ->GetOriginalProfile(), | 221 ->GetOriginalProfile(), |
| 166 kDownloadNamespace, query_url, | 222 kDownloadNamespace, query_url, |
| 167 base::Bind(&RequestQueueDuplicateCheckDone, query_url, j_tab_ref)); | 223 base::Bind(&RequestQueueDuplicateCheckDone, query_url, j_tab_ref)); |
| 168 } | 224 } |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 JNIEnv* env, | 370 JNIEnv* env, |
| 315 const JavaParamRef<jobject>& obj, | 371 const JavaParamRef<jobject>& obj, |
| 316 const JavaParamRef<jstring>& j_guid) { | 372 const JavaParamRef<jstring>& j_guid) { |
| 317 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | 373 std::string guid = ConvertJavaStringToUTF8(env, j_guid); |
| 318 return download_ui_adapter_->GetOfflineIdByGuid(guid); | 374 return download_ui_adapter_->GetOfflineIdByGuid(guid); |
| 319 } | 375 } |
| 320 | 376 |
| 321 void OfflinePageDownloadBridge::StartDownload( | 377 void OfflinePageDownloadBridge::StartDownload( |
| 322 JNIEnv* env, | 378 JNIEnv* env, |
| 323 const JavaParamRef<jobject>& obj, | 379 const JavaParamRef<jobject>& obj, |
| 324 const JavaParamRef<jobject>& j_tab) { | 380 const JavaParamRef<jobject>& j_tab, |
| 381 const JavaParamRef<jstring>& j_downloads_string) { | |
| 382 std::string downloads = ConvertJavaStringToUTF8(env, j_downloads_string); | |
| 325 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); | 383 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); |
| 326 if (!tab) | 384 if (!tab) |
| 327 return; | 385 return; |
| 328 | 386 |
| 329 content::WebContents* web_contents = tab->web_contents(); | 387 content::WebContents* web_contents = tab->web_contents(); |
| 330 if (!web_contents) | 388 if (!web_contents) |
| 331 return; | 389 return; |
| 332 | 390 |
| 333 GURL url = web_contents->GetLastCommittedURL(); | 391 GURL url = web_contents->GetLastCommittedURL(); |
| 334 | 392 |
| 335 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab); | 393 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab); |
| 336 | 394 |
| 337 OfflinePageUtils::CheckExistenceOfPagesWithURL( | 395 OfflinePageUtils::CheckExistenceOfPagesWithURL( |
| 338 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, | 396 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, |
| 339 base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref)); | 397 base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref, downloads)); |
| 340 } | 398 } |
| 341 | 399 |
| 342 void OfflinePageDownloadBridge::CancelDownload( | 400 void OfflinePageDownloadBridge::CancelDownload( |
| 343 JNIEnv* env, | 401 JNIEnv* env, |
| 344 const JavaParamRef<jobject>& obj, | 402 const JavaParamRef<jobject>& obj, |
| 345 const JavaParamRef<jstring>& j_guid) { | 403 const JavaParamRef<jstring>& j_guid) { |
| 346 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | 404 std::string guid = ConvertJavaStringToUTF8(env, j_guid); |
| 347 RequestCoordinator* coordinator = | 405 RequestCoordinator* coordinator = |
| 348 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); | 406 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); |
| 349 | 407 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 433 | 491 |
| 434 DownloadUIAdapter* adapter = | 492 DownloadUIAdapter* adapter = |
| 435 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); | 493 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); |
| 436 | 494 |
| 437 return reinterpret_cast<jlong>( | 495 return reinterpret_cast<jlong>( |
| 438 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); | 496 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); |
| 439 } | 497 } |
| 440 | 498 |
| 441 } // namespace android | 499 } // namespace android |
| 442 } // namespace offline_pages | 500 } // namespace offline_pages |
| OLD | NEW |