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 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_REQUEST_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_REQUEST_HANDLER_H_ |
6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_REQUEST_HANDLER_H_ | 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_REQUEST_HANDLER_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/weak_ptr.h" | |
9 #include "base/supports_user_data.h" | 10 #include "base/supports_user_data.h" |
11 #include "chrome/browser/android/offline_pages/offline_page_request_job.h" | |
12 #include "content/public/browser/resource_request_info.h" | |
10 #include "content/public/common/resource_type.h" | 13 #include "content/public/common/resource_type.h" |
11 | 14 |
12 namespace net { | 15 namespace net { |
13 class NetworkDelegate; | 16 class NetworkDelegate; |
14 class URLRequest; | 17 class URLRequest; |
15 class URLRequestInterceptor; | 18 class URLRequestInterceptor; |
16 class URLRequestJob; | 19 class URLRequestJob; |
17 } | 20 } |
18 | 21 |
19 namespace offline_pages { | 22 namespace offline_pages { |
20 | 23 |
24 // This enum is used to tell current network state that will affect offline | |
25 // page loading. | |
26 enum class NetworkState { | |
27 // No network connection. | |
28 DISCONNECTED_NETWORK, | |
29 // Prohibitively slow means that the NetworkQualityEstimator reported a | |
30 // connection slow enough to warrant showing an offline page if available. | |
31 PROHIBITIVELY_SLOW_NETWORK, | |
32 // Network error received due to bad network, i.e. connected to a hotspot or | |
33 // proxy that does not have a working network. | |
34 FLAKY_NETWORK, | |
35 // Network is in working condition. | |
36 CONNECTED_NETWORK, | |
37 // Network condition check will be skipped. This will force to load the | |
38 // offline page if it is available. | |
39 SKIPPED_NETWORK_CHECK | |
40 }; | |
41 | |
42 // This enum is used for UMA reporting. It contains all possible outcomes of | |
43 // handling requests that might service offline page in different network | |
44 // conditions. Generally one of these outcomes will happen. | |
45 // The fringe errors (like no OfflinePageModel, etc.) are not reported due | |
46 // to their low probability. | |
47 // NOTE: because this is used for UMA reporting, these values should not be | |
48 // changed or reused; new values should be ended immediately before the MAX | |
49 // value. Make sure to update the histogram enum | |
50 // (OfflinePagesAggregatedRequestResult in histograms.xml) accordingly. | |
51 // Public for testing. | |
52 enum class AggregatedRequestResult { | |
53 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK, | |
54 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK, | |
55 SHOW_OFFLINE_ON_FLAKY_NETWORK, | |
56 PAGE_NOT_FOUND_ON_FLAKY_NETWORK, | |
57 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK, | |
58 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK, | |
59 PAGE_NOT_FRESH_ON_PROHIBITIVELY_SLOW_NETWORK, | |
60 SHOW_OFFLINE_ON_CONNECTED_NETWORK, | |
61 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK, | |
62 NO_TAB_ID, | |
63 NO_WEB_CONTENTS, | |
64 SHOW_NET_ERROR_PAGE, | |
65 AGGREGATED_REQUEST_RESULT_MAX, | |
fgorski
2016/08/15 21:38:32
nit: add a comment that new values are added above
jianli
2016/08/15 23:15:43
Such comment is already added at line 48.
| |
66 }; | |
67 | |
68 // Header that indicates that the offline page should be loaded if it exists | |
69 // regardless current network conditions. Its value is a comma/space separated | |
70 // name-value pair that may provide reason or define custom behavior. | |
71 extern const char kLoadingOfflinePageHeader[]; | |
72 | |
73 // The name used in name-value pair of kLoadingOfflinePageHeader to denote the | |
74 // reason for loading offline page. | |
75 extern const char kLoadingOfflinePageReason[]; | |
76 // Possible values in name-value pair that denote the reason for loading offline | |
77 // page. | |
78 extern const char kLoadingOfflinePageDueToNetError[]; | |
79 | |
21 // Class for servicing requests based on their offline information. Created one | 80 // Class for servicing requests based on their offline information. Created one |
22 // per URLRequest and attached to each request. | 81 // per URLRequest and attached to each request. |
23 // | 82 // |
24 // For each supported profile, OfflinePageRequestHandler::CreateInterceptor | 83 // For each supported profile, OfflinePageRequestHandler::CreateInterceptor |
25 // should be called once to install the custom interceptor. | 84 // should be called once to install the custom interceptor. |
26 // | 85 // |
27 // For each request: | 86 // For each request: |
28 // 1) When a request is starting, OfflinePageRequestHandler::InitializeHandler | 87 // 1) When a request is starting, OfflinePageRequestHandler::InitializeHandler |
29 // will be called to create a new handler and attach to the request. | 88 // will be called to create a new handler and attach to the request. |
30 // 2) When a request is being processed, MaybeInterceptRequest of custom | 89 // 2) When a request is being processed, MaybeInterceptRequest of custom |
31 // interceptor will be inquired. | 90 // interceptor will be inquired. |
32 // 2.1) If the attached OfflinePageRequestHandler for the request is found, | 91 // 2.1) If the attached OfflinePageRequestHandler for the request is found, |
33 // delegate to OfflinePageRequestHandler::MaybeCreateJob to do the work. | 92 // delegate to OfflinePageRequestHandler::MaybeCreateJob to do the work. |
34 // 2.1.1) Do immediate checks for those scenarios that the interception | 93 // 2.1.1) Do immediate checks for those scenarios that the interception |
35 // is not needed. Bail out if so. | 94 // is not needed. Bail out if so. |
36 // 2.1.2) Start an async task to try to find the offline page. | 95 // 2.1.2) Start an async task to try to find the offline page. |
37 // 2.1.3) Return a custom URLRequestJob that is put on hold to wait | 96 // 2.1.3) Return a custom URLRequestJob that is put on hold to wait |
38 // for the result of finding offline page. | 97 // for the result of finding offline page. |
39 // 2.2) Otherwise, bail out without interception. | 98 // 2.2) Otherwise, bail out without interception. |
40 class OfflinePageRequestHandler : public base::SupportsUserData::Data { | 99 class OfflinePageRequestHandler : public base::SupportsUserData::Data, |
100 public OfflinePageRequestJob::Delegate { | |
41 public: | 101 public: |
42 // Attaches a newly created handler if the given |request| needs to | 102 // Attaches a newly created handler if the given |request| needs to |
43 // be handled by offline pages. | 103 // be handled by offline pages. |
44 static void InitializeHandler(net::URLRequest* request, | 104 static void InitializeHandler(net::URLRequest* request, |
45 content::ResourceType resource_type); | 105 content::ResourceType resource_type); |
46 | 106 |
47 // Returns the handler attached to |request|. This may return null if no | 107 // Returns the handler attached to |request|. This may return null if no |
48 // handler is attached. | 108 // handler is attached. |
49 static OfflinePageRequestHandler* GetHandler(net::URLRequest* request); | 109 static OfflinePageRequestHandler* GetHandler(net::URLRequest* request); |
50 | 110 |
51 // Creates a protocol interceptor for offline pages. Created one per | 111 // Creates a protocol interceptor for offline pages. Created one per |
52 // supported, i.e. non-incognito, profile. | 112 // supported, i.e. non-incognito, profile. |
53 // |profile_id|, which identifies the profile, is passed as a void* to ensure | 113 // |profile_id|, which identifies the profile, is passed as a void* to ensure |
54 // it's not accidently used on the IO thread. | 114 // it's not accidently used on the IO thread. |
55 static std::unique_ptr<net::URLRequestInterceptor> CreateInterceptor( | 115 static std::unique_ptr<net::URLRequestInterceptor> CreateInterceptor( |
56 void* profile_id); | 116 void* profile_id); |
57 | 117 |
118 static void ReportAggregatedRequestResult(AggregatedRequestResult result); | |
119 | |
58 ~OfflinePageRequestHandler() override; | 120 ~OfflinePageRequestHandler() override; |
59 | 121 |
122 // OfflinePageRequestJob::Delegate overrides: | |
123 void OnPrepareToRestart() override; | |
124 | |
60 net::URLRequestJob* MaybeCreateJob( | 125 net::URLRequestJob* MaybeCreateJob( |
61 net::URLRequest* request, | 126 net::URLRequest* request, |
62 net::NetworkDelegate* network_delegate, | 127 net::NetworkDelegate* network_delegate, |
63 void* profile_id); | 128 void* profile_id); |
64 | 129 |
65 private: | 130 private: |
66 OfflinePageRequestHandler(); | 131 explicit OfflinePageRequestHandler(NetworkState network_state); |
132 | |
133 // Invoked from UI thread. | |
134 void GetOfflineFileURL( | |
135 const GURL& online_url, | |
136 void* profile_id, | |
137 content::ResourceRequestInfo::WebContentsGetter web_contents_getter); | |
138 void FailedToGetOfflineFileURL(); | |
139 | |
140 base::WeakPtr<OfflinePageRequestJob> offline_job_; | |
141 | |
142 // True if the next time this request is started, the request should be | |
143 // serviced from the default handler. | |
144 bool use_default_ = false; | |
145 | |
146 NetworkState network_state_; | |
147 | |
148 base::WeakPtrFactory<OfflinePageRequestHandler> weak_ptr_factory_; | |
67 | 149 |
68 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestHandler); | 150 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestHandler); |
69 }; | 151 }; |
70 | 152 |
71 } // namespace offline_pages | 153 } // namespace offline_pages |
72 | 154 |
73 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_REQUEST_HANDLER_H_ | 155 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_REQUEST_HANDLER_H_ |
OLD | NEW |