Chromium Code Reviews| Index: chrome/browser/android/offline_pages/offline_page_request_job.cc |
| diff --git a/chrome/browser/android/offline_pages/offline_page_request_job.cc b/chrome/browser/android/offline_pages/offline_page_request_job.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6425ef23f170b8d0057b4f536605ab0a14a22468 |
| --- /dev/null |
| +++ b/chrome/browser/android/offline_pages/offline_page_request_job.cc |
| @@ -0,0 +1,88 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/offline_pages/offline_page_request_job.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_path.h" |
| +#include "base/logging.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "net/http/http_request_headers.h" |
| +#include "net/url_request/url_request.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace offline_pages { |
| + |
| +OfflinePageRequestJob::OfflinePageRequestJob( |
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate, |
| + Delegate* delegate) |
| + : net::URLRequestFileJob( |
| + request, |
| + network_delegate, |
| + base::FilePath(), |
| + BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| + base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
| + delegate_(delegate), |
| + is_started_(false), |
| + state_(State::NOT_DETERMINED), |
| + weak_ptr_factory_(this) { |
| +} |
| + |
| +OfflinePageRequestJob::~OfflinePageRequestJob() { |
| +} |
| + |
| +void OfflinePageRequestJob::Start() { |
| + MaybeStartRequest(); |
| +} |
| + |
| +void OfflinePageRequestJob::Kill() { |
| + net::URLRequestJob::Kill(); |
| + weak_ptr_factory_.InvalidateWeakPtrs(); |
| +} |
| + |
| +base::WeakPtr<OfflinePageRequestJob> OfflinePageRequestJob::GetWeakPtr() { |
| + return weak_ptr_factory_.GetWeakPtr(); |
| +} |
| + |
| +void OfflinePageRequestJob::FallbackToDefault() { |
| + state_ = State::FALLBACK_TO_DEFAULT; |
| + MaybeStartRequest(); |
| +} |
| + |
| +void OfflinePageRequestJob::SetOfflineFilePath( |
| + const base::FilePath& offline_file_path) { |
| + DCHECK(!offline_file_path.empty()); |
| + |
| + state_ = State::SHOW_OFFLINE_CONTENT; |
| + file_path_ = offline_file_path; |
| + URLRequestFileJob::Start(); |
|
mmenke
2016/08/17 17:09:28
Seems like we should have protection against calli
jianli
2016/08/18 22:46:35
Fixed.
|
| +} |
| + |
| +void OfflinePageRequestJob::MaybeStartRequest() { |
| + if (is_started_) |
| + return; |
| + |
| + switch (state_) { |
| + case State::NOT_DETERMINED: |
| + // Still waiting to get offline data. |
| + return; |
| + case State::FALLBACK_TO_DEFAULT: |
| + // Restart the request to create a new job. Our request handler will |
| + // return nullptr, and the default job should be created. |
| + is_started_ = false; |
| + delegate_->OnPrepareToRestart(); |
| + URLRequestJob::NotifyRestartRequired(); |
| + return; |
| + case State::SHOW_OFFLINE_CONTENT: |
| + is_started_ = true; |
| + NotifyHeadersComplete(); |
|
mmenke
2016/08/17 17:09:28
We don't have DCHECKs for it (We really should), b
jianli
2016/08/18 22:46:35
Fixed.
|
| + return; |
| + } |
| + |
| + NOTREACHED(); |
| +} |
| + |
| +} // namespace offline_pages |