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/offline_page_request_job.h" | 5 #include "chrome/browser/android/offline_pages/offline_page_request_job.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 | 211 |
| 212 void NotifyOfflineFilePathOnIO(base::WeakPtr<OfflinePageRequestJob> job, | 212 void NotifyOfflineFilePathOnIO(base::WeakPtr<OfflinePageRequestJob> job, |
| 213 const base::FilePath& offline_file_path) { | 213 const base::FilePath& offline_file_path) { |
| 214 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 214 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 215 | 215 |
| 216 if (!job) | 216 if (!job) |
| 217 return; | 217 return; |
| 218 job->OnOfflineFilePathAvailable(offline_file_path); | 218 job->OnOfflineFilePathAvailable(offline_file_path); |
| 219 } | 219 } |
| 220 | 220 |
| 221 void NotifyOfflineRedirectOnIO(base::WeakPtr<OfflinePageRequestJob> job, | |
| 222 const GURL& redirected_url) { | |
| 223 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 224 | |
| 225 if (!job) | |
| 226 return; | |
| 227 job->OnOfflineRedirectAvailabe(redirected_url); | |
| 228 } | |
| 229 | |
| 221 // Notifies OfflinePageRequestJob about the offline file path. Note that the | 230 // Notifies OfflinePageRequestJob about the offline file path. Note that the |
| 222 // file path may be empty if not found or on error. | 231 // file path may be empty if not found or on error. |
| 223 void NotifyOfflineFilePathOnUI(base::WeakPtr<OfflinePageRequestJob> job, | 232 void NotifyOfflineFilePathOnUI(base::WeakPtr<OfflinePageRequestJob> job, |
| 224 const base::FilePath& offline_file_path) { | 233 const base::FilePath& offline_file_path) { |
| 225 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 234 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 226 | 235 |
| 227 // Delegates to IO thread since OfflinePageRequestJob should only be accessed | 236 // Delegates to IO thread since OfflinePageRequestJob should only be accessed |
| 228 // from IO thread. | 237 // from IO thread. |
| 229 content::BrowserThread::PostTask( | 238 content::BrowserThread::PostTask( |
| 230 content::BrowserThread::IO, | 239 content::BrowserThread::IO, |
| 231 FROM_HERE, | 240 FROM_HERE, |
| 232 base::Bind(&NotifyOfflineFilePathOnIO, job, offline_file_path)); | 241 base::Bind(&NotifyOfflineFilePathOnIO, job, offline_file_path)); |
| 233 } | 242 } |
| 234 | 243 |
| 244 // Notifies OfflinePageRequestJob about the redirected URL. Note that | |
| 245 // redirected_url should not be empty. | |
| 246 void NotifyOfflineRedirectOnUI(base::WeakPtr<OfflinePageRequestJob> job, | |
| 247 const GURL& redirected_url) { | |
| 248 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 249 DCHECK(!redirected_url.is_empty()); | |
| 250 | |
| 251 // Delegates to IO thread since OfflinePageRequestJob should only be accessed | |
| 252 // from IO thread. | |
| 253 content::BrowserThread::PostTask( | |
| 254 content::BrowserThread::IO, | |
| 255 FROM_HERE, | |
| 256 base::Bind(&NotifyOfflineRedirectOnIO, job, redirected_url)); | |
| 257 } | |
| 258 | |
| 235 // Finds the offline file path based on the select page result and network | 259 // Finds the offline file path based on the select page result and network |
| 236 // state and marks it as accessed. | 260 // state and marks it as accessed. |
| 237 RequestResult AccessOfflineFile( | 261 RequestResult AccessOfflineFile( |
| 238 const OfflinePageHeader& offline_header, | 262 const OfflinePageHeader& offline_header, |
| 239 NetworkState network_state, | 263 NetworkState network_state, |
| 240 base::WeakPtr<OfflinePageRequestJob> job, | 264 base::WeakPtr<OfflinePageRequestJob> job, |
| 241 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, | 265 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, |
| 242 const OfflinePageItem* offline_page, | 266 const OfflinePageItem* offline_page, |
| 243 base::FilePath* offline_file_path) { | 267 base::FilePath* offline_file_path) { |
| 244 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 268 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 279 *offline_page, | 303 *offline_page, |
| 280 offline_header, | 304 offline_header, |
| 281 network_state == NetworkState::PROHIBITIVELY_SLOW_NETWORK); | 305 network_state == NetworkState::PROHIBITIVELY_SLOW_NETWORK); |
| 282 | 306 |
| 283 *offline_file_path = offline_page->file_path; | 307 *offline_file_path = offline_page->file_path; |
| 284 return RequestResult::OFFLINE_PAGE_SERVED; | 308 return RequestResult::OFFLINE_PAGE_SERVED; |
| 285 } | 309 } |
| 286 | 310 |
| 287 // Handles the result of finding an offline page. | 311 // Handles the result of finding an offline page. |
| 288 void SucceededToFindOfflinePage( | 312 void SucceededToFindOfflinePage( |
| 313 const GURL& url, | |
| 289 const OfflinePageHeader& offline_header, | 314 const OfflinePageHeader& offline_header, |
| 290 NetworkState network_state, | 315 NetworkState network_state, |
| 291 base::WeakPtr<OfflinePageRequestJob> job, | 316 base::WeakPtr<OfflinePageRequestJob> job, |
| 292 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, | 317 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, |
| 293 const OfflinePageItem* offline_page) { | 318 const OfflinePageItem* offline_page) { |
| 294 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 319 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 295 | 320 |
| 321 // If the match is for original URL, trigger the redirect. | |
| 322 if (offline_page && url == offline_page->original_url) { | |
| 323 NotifyOfflineRedirectOnUI(job, offline_page->url); | |
| 324 return; | |
| 325 } | |
| 326 | |
| 296 base::FilePath offline_file_path; | 327 base::FilePath offline_file_path; |
| 297 RequestResult request_result = AccessOfflineFile( | 328 RequestResult request_result = AccessOfflineFile( |
| 298 offline_header, network_state, job, web_contents_getter, offline_page, | 329 offline_header, network_state, job, web_contents_getter, offline_page, |
| 299 &offline_file_path); | 330 &offline_file_path); |
| 300 | 331 |
| 301 ReportRequestResult(request_result, network_state); | 332 ReportRequestResult(request_result, network_state); |
| 302 | 333 |
| 303 // NotifyOfflineFilePathOnUI should always be called regardless the failure | 334 // NotifyOfflineFilePathOnUI should always be called regardless the failure |
| 304 // result and empty file path such that OfflinePageRequestJob will be notified | 335 // result and empty file path such that OfflinePageRequestJob will be notified |
| 305 // on failure. | 336 // on failure. |
| 306 NotifyOfflineFilePathOnUI(job, offline_file_path); | 337 NotifyOfflineFilePathOnUI(job, offline_file_path); |
| 307 } | 338 } |
| 308 | 339 |
| 309 void FailedToFindOfflinePage(base::WeakPtr<OfflinePageRequestJob> job) { | 340 void FailedToFindOfflinePage(base::WeakPtr<OfflinePageRequestJob> job) { |
| 310 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 341 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 311 | 342 |
| 312 // Proceed with empty file path in order to notify the OfflinePageRequestJob | 343 // Proceed with empty file path in order to notify the OfflinePageRequestJob |
| 313 // about the failure. | 344 // about the failure. |
| 314 base::FilePath empty_file_path; | 345 base::FilePath empty_file_path; |
| 315 NotifyOfflineFilePathOnUI(job, empty_file_path); | 346 NotifyOfflineFilePathOnUI(job, empty_file_path); |
| 316 } | 347 } |
| 317 | 348 |
| 318 // Tries to find the offline page to serve for |online_url|. | 349 // Tries to find the offline page to serve for |url|. |
| 319 void SelectPageForOnlineURL( | 350 void SelectPageForURL( |
| 320 const GURL& online_url, | 351 const GURL& url, |
| 321 const OfflinePageHeader& offline_header, | 352 const OfflinePageHeader& offline_header, |
| 322 NetworkState network_state, | 353 NetworkState network_state, |
| 323 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, | 354 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, |
| 324 OfflinePageRequestJob::Delegate::TabIdGetter tab_id_getter, | 355 OfflinePageRequestJob::Delegate::TabIdGetter tab_id_getter, |
| 325 base::WeakPtr<OfflinePageRequestJob> job) { | 356 base::WeakPtr<OfflinePageRequestJob> job) { |
| 326 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 357 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 327 | 358 |
| 328 content::WebContents* web_contents = web_contents_getter.Run(); | 359 content::WebContents* web_contents = web_contents_getter.Run(); |
| 329 if (!web_contents){ | 360 if (!web_contents){ |
| 330 ReportRequestResult(RequestResult::NO_WEB_CONTENTS, network_state); | 361 ReportRequestResult(RequestResult::NO_WEB_CONTENTS, network_state); |
| 331 FailedToFindOfflinePage(job); | 362 FailedToFindOfflinePage(job); |
| 332 return; | 363 return; |
| 333 } | 364 } |
| 334 int tab_id; | 365 int tab_id; |
| 335 if (!tab_id_getter.Run(web_contents, &tab_id)) { | 366 if (!tab_id_getter.Run(web_contents, &tab_id)) { |
| 336 ReportRequestResult(RequestResult::NO_TAB_ID, network_state); | 367 ReportRequestResult(RequestResult::NO_TAB_ID, network_state); |
| 337 FailedToFindOfflinePage(job); | 368 FailedToFindOfflinePage(job); |
| 338 return; | 369 return; |
| 339 } | 370 } |
| 340 | 371 |
| 341 OfflinePageUtils::SelectPageForOnlineURL( | 372 OfflinePageUtils::SelectPageForURL( |
| 342 web_contents->GetBrowserContext(), | 373 web_contents->GetBrowserContext(), |
| 343 online_url, | 374 url, |
| 375 false /* search_by_final_url_only */, | |
| 344 tab_id, | 376 tab_id, |
| 345 base::Bind(&SucceededToFindOfflinePage, | 377 base::Bind(&SucceededToFindOfflinePage, |
| 378 url, | |
| 346 offline_header, | 379 offline_header, |
| 347 network_state, | 380 network_state, |
| 348 job, | 381 job, |
| 349 web_contents_getter)); | 382 web_contents_getter)); |
| 350 } | 383 } |
| 351 | 384 |
| 352 void FindPageWithOfflineIDDone( | 385 void FindPageWithOfflineIDDone( |
| 353 const GURL& online_url, | 386 const GURL& url, |
| 354 const OfflinePageHeader& offline_header, | 387 const OfflinePageHeader& offline_header, |
| 355 NetworkState network_state, | 388 NetworkState network_state, |
| 356 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, | 389 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, |
| 357 OfflinePageRequestJob::Delegate::TabIdGetter tab_id_getter, | 390 OfflinePageRequestJob::Delegate::TabIdGetter tab_id_getter, |
| 358 base::WeakPtr<OfflinePageRequestJob> job, | 391 base::WeakPtr<OfflinePageRequestJob> job, |
| 359 const OfflinePageItem* offline_page) { | 392 const OfflinePageItem* offline_page) { |
| 360 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 393 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 361 | 394 |
| 362 // If the found offline page does not has same URL as the request URL, fall | 395 // If the found offline page does not has same URL as the request URL, fall |
| 363 // back to find the offline page based on the URL. | 396 // back to find the offline page based on the URL. |
| 364 if (!offline_page || offline_page->url != online_url) { | 397 if (!offline_page || offline_page->url != url) { |
| 365 SelectPageForOnlineURL( | 398 SelectPageForURL( |
| 366 online_url, offline_header, network_state, web_contents_getter, | 399 url, offline_header, network_state, web_contents_getter, |
| 367 tab_id_getter, job); | 400 tab_id_getter, job); |
| 368 return; | 401 return; |
| 369 } | 402 } |
| 370 | 403 |
| 371 SucceededToFindOfflinePage( | 404 SucceededToFindOfflinePage( |
| 372 offline_header, network_state, job, web_contents_getter, offline_page); | 405 url, offline_header, network_state, job, web_contents_getter, |
| 406 offline_page); | |
| 373 } | 407 } |
| 374 | 408 |
| 375 // Tries to find an offline page associated with |offline_id|. | 409 // Tries to find an offline page associated with |offline_id|. |
| 376 void FindPageWithOfflineID( | 410 void FindPageWithOfflineID( |
| 377 const GURL& online_url, | 411 const GURL& url, |
| 378 const OfflinePageHeader& offline_header, | 412 const OfflinePageHeader& offline_header, |
| 379 int64_t offline_id, | 413 int64_t offline_id, |
| 380 NetworkState network_state, | 414 NetworkState network_state, |
| 381 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, | 415 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, |
| 382 OfflinePageRequestJob::Delegate::TabIdGetter tab_id_getter, | 416 OfflinePageRequestJob::Delegate::TabIdGetter tab_id_getter, |
| 383 base::WeakPtr<OfflinePageRequestJob> job) { | 417 base::WeakPtr<OfflinePageRequestJob> job) { |
| 384 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 418 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 385 | 419 |
| 386 OfflinePageModel* offline_page_model = | 420 OfflinePageModel* offline_page_model = |
| 387 GetOfflinePageModel(web_contents_getter); | 421 GetOfflinePageModel(web_contents_getter); |
| 388 if (!offline_page_model) { | 422 if (!offline_page_model) { |
| 389 FailedToFindOfflinePage(job); | 423 FailedToFindOfflinePage(job); |
| 390 return; | 424 return; |
| 391 } | 425 } |
| 392 | 426 |
| 393 offline_page_model->GetPageByOfflineId( | 427 offline_page_model->GetPageByOfflineId( |
| 394 offline_id, | 428 offline_id, |
| 395 base::Bind(&FindPageWithOfflineIDDone, | 429 base::Bind(&FindPageWithOfflineIDDone, |
| 396 online_url, | 430 url, |
| 397 offline_header, | 431 offline_header, |
| 398 network_state, | 432 network_state, |
| 399 web_contents_getter, | 433 web_contents_getter, |
| 400 tab_id_getter, | 434 tab_id_getter, |
| 401 job)); | 435 job)); |
| 402 } | 436 } |
| 403 | 437 |
| 404 // Tries to find the offline page to serve for |online_url|. | 438 // Tries to find the offline page to serve for |url|. |
| 405 void SelectPage( | 439 void SelectPage( |
| 406 const GURL& online_url, | 440 const GURL& url, |
| 407 const OfflinePageHeader& offline_header, | 441 const OfflinePageHeader& offline_header, |
| 408 NetworkState network_state, | 442 NetworkState network_state, |
| 409 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, | 443 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, |
| 410 OfflinePageRequestJob::Delegate::TabIdGetter tab_id_getter, | 444 OfflinePageRequestJob::Delegate::TabIdGetter tab_id_getter, |
| 411 base::WeakPtr<OfflinePageRequestJob> job) { | 445 base::WeakPtr<OfflinePageRequestJob> job) { |
| 412 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 446 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 413 | 447 |
| 414 // If an offline ID is present in the offline header, try to load that | 448 // If an offline ID is present in the offline header, try to load that |
| 415 // particular version. | 449 // particular version. |
| 416 if (!offline_header.id.empty()) { | 450 if (!offline_header.id.empty()) { |
| 417 // if the id string cannot be converted to int64 id, fall through to | 451 // if the id string cannot be converted to int64 id, fall through to |
| 418 // select page via online URL. | 452 // select page via URL. |
| 419 int64_t offline_id; | 453 int64_t offline_id; |
| 420 if (base::StringToInt64(offline_header.id, &offline_id)) { | 454 if (base::StringToInt64(offline_header.id, &offline_id)) { |
| 421 FindPageWithOfflineID(online_url, offline_header, offline_id, | 455 FindPageWithOfflineID(url, offline_header, offline_id, |
| 422 network_state, web_contents_getter, tab_id_getter, | 456 network_state, web_contents_getter, tab_id_getter, |
| 423 job); | 457 job); |
| 424 return; | 458 return; |
| 425 } | 459 } |
| 426 } | 460 } |
| 427 | 461 |
| 428 SelectPageForOnlineURL(online_url, offline_header, network_state, | 462 SelectPageForURL(url, offline_header, network_state, web_contents_getter, |
| 429 web_contents_getter, tab_id_getter, job); | 463 tab_id_getter, job); |
| 430 } | 464 } |
| 431 | 465 |
| 432 } // namespace | 466 } // namespace |
| 433 | 467 |
| 434 // static | 468 // static |
| 435 void OfflinePageRequestJob::ReportAggregatedRequestResult( | 469 void OfflinePageRequestJob::ReportAggregatedRequestResult( |
| 436 AggregatedRequestResult result) { | 470 AggregatedRequestResult result) { |
| 437 UMA_HISTOGRAM_ENUMERATION("OfflinePages.AggregatedRequestResult2", | 471 UMA_HISTOGRAM_ENUMERATION("OfflinePages.AggregatedRequestResult2", |
| 438 static_cast<int>(result), | 472 static_cast<int>(result), |
| 439 static_cast<int>(AggregatedRequestResult::AGGREGATED_REQUEST_RESULT_MAX)); | 473 static_cast<int>(AggregatedRequestResult::AGGREGATED_REQUEST_RESULT_MAX)); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 549 if (offline_file_path.empty()) { | 583 if (offline_file_path.empty()) { |
| 550 FallbackToDefault(); | 584 FallbackToDefault(); |
| 551 return; | 585 return; |
| 552 } | 586 } |
| 553 | 587 |
| 554 // Sets the file path and lets URLRequestFileJob start to read from the file. | 588 // Sets the file path and lets URLRequestFileJob start to read from the file. |
| 555 file_path_ = offline_file_path; | 589 file_path_ = offline_file_path; |
| 556 URLRequestFileJob::Start(); | 590 URLRequestFileJob::Start(); |
| 557 } | 591 } |
| 558 | 592 |
| 593 void OfflinePageRequestJob::OnOfflineRedirectAvailabe( | |
| 594 const GURL& redirected_url) { | |
| 595 // TODO(jianli): kicks off the redirect. For now, use the default. | |
|
fgorski
2016/11/16 23:43:46
Please document it better. I don't understand what
jianli
2016/11/17 01:12:14
This will be done in my 3rd patch which is going t
| |
| 596 FallbackToDefault(); | |
| 597 } | |
| 598 | |
| 559 void OfflinePageRequestJob::SetDelegateForTesting( | 599 void OfflinePageRequestJob::SetDelegateForTesting( |
| 560 std::unique_ptr<Delegate> delegate) { | 600 std::unique_ptr<Delegate> delegate) { |
| 561 delegate_ = std::move(delegate); | 601 delegate_ = std::move(delegate); |
| 562 } | 602 } |
| 563 | 603 |
| 564 } // namespace offline_pages | 604 } // namespace offline_pages |
| OLD | NEW |