Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: chrome/browser/android/offline_pages/downloads/offline_page_download_bridge.cc

Issue 2412823002: Improve the page download: (Closed)
Patch Set: merge Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 if (!web_contents) 74 if (!web_contents)
75 return nullptr; 75 return nullptr;
76 76
77 Profile* profile = 77 Profile* profile =
78 Profile::FromBrowserContext(web_contents->GetBrowserContext()) 78 Profile::FromBrowserContext(web_contents->GetBrowserContext())
79 ->GetOriginalProfile(); 79 ->GetOriginalProfile();
80 80
81 return OfflinePageModelFactory::GetForBrowserContext(profile); 81 return OfflinePageModelFactory::GetForBrowserContext(profile);
82 } 82 }
83 83
84 void SavePageIfNavigatedToURL(const GURL& query_url, 84 void SavePageIfStillOnSamePage(const GURL& original_url,
dewittj 2016/10/12 03:27:27 Page is not really right here, that's why I referr
Dmitry Titov 2016/10/12 23:11:14 "SamePage" is more established than "SameURL". For
dewittj 2016/10/13 17:25:37 Acknowledged.
85 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { 85 const ScopedJavaGlobalRef<jobject>& j_tab_ref) {
86 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); 86 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref);
87 if (!web_contents) 87 if (!web_contents)
88 return; 88 return;
89 89
90 // This doesn't detect navigations to the same URL, only that we are looking 90 // This ignores fragment differences in URLs, bails out only if tab has
91 // at a completely different page. 91 // navigated to a completely different page.
dewittj 2016/10/12 03:27:27 My comment here is not internally consistent, my p
Dmitry Titov 2016/10/12 23:11:14 Same as above.
92 GURL url = web_contents->GetLastCommittedURL(); 92 GURL url = web_contents->GetLastCommittedURL();
93 if (!OfflinePageUtils::EqualsIgnoringFragment(url, query_url)) 93 if (!OfflinePageUtils::EqualsIgnoringFragment(url, original_url))
94 return; 94 return;
95 95
96 offline_pages::ClientId client_id; 96 offline_pages::ClientId client_id;
97 client_id.name_space = offline_pages::kDownloadNamespace; 97 client_id.name_space = offline_pages::kDownloadNamespace;
98 client_id.id = base::GenerateGUID(); 98 client_id.id = base::GenerateGUID();
99 int64_t request_id = 0;
dewittj 2016/10/12 03:27:27 this default is suspicious. Needs at least a comm
Dmitry Titov 2016/10/12 23:11:14 Done.
99 100
100 Profile* profile = 101 if (offline_pages::IsBackgroundLoaderForDownloadsEnabled()) {
101 Profile::FromBrowserContext(web_contents->GetBrowserContext()) 102 // Post disabled request before passing the download task to the tab helper.
102 ->GetOriginalProfile(); 103 // This will keep the request persisted in case Chrome is evicted from RAM
104 // or closed by the user.
105 offline_pages::RequestCoordinator* request_coordinator =
106 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(
107 web_contents->GetBrowserContext());
108 request_id = request_coordinator->SavePageLater(
109 url, client_id, true,
110 RequestCoordinator::RequestAvailability::DISABLED_FOR_OFFLINER);
dewittj 2016/10/12 03:27:27 The comment above doesn't mention that the request
Dmitry Titov 2016/10/12 23:11:14 Done.
111 }
112
113 // Pass request_id to the current tab's helper to attempt download right from
114 // the tab. If unsuccessful, it'll enable the already-queued request for
115 // background offliner. Same will happen if Chrome is terminated since
116 // 'disabled' status of the request is RAM-stored info.
117 offline_pages::RecentTabHelper* tab_helper =
118 RecentTabHelper::FromWebContents(web_contents);
119 if (!tab_helper)
dewittj 2016/10/12 03:27:27 If no tab helper then perhaps this code should re-
Dmitry Titov 2016/10/12 23:11:14 Done.
120 return;
121 tab_helper->ObserveAndDownloadCurrentPage(client_id, request_id);
103 122
104 OfflinePageNotificationBridge notification_bridge; 123 OfflinePageNotificationBridge notification_bridge;
105
106 // If the page is not loaded enough to be captured, submit a background loader
107 // request instead.
108 offline_pages::RecentTabHelper* tab_helper =
109 RecentTabHelper::FromWebContents(web_contents);
110 if (tab_helper && !tab_helper->is_page_ready_for_snapshot() &&
111 offline_pages::IsBackgroundLoaderForDownloadsEnabled()) {
112 // TODO(dimich): Improve this to wait for the page load if it is still going
113 // on. Pre-submit the request and if the load finishes and capture happens,
114 // remove request.
115 offline_pages::RequestCoordinator* request_coordinator =
116 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile);
117 request_coordinator->SavePageLater(
118 url, client_id, true,
119 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER);
120
121 notification_bridge.ShowDownloadingToast();
122 return;
123 }
124
125 // Page is ready, capture it right from the tab.
126 offline_pages::OfflinePageModel* offline_page_model =
127 OfflinePageModelFactory::GetForBrowserContext(profile);
128 if (!offline_page_model)
129 return;
130
131 auto archiver =
132 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents);
133
134 DownloadUIItem item;
135 item.guid = client_id.id;
136 item.url = url;
137
138 notification_bridge.NotifyDownloadProgress(item);
139
140 notification_bridge.ShowDownloadingToast(); 124 notification_bridge.ShowDownloadingToast();
141 offline_page_model->SavePage(url, client_id, 0l, std::move(archiver),
142 base::Bind(&SavePageCallback, item));
143 } 125 }
144 126
145 void OnDeletePagesForInfoBar(const GURL& query_url, 127 void OnDeletePagesForInfoBar(const GURL& original_url,
146 const ScopedJavaGlobalRef<jobject>& j_tab_ref, 128 const ScopedJavaGlobalRef<jobject>& j_tab_ref,
147 DeletePageResult result) { 129 DeletePageResult result) {
148 SavePageIfNavigatedToURL(query_url, j_tab_ref); 130 SavePageIfStillOnSamePage(original_url, j_tab_ref);
149 } 131 }
150 132
151 void DeletePagesForOverwrite(const GURL& query_url, 133 void DeletePagesForOverwrite(const GURL& original_url,
152 const ScopedJavaGlobalRef<jobject>& j_tab_ref, 134 const ScopedJavaGlobalRef<jobject>& j_tab_ref,
153 const MultipleOfflinePageItemResult& pages) { 135 const MultipleOfflinePageItemResult& pages) {
154 OfflinePageModel* model = GetOfflinePageModelFromJavaTab(j_tab_ref); 136 OfflinePageModel* model = GetOfflinePageModelFromJavaTab(j_tab_ref);
155 if (!model) 137 if (!model)
156 return; 138 return;
157 139
158 std::vector<int64_t> offline_ids; 140 std::vector<int64_t> offline_ids;
159 for (auto& page : pages) { 141 for (auto& page : pages) {
160 if (page.client_id.name_space == kDownloadNamespace || 142 if (page.client_id.name_space == kDownloadNamespace ||
161 page.client_id.name_space == kAsyncNamespace) { 143 page.client_id.name_space == kAsyncNamespace) {
162 offline_ids.emplace_back(page.offline_id); 144 offline_ids.emplace_back(page.offline_id);
163 } 145 }
164 } 146 }
165 147
166 model->DeletePagesByOfflineId( 148 model->DeletePagesByOfflineId(
167 offline_ids, base::Bind(&OnDeletePagesForInfoBar, query_url, j_tab_ref)); 149 offline_ids, base::Bind(
150 &OnDeletePagesForInfoBar, original_url, j_tab_ref));
168 } 151 }
169 152
170 void OnInfoBarAction(const GURL& query_url, 153 void OnInfoBarAction(const GURL& original_url,
171 const ScopedJavaGlobalRef<jobject>& j_tab_ref, 154 const ScopedJavaGlobalRef<jobject>& j_tab_ref,
172 OfflinePageInfoBarDelegate::Action action) { 155 OfflinePageInfoBarDelegate::Action action) {
173 switch (action) { 156 switch (action) {
174 case OfflinePageInfoBarDelegate::Action::CREATE_NEW: 157 case OfflinePageInfoBarDelegate::Action::CREATE_NEW:
175 SavePageIfNavigatedToURL(query_url, j_tab_ref); 158 SavePageIfStillOnSamePage(original_url, j_tab_ref);
176 break; 159 break;
177 case OfflinePageInfoBarDelegate::Action::OVERWRITE: 160 case OfflinePageInfoBarDelegate::Action::OVERWRITE:
178 OfflinePageModel* offline_page_model = 161 OfflinePageModel* offline_page_model =
179 GetOfflinePageModelFromJavaTab(j_tab_ref); 162 GetOfflinePageModelFromJavaTab(j_tab_ref);
180 if (!offline_page_model) 163 if (!offline_page_model)
181 return; 164 return;
182 165
183 offline_page_model->GetPagesByOnlineURL( 166 offline_page_model->GetPagesByOnlineURL(
184 query_url, 167 original_url,
185 base::Bind(&DeletePagesForOverwrite, query_url, j_tab_ref)); 168 base::Bind(&DeletePagesForOverwrite, original_url, j_tab_ref));
186 break; 169 break;
187 } 170 }
188 } 171 }
189 172
190 void RequestQueueDuplicateCheckDone( 173 void RequestQueueDuplicateCheckDone(
191 const GURL& query_url, 174 const GURL& original_url,
192 const ScopedJavaGlobalRef<jobject>& j_tab_ref, 175 const ScopedJavaGlobalRef<jobject>& j_tab_ref,
193 bool has_duplicates) { 176 bool has_duplicates) {
194 if (has_duplicates) { 177 if (has_duplicates) {
195 // TODO(fgorski): Additionally we could update existing request's expiration 178 // TODO(fgorski): Additionally we could update existing request's expiration
196 // period, as it is still important. Alternative would be to actually take a 179 // period, as it is still important. Alternative would be to actually take a
197 // snapshot on the spot, but that would only work if the page is loaded 180 // snapshot on the spot, but that would only work if the page is loaded
198 // enough. 181 // enough.
199 // This simply toasts that the item is downloading. 182 // This simply toasts that the item is downloading.
200 OfflinePageNotificationBridge notification_bridge; 183 OfflinePageNotificationBridge notification_bridge;
201 notification_bridge.ShowDownloadingToast(); 184 notification_bridge.ShowDownloadingToast();
202 return; 185 return;
203 } 186 }
204 187
205 SavePageIfNavigatedToURL(query_url, j_tab_ref); 188 SavePageIfStillOnSamePage(original_url, j_tab_ref);
206 } 189 }
207 190
208 void ModelDuplicateCheckDone(const GURL& query_url, 191 void ModelDuplicateCheckDone(const GURL& original_url,
209 const ScopedJavaGlobalRef<jobject>& j_tab_ref, 192 const ScopedJavaGlobalRef<jobject>& j_tab_ref,
210 const std::string& downloads_label, 193 const std::string& downloads_label,
211 bool has_duplicates) { 194 bool has_duplicates) {
212 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); 195 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref);
213 if (!web_contents) 196 if (!web_contents)
214 return; 197 return;
215 198
216 if (has_duplicates) { 199 if (has_duplicates) {
217 OfflinePageInfoBarDelegate::Create( 200 OfflinePageInfoBarDelegate::Create(
218 base::Bind(&OnInfoBarAction, query_url, j_tab_ref), downloads_label, 201 base::Bind(&OnInfoBarAction, original_url, j_tab_ref), downloads_label,
219 query_url.spec(), web_contents); 202 original_url.spec(), web_contents);
220 return; 203 return;
221 } 204 }
222 205
223 OfflinePageUtils::CheckExistenceOfRequestsWithURL( 206 OfflinePageUtils::CheckExistenceOfRequestsWithURL(
224 Profile::FromBrowserContext(web_contents->GetBrowserContext()) 207 Profile::FromBrowserContext(web_contents->GetBrowserContext())
225 ->GetOriginalProfile(), 208 ->GetOriginalProfile(),
226 kDownloadNamespace, query_url, 209 kDownloadNamespace, original_url,
227 base::Bind(&RequestQueueDuplicateCheckDone, query_url, j_tab_ref)); 210 base::Bind(&RequestQueueDuplicateCheckDone, original_url, j_tab_ref));
228 } 211 }
229 212
230 void ToJavaOfflinePageDownloadItemList( 213 void ToJavaOfflinePageDownloadItemList(
231 JNIEnv* env, 214 JNIEnv* env,
232 jobject j_result_obj, 215 jobject j_result_obj,
233 const std::vector<const DownloadUIItem*>& items) { 216 const std::vector<const DownloadUIItem*>& items) {
234 for (const auto item : items) { 217 for (const auto item : items) {
235 Java_OfflinePageDownloadBridge_createDownloadItemAndAddToList( 218 Java_OfflinePageDownloadBridge_createDownloadItemAndAddToList(
236 env, j_result_obj, ConvertUTF8ToJavaString(env, item->guid), 219 env, j_result_obj, ConvertUTF8ToJavaString(env, item->guid),
237 ConvertUTF8ToJavaString(env, item->url.spec()), 220 ConvertUTF8ToJavaString(env, item->url.spec()),
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 std::string downloads_label = ConvertJavaStringToUTF8(env, j_downloads_label); 369 std::string downloads_label = ConvertJavaStringToUTF8(env, j_downloads_label);
387 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); 370 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab);
388 if (!tab) 371 if (!tab)
389 return; 372 return;
390 373
391 content::WebContents* web_contents = tab->web_contents(); 374 content::WebContents* web_contents = tab->web_contents();
392 if (!web_contents) 375 if (!web_contents)
393 return; 376 return;
394 377
395 GURL url = web_contents->GetLastCommittedURL(); 378 GURL url = web_contents->GetLastCommittedURL();
396
397 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab); 379 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab);
398 380
dewittj 2016/10/12 03:27:27 if you're deleting blank lines may as well also de
Dmitry Titov 2016/10/12 23:11:14 Restoring blank line, wasnt' deliberate.
399 OfflinePageUtils::CheckExistenceOfPagesWithURL( 381 OfflinePageUtils::CheckExistenceOfPagesWithURL(
400 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, 382 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url,
401 base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref, downloads_label)); 383 base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref, downloads_label));
402 } 384 }
403 385
404 void OfflinePageDownloadBridge::CancelDownload( 386 void OfflinePageDownloadBridge::CancelDownload(
405 JNIEnv* env, 387 JNIEnv* env,
406 const JavaParamRef<jobject>& obj, 388 const JavaParamRef<jobject>& obj,
407 const JavaParamRef<jstring>& j_guid) { 389 const JavaParamRef<jstring>& j_guid) {
408 std::string guid = ConvertJavaStringToUTF8(env, j_guid); 390 std::string guid = ConvertJavaStringToUTF8(env, j_guid);
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 477
496 DownloadUIAdapter* adapter = 478 DownloadUIAdapter* adapter =
497 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); 479 DownloadUIAdapter::FromOfflinePageModel(offline_page_model);
498 480
499 return reinterpret_cast<jlong>( 481 return reinterpret_cast<jlong>(
500 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); 482 new OfflinePageDownloadBridge(env, obj, adapter, browser_context));
501 } 483 }
502 484
503 } // namespace android 485 } // namespace android
504 } // namespace offline_pages 486 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698