| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 // Network is in working condition. | 51 // Network is in working condition. |
| 52 CONNECTED_NETWORK, | 52 CONNECTED_NETWORK, |
| 53 // Force to load the offline page if it is available, though network is in | 53 // Force to load the offline page if it is available, though network is in |
| 54 // working condition. | 54 // working condition. |
| 55 FORCE_OFFLINE_ON_CONNECTED_NETWORK | 55 FORCE_OFFLINE_ON_CONNECTED_NETWORK |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 // This enum is used to tell all possible outcomes of handling network requests | 58 // This enum is used to tell all possible outcomes of handling network requests |
| 59 // that might serve offline contents. | 59 // that might serve offline contents. |
| 60 enum class RequestResult { | 60 enum class RequestResult { |
| 61 // Offline page was shown for current URL. |
| 61 OFFLINE_PAGE_SERVED, | 62 OFFLINE_PAGE_SERVED, |
| 63 // Redirected from original URL to final URL in preparation to show the |
| 64 // offline page under final URL. OFFLINE_PAGE_SERVED is most likely to be |
| 65 // reported next if no other error is encountered. |
| 66 REDIRECTED, |
| 67 // Tab was gone. |
| 62 NO_TAB_ID, | 68 NO_TAB_ID, |
| 69 // Web contents was gone. |
| 63 NO_WEB_CONTENTS, | 70 NO_WEB_CONTENTS, |
| 71 // The offline page found was not fresh enough, i.e. not created in the past |
| 72 // day. This only applies in prohibitively slow network. |
| 64 PAGE_NOT_FRESH, | 73 PAGE_NOT_FRESH, |
| 74 // Offline page was not found, by searching with either final URL or original |
| 75 // URL. |
| 65 OFFLINE_PAGE_NOT_FOUND | 76 OFFLINE_PAGE_NOT_FOUND |
| 66 }; | 77 }; |
| 67 | 78 |
| 68 const char kUserDataKey[] = "offline_page_key"; | 79 const char kUserDataKey[] = "offline_page_key"; |
| 69 | 80 |
| 70 // Contains the info to handle offline page request. | 81 // Contains the info to handle offline page request. |
| 71 class OfflinePageRequestInfo : public base::SupportsUserData::Data { | 82 class OfflinePageRequestInfo : public base::SupportsUserData::Data { |
| 72 public: | 83 public: |
| 73 OfflinePageRequestInfo() : use_default_(false) {} | 84 OfflinePageRequestInfo() : use_default_(false) {} |
| 74 ~OfflinePageRequestInfo() override {} | 85 ~OfflinePageRequestInfo() override {} |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 return OfflinePageRequestJob::AggregatedRequestResult:: | 177 return OfflinePageRequestJob::AggregatedRequestResult:: |
| 167 PAGE_NOT_FOUND_ON_FLAKY_NETWORK; | 178 PAGE_NOT_FOUND_ON_FLAKY_NETWORK; |
| 168 case NetworkState::FORCE_OFFLINE_ON_CONNECTED_NETWORK: | 179 case NetworkState::FORCE_OFFLINE_ON_CONNECTED_NETWORK: |
| 169 return OfflinePageRequestJob::AggregatedRequestResult:: | 180 return OfflinePageRequestJob::AggregatedRequestResult:: |
| 170 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK; | 181 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK; |
| 171 default: | 182 default: |
| 172 NOTREACHED(); | 183 NOTREACHED(); |
| 173 } | 184 } |
| 174 } | 185 } |
| 175 | 186 |
| 187 if (request_result == RequestResult::REDIRECTED) { |
| 188 switch (network_state) { |
| 189 case NetworkState::DISCONNECTED_NETWORK: |
| 190 return OfflinePageRequestJob::AggregatedRequestResult:: |
| 191 REDIRECTED_ON_DISCONNECTED_NETWORK; |
| 192 case NetworkState::PROHIBITIVELY_SLOW_NETWORK: |
| 193 return OfflinePageRequestJob::AggregatedRequestResult:: |
| 194 REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK; |
| 195 case NetworkState::FLAKY_NETWORK: |
| 196 return OfflinePageRequestJob::AggregatedRequestResult:: |
| 197 REDIRECTED_ON_FLAKY_NETWORK; |
| 198 case NetworkState::FORCE_OFFLINE_ON_CONNECTED_NETWORK: |
| 199 return OfflinePageRequestJob::AggregatedRequestResult:: |
| 200 REDIRECTED_ON_CONNECTED_NETWORK; |
| 201 default: |
| 202 NOTREACHED(); |
| 203 } |
| 204 } |
| 205 |
| 176 DCHECK_EQ(RequestResult::OFFLINE_PAGE_SERVED, request_result); | 206 DCHECK_EQ(RequestResult::OFFLINE_PAGE_SERVED, request_result); |
| 177 DCHECK_NE(NetworkState::CONNECTED_NETWORK, network_state); | 207 DCHECK_NE(NetworkState::CONNECTED_NETWORK, network_state); |
| 178 switch (network_state) { | 208 switch (network_state) { |
| 179 case NetworkState::DISCONNECTED_NETWORK: | 209 case NetworkState::DISCONNECTED_NETWORK: |
| 180 return OfflinePageRequestJob::AggregatedRequestResult:: | 210 return OfflinePageRequestJob::AggregatedRequestResult:: |
| 181 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK; | 211 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK; |
| 182 case NetworkState::PROHIBITIVELY_SLOW_NETWORK: | 212 case NetworkState::PROHIBITIVELY_SLOW_NETWORK: |
| 183 return OfflinePageRequestJob::AggregatedRequestResult:: | 213 return OfflinePageRequestJob::AggregatedRequestResult:: |
| 184 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK; | 214 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK; |
| 185 case NetworkState::FLAKY_NETWORK: | 215 case NetworkState::FLAKY_NETWORK: |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 const GURL& url, | 347 const GURL& url, |
| 318 const OfflinePageHeader& offline_header, | 348 const OfflinePageHeader& offline_header, |
| 319 NetworkState network_state, | 349 NetworkState network_state, |
| 320 base::WeakPtr<OfflinePageRequestJob> job, | 350 base::WeakPtr<OfflinePageRequestJob> job, |
| 321 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, | 351 content::ResourceRequestInfo::WebContentsGetter web_contents_getter, |
| 322 const OfflinePageItem* offline_page) { | 352 const OfflinePageItem* offline_page) { |
| 323 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 353 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 324 | 354 |
| 325 // If the match is for original URL, trigger the redirect. | 355 // If the match is for original URL, trigger the redirect. |
| 326 if (offline_page && url == offline_page->original_url) { | 356 if (offline_page && url == offline_page->original_url) { |
| 357 ReportRequestResult(RequestResult::REDIRECTED, network_state); |
| 327 NotifyOfflineRedirectOnUI(job, offline_page->url); | 358 NotifyOfflineRedirectOnUI(job, offline_page->url); |
| 328 return; | 359 return; |
| 329 } | 360 } |
| 330 | 361 |
| 331 base::FilePath offline_file_path; | 362 base::FilePath offline_file_path; |
| 332 RequestResult request_result = AccessOfflineFile( | 363 RequestResult request_result = AccessOfflineFile( |
| 333 offline_header, network_state, job, web_contents_getter, offline_page, | 364 offline_header, network_state, job, web_contents_getter, offline_page, |
| 334 &offline_file_path); | 365 &offline_file_path); |
| 335 | 366 |
| 336 ReportRequestResult(request_result, network_state); | 367 ReportRequestResult(request_result, network_state); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 | 688 |
| 658 URLRequestJob::NotifyHeadersComplete(); | 689 URLRequestJob::NotifyHeadersComplete(); |
| 659 } | 690 } |
| 660 | 691 |
| 661 void OfflinePageRequestJob::SetDelegateForTesting( | 692 void OfflinePageRequestJob::SetDelegateForTesting( |
| 662 std::unique_ptr<Delegate> delegate) { | 693 std::unique_ptr<Delegate> delegate) { |
| 663 delegate_ = std::move(delegate); | 694 delegate_ = std::move(delegate); |
| 664 } | 695 } |
| 665 | 696 |
| 666 } // namespace offline_pages | 697 } // namespace offline_pages |
| OLD | NEW |