Chromium Code Reviews| Index: chrome/browser/android/offline_pages/downloads/offline_page_download_bridge.cc |
| diff --git a/chrome/browser/android/offline_pages/downloads/offline_page_download_bridge.cc b/chrome/browser/android/offline_pages/downloads/offline_page_download_bridge.cc |
| index c6227668269a875c5938e3f501c7820814f00979..aef7513440fbf3cc027d89d1fd1d1c6b836e5764 100644 |
| --- a/chrome/browser/android/offline_pages/downloads/offline_page_download_bridge.cc |
| +++ b/chrome/browser/android/offline_pages/downloads/offline_page_download_bridge.cc |
| @@ -56,6 +56,7 @@ void SavePageCallback(const DownloadUIItem& item, |
| notification_bridge.NotifyDownloadFailed(item); |
| } |
| +// TODO(dewittj): Move to Download UI Adapter. |
| content::WebContents* GetWebContentsFromJavaTab( |
| const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| JNIEnv* env = AttachCurrentThread(); |
| @@ -66,6 +67,20 @@ content::WebContents* GetWebContentsFromJavaTab( |
| return tab->web_contents(); |
| } |
| +// TODO(dewittj): Move to Download UI Adapter. |
| +OfflinePageModel* GetOfflinePageModelFromJavaTab( |
| + const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| + content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| + if (!web_contents) |
| + return nullptr; |
| + |
| + Profile* profile = |
| + Profile::FromBrowserContext(web_contents->GetBrowserContext()) |
| + ->GetOriginalProfile(); |
| + |
| + return OfflinePageModelFactory::GetForBrowserContext(profile); |
| +} |
| + |
| void SavePageIfNavigatedToURL(const GURL& query_url, |
| const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| @@ -120,14 +135,58 @@ void SavePageIfNavigatedToURL(const GURL& query_url, |
| item.guid = client_id.id; |
| item.url = url; |
| - OfflinePageNotificationBridge bridge; |
| - bridge.NotifyDownloadProgress(item); |
| + notification_bridge.NotifyDownloadProgress(item); |
| notification_bridge.ShowDownloadingToast(); |
| offline_page_model->SavePage(url, client_id, 0l, std::move(archiver), |
| base::Bind(&SavePageCallback, item)); |
| } |
| +void OnDeletePagesForInfoBar(const GURL& query_url, |
| + const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| + DeletePageResult result) { |
| + SavePageIfNavigatedToURL(query_url, j_tab_ref); |
| +} |
| + |
| +void DeletePagesForOverwrite(const GURL& query_url, |
| + const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| + const MultipleOfflinePageItemResult& pages) { |
| + OfflinePageModel* model = GetOfflinePageModelFromJavaTab(j_tab_ref); |
| + if (!model) |
| + return; |
| + |
| + std::vector<int64_t> offline_ids; |
| + for (auto& page : pages) { |
| + if (page.client_id.name_space == kDownloadNamespace || |
| + page.client_id.name_space == kAsyncNamespace) { |
| + offline_ids.emplace_back(page.offline_id); |
| + } |
| + } |
| + |
| + model->DeletePagesByOfflineId( |
| + offline_ids, base::Bind(&OnDeletePagesForInfoBar, query_url, j_tab_ref)); |
| +} |
| + |
| +void OnInfoBarAction(const GURL& query_url, |
| + const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| + OfflinePageInfoBarDelegate::Action action) { |
| + switch (action) { |
| + case OfflinePageInfoBarDelegate::Action::CREATE_NEW: |
| + SavePageIfNavigatedToURL(query_url, j_tab_ref); |
| + break; |
| + case OfflinePageInfoBarDelegate::Action::OVERWRITE: |
| + OfflinePageModel* offline_page_model = |
| + GetOfflinePageModelFromJavaTab(j_tab_ref); |
| + if (!offline_page_model) |
| + return; |
| + |
| + offline_page_model->GetPagesByOnlineURL( |
| + query_url, |
| + base::Bind(&DeletePagesForOverwrite, query_url, j_tab_ref)); |
| + break; |
| + } |
| +} |
| + |
| void RequestQueueDuplicateCheckDone( |
| const GURL& query_url, |
| const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| @@ -148,6 +207,7 @@ void RequestQueueDuplicateCheckDone( |
| void ModelDuplicateCheckDone(const GURL& query_url, |
| const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| + const std::string& downloads, |
|
qinmin
2016/10/11 21:05:06
nit: s/downloads/downloads_label/ ?
dewittj
2016/10/11 21:24:52
Done.
|
| bool has_duplicates) { |
| content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| if (!web_contents) |
| @@ -155,7 +215,7 @@ void ModelDuplicateCheckDone(const GURL& query_url, |
| if (has_duplicates) { |
| OfflinePageInfoBarDelegate::Create( |
| - base::Bind(&SavePageIfNavigatedToURL, query_url, j_tab_ref), |
| + base::Bind(&OnInfoBarAction, query_url, j_tab_ref), downloads, |
| query_url.spec(), web_contents); |
| return; |
| } |
| @@ -321,7 +381,9 @@ jlong OfflinePageDownloadBridge::GetOfflineIdByGuid( |
| void OfflinePageDownloadBridge::StartDownload( |
| JNIEnv* env, |
| const JavaParamRef<jobject>& obj, |
| - const JavaParamRef<jobject>& j_tab) { |
| + const JavaParamRef<jobject>& j_tab, |
| + const JavaParamRef<jstring>& j_downloads_label) { |
| + std::string downloads = ConvertJavaStringToUTF8(env, j_downloads_label); |
|
gone
2016/10/11 20:01:18
downloads_label? "downloads" is ambiguous.
dewittj
2016/10/11 21:24:52
Done.
|
| TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); |
| if (!tab) |
| return; |
| @@ -336,7 +398,7 @@ void OfflinePageDownloadBridge::StartDownload( |
| OfflinePageUtils::CheckExistenceOfPagesWithURL( |
| tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, |
| - base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref)); |
| + base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref, downloads)); |
| } |
| void OfflinePageDownloadBridge::CancelDownload( |