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 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_REQUEST_JOB_H_ |
| 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_REQUEST_JOB_H_ |
| 7 |
| 8 #include "base/memory/weak_ptr.h" |
| 9 #include "content/public/browser/resource_request_info.h" |
| 10 #include "content/public/common/resource_type.h" |
| 11 #include "net/url_request/url_request_file_job.h" |
| 12 |
| 13 namespace base { |
| 14 class FilePath; |
| 15 } |
| 16 |
| 17 namespace offline_pages { |
| 18 |
| 19 // Header that indicates that the offline page should be loaded if it exists |
| 20 // regardless current network conditions. Its value is a comma/space separated |
| 21 // name-value pair that may provide reason or define custom behavior. |
| 22 extern const char kLoadingOfflinePageHeader[]; |
| 23 // The name used in name-value pair of kLoadingOfflinePageHeader to denote the |
| 24 // reason for loading offline page. |
| 25 extern const char kLoadingOfflinePageReason[]; |
| 26 // Possible values in name-value pair that denote the reason for loading offline |
| 27 // page. |
| 28 extern const char kLoadingOfflinePageDueToNetError[]; |
| 29 |
| 30 // A request job that serves content from offline file. |
| 31 class OfflinePageRequestJob : public net::URLRequestFileJob { |
| 32 public: |
| 33 // This enum is used for UMA reporting. It contains all possible outcomes of |
| 34 // handling requests that might service offline page in different network |
| 35 // conditions. Generally one of these outcomes will happen. |
| 36 // The fringe errors (like no OfflinePageModel, etc.) are not reported due |
| 37 // to their low probability. |
| 38 // NOTE: because this is used for UMA reporting, these values should not be |
| 39 // changed or reused; new values should be ended immediately before the MAX |
| 40 // value. Make sure to update the histogram enum |
| 41 // (OfflinePagesAggregatedRequestResult in histograms.xml) accordingly. |
| 42 // Public for testing. |
| 43 enum class AggregatedRequestResult { |
| 44 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK, |
| 45 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK, |
| 46 SHOW_OFFLINE_ON_FLAKY_NETWORK, |
| 47 PAGE_NOT_FOUND_ON_FLAKY_NETWORK, |
| 48 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK, |
| 49 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK, |
| 50 PAGE_NOT_FRESH_ON_PROHIBITIVELY_SLOW_NETWORK, |
| 51 SHOW_OFFLINE_ON_CONNECTED_NETWORK, |
| 52 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK, |
| 53 NO_TAB_ID, |
| 54 NO_WEB_CONTENTS, |
| 55 SHOW_NET_ERROR_PAGE, |
| 56 AGGREGATED_REQUEST_RESULT_MAX |
| 57 }; |
| 58 |
| 59 // Delegate that allows tests to overwrite certain behaviors. |
| 60 class Delegate { |
| 61 public: |
| 62 using TabIdGetter = base::Callback<bool(content::WebContents*, int*)>; |
| 63 |
| 64 virtual ~Delegate() {} |
| 65 |
| 66 virtual content::ResourceRequestInfo::WebContentsGetter |
| 67 GetWebContentsGetter(net::URLRequest* request) const = 0; |
| 68 |
| 69 virtual TabIdGetter GetTabIdGetter() const = 0; |
| 70 }; |
| 71 |
| 72 // Reports the aggregated result combining both request result and network |
| 73 // state. |
| 74 static void ReportAggregatedRequestResult(AggregatedRequestResult result); |
| 75 |
| 76 // Creates and returns a job to serve the offline page. Nullptr is returned if |
| 77 // offline page cannot or should not be served. |
| 78 static OfflinePageRequestJob* Create(void* profile_id, |
| 79 net::URLRequest* request, |
| 80 net::NetworkDelegate* network_delegate); |
| 81 |
| 82 ~OfflinePageRequestJob() override; |
| 83 |
| 84 // net::URLRequestJob overrides: |
| 85 void Start() override; |
| 86 void Kill() override; |
| 87 |
| 88 void OnOfflineFilePathAvailable(const base::FilePath& offline_file_path); |
| 89 |
| 90 void SetDelegateForTesting(std::unique_ptr<Delegate> delegate); |
| 91 |
| 92 private: |
| 93 OfflinePageRequestJob(void* profile_id, |
| 94 net::URLRequest* request, |
| 95 net::NetworkDelegate* network_delegate); |
| 96 |
| 97 void StartAsync(); |
| 98 |
| 99 // Restarts the request job in order to fall back to the default handling. |
| 100 void FallbackToDefault(); |
| 101 |
| 102 // The profile for processing offline pages. |
| 103 void* profile_id_; |
| 104 |
| 105 std::unique_ptr<Delegate> delegate_; |
| 106 |
| 107 base::WeakPtrFactory<OfflinePageRequestJob> weak_ptr_factory_; |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestJob); |
| 110 }; |
| 111 |
| 112 } // namespace offline_pages |
| 113 |
| 114 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_REQUEST_JOB_H_ |
OLD | NEW |