OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/offline_pages/offline_page_request_job.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "net/http/http_request_headers.h" |
| 12 #include "net/url_request/url_request.h" |
| 13 |
| 14 using content::BrowserThread; |
| 15 |
| 16 namespace offline_pages { |
| 17 |
| 18 OfflinePageRequestJob::OfflinePageRequestJob( |
| 19 net::URLRequest* request, |
| 20 net::NetworkDelegate* network_delegate, |
| 21 Delegate* delegate) |
| 22 : net::URLRequestFileJob( |
| 23 request, |
| 24 network_delegate, |
| 25 base::FilePath(), |
| 26 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| 27 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
| 28 delegate_(delegate), |
| 29 is_started_(false), |
| 30 state_(State::NOT_DETERMINED), |
| 31 weak_ptr_factory_(this) { |
| 32 } |
| 33 |
| 34 OfflinePageRequestJob::~OfflinePageRequestJob() { |
| 35 } |
| 36 |
| 37 void OfflinePageRequestJob::Start() { |
| 38 MaybeStartRequest(); |
| 39 } |
| 40 |
| 41 void OfflinePageRequestJob::Kill() { |
| 42 net::URLRequestJob::Kill(); |
| 43 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 44 } |
| 45 |
| 46 base::WeakPtr<OfflinePageRequestJob> OfflinePageRequestJob::GetWeakPtr() { |
| 47 return weak_ptr_factory_.GetWeakPtr(); |
| 48 } |
| 49 |
| 50 void OfflinePageRequestJob::FallbackToDefault() { |
| 51 state_ = State::FALLBACK_TO_DEFAULT; |
| 52 MaybeStartRequest(); |
| 53 } |
| 54 |
| 55 void OfflinePageRequestJob::SetOfflineFilePath( |
| 56 const base::FilePath& offline_file_path) { |
| 57 DCHECK(!offline_file_path.empty()); |
| 58 |
| 59 state_ = State::SHOW_OFFLINE_CONTENT; |
| 60 file_path_ = offline_file_path; |
| 61 URLRequestFileJob::Start(); |
| 62 } |
| 63 |
| 64 void OfflinePageRequestJob::MaybeStartRequest() { |
| 65 if (is_started_) |
| 66 return; |
| 67 |
| 68 switch (state_) { |
| 69 case State::NOT_DETERMINED: |
| 70 // Still waiting to get offline data. |
| 71 return; |
| 72 case State::FALLBACK_TO_DEFAULT: |
| 73 // Restart the request to create a new job. Our request handler will |
| 74 // return nullptr, and the default job should be created. |
| 75 is_started_ = false; |
| 76 delegate_->OnPrepareToRestart(); |
| 77 URLRequestJob::NotifyRestartRequired(); |
| 78 return; |
| 79 case State::SHOW_OFFLINE_CONTENT: |
| 80 is_started_ = true; |
| 81 NotifyHeadersComplete(); |
| 82 return; |
| 83 } |
| 84 |
| 85 NOTREACHED(); |
| 86 } |
| 87 |
| 88 } // namespace offline_pages |
OLD | NEW |