OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file contains an implementation of the ResourceLoaderBridge class. | 5 // This file contains an implementation of the ResourceLoaderBridge class. |
6 // The class is implemented using net::URLRequest, meaning it is a "simple" | 6 // The class is implemented using net::URLRequest, meaning it is a "simple" |
7 // version that directly issues requests. The more complicated one used in the | 7 // version that directly issues requests. The more complicated one used in the |
8 // browser uses IPC. | 8 // browser uses IPC. |
9 // | 9 // |
10 // Because net::URLRequest only provides an asynchronous resource loading API, | 10 // Because net::URLRequest only provides an asynchronous resource loading API, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 #include "base/timer.h" | 45 #include "base/timer.h" |
46 #include "base/threading/thread.h" | 46 #include "base/threading/thread.h" |
47 #include "base/synchronization/waitable_event.h" | 47 #include "base/synchronization/waitable_event.h" |
48 #include "net/base/cookie_store.h" | 48 #include "net/base/cookie_store.h" |
49 #include "net/base/file_stream.h" | 49 #include "net/base/file_stream.h" |
50 #include "net/base/io_buffer.h" | 50 #include "net/base/io_buffer.h" |
51 #include "net/base/load_flags.h" | 51 #include "net/base/load_flags.h" |
52 #include "net/base/mime_util.h" | 52 #include "net/base/mime_util.h" |
53 #include "net/base/net_errors.h" | 53 #include "net/base/net_errors.h" |
54 #include "net/base/net_util.h" | 54 #include "net/base/net_util.h" |
| 55 #include "net/base/network_delegate.h" |
55 #include "net/base/static_cookie_policy.h" | 56 #include "net/base/static_cookie_policy.h" |
56 #include "net/base/upload_data.h" | 57 #include "net/base/upload_data.h" |
57 #include "net/http/http_cache.h" | 58 #include "net/http/http_cache.h" |
58 #include "net/http/http_request_headers.h" | 59 #include "net/http/http_request_headers.h" |
59 #include "net/http/http_response_headers.h" | 60 #include "net/http/http_response_headers.h" |
60 #include "net/url_request/url_request.h" | 61 #include "net/url_request/url_request.h" |
61 #include "net/url_request/url_request_job.h" | 62 #include "net/url_request/url_request_job.h" |
62 #include "webkit/appcache/appcache_interfaces.h" | 63 #include "webkit/appcache/appcache_interfaces.h" |
63 #include "webkit/blob/blob_storage_controller.h" | 64 #include "webkit/blob/blob_storage_controller.h" |
64 #include "webkit/blob/blob_url_request_job.h" | 65 #include "webkit/blob/blob_url_request_job.h" |
(...skipping 28 matching lines...) Expand all Loading... |
93 bool in_no_proxy) | 94 bool in_no_proxy) |
94 : cache_path(in_cache_path), | 95 : cache_path(in_cache_path), |
95 cache_mode(in_cache_mode), | 96 cache_mode(in_cache_mode), |
96 no_proxy(in_no_proxy) {} | 97 no_proxy(in_no_proxy) {} |
97 | 98 |
98 FilePath cache_path; | 99 FilePath cache_path; |
99 net::HttpCache::Mode cache_mode; | 100 net::HttpCache::Mode cache_mode; |
100 bool no_proxy; | 101 bool no_proxy; |
101 }; | 102 }; |
102 | 103 |
| 104 //----------------------------------------------------------------------------- |
| 105 |
| 106 bool g_accept_all_cookies = false; |
| 107 |
| 108 class TestShellNetworkDelegate : public net::NetworkDelegate { |
| 109 public: |
| 110 virtual ~TestShellNetworkDelegate() {} |
| 111 |
| 112 private: |
| 113 // net::NetworkDelegate implementation. |
| 114 virtual int OnBeforeURLRequest(net::URLRequest* request, |
| 115 const net::CompletionCallback& callback, |
| 116 GURL* new_url) OVERRIDE { |
| 117 return net::OK; |
| 118 } |
| 119 virtual int OnBeforeSendHeaders(net::URLRequest* request, |
| 120 const net::CompletionCallback& callback, |
| 121 net::HttpRequestHeaders* headers) OVERRIDE { |
| 122 return net::OK; |
| 123 } |
| 124 virtual void OnSendHeaders(net::URLRequest* request, |
| 125 const net::HttpRequestHeaders& headers) OVERRIDE {} |
| 126 virtual int OnHeadersReceived( |
| 127 net::URLRequest* request, |
| 128 const net::CompletionCallback& callback, |
| 129 net::HttpResponseHeaders* original_response_headers, |
| 130 scoped_refptr<net::HttpResponseHeaders>* |
| 131 override_response_headers) OVERRIDE { |
| 132 return net::OK; |
| 133 } |
| 134 virtual void OnBeforeRedirect(net::URLRequest* request, |
| 135 const GURL& new_location) OVERRIDE {} |
| 136 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE {} |
| 137 virtual void OnRawBytesRead(const net::URLRequest& request, |
| 138 int bytes_read) OVERRIDE {} |
| 139 virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE {} |
| 140 virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE {} |
| 141 |
| 142 virtual void OnPACScriptError(int line_number, |
| 143 const string16& error) OVERRIDE { |
| 144 } |
| 145 virtual AuthRequiredResponse OnAuthRequired( |
| 146 net::URLRequest* request, |
| 147 const net::AuthChallengeInfo& auth_info, |
| 148 const AuthCallback& callback, |
| 149 net::AuthCredentials* credentials) OVERRIDE { |
| 150 return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 151 } |
| 152 virtual bool CanGetCookies( |
| 153 const net::URLRequest* request, |
| 154 const net::CookieList& cookie_list) OVERRIDE { |
| 155 StaticCookiePolicy::Type policy_type = g_accept_all_cookies ? |
| 156 StaticCookiePolicy::ALLOW_ALL_COOKIES : |
| 157 StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES; |
| 158 |
| 159 StaticCookiePolicy policy(policy_type); |
| 160 int rv = policy.CanGetCookies( |
| 161 request->url(), request->first_party_for_cookies()); |
| 162 return rv == net::OK; |
| 163 } |
| 164 virtual bool CanSetCookie(const net::URLRequest* request, |
| 165 const std::string& cookie_line, |
| 166 net::CookieOptions* options) OVERRIDE { |
| 167 StaticCookiePolicy::Type policy_type = g_accept_all_cookies ? |
| 168 StaticCookiePolicy::ALLOW_ALL_COOKIES : |
| 169 StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES; |
| 170 |
| 171 StaticCookiePolicy policy(policy_type); |
| 172 int rv = policy.CanSetCookie( |
| 173 request->url(), request->first_party_for_cookies()); |
| 174 return rv == net::OK; |
| 175 } |
| 176 }; |
| 177 |
103 TestShellRequestContextParams* g_request_context_params = NULL; | 178 TestShellRequestContextParams* g_request_context_params = NULL; |
104 TestShellRequestContext* g_request_context = NULL; | 179 TestShellRequestContext* g_request_context = NULL; |
| 180 TestShellNetworkDelegate* g_network_delegate = NULL; |
105 base::Thread* g_cache_thread = NULL; | 181 base::Thread* g_cache_thread = NULL; |
106 bool g_accept_all_cookies = false; | |
107 | 182 |
108 struct FileOverHTTPParams { | 183 struct FileOverHTTPParams { |
109 FileOverHTTPParams(std::string in_file_path_template, GURL in_http_prefix) | 184 FileOverHTTPParams(std::string in_file_path_template, GURL in_http_prefix) |
110 : file_path_template(in_file_path_template), | 185 : file_path_template(in_file_path_template), |
111 http_prefix(in_http_prefix) {} | 186 http_prefix(in_http_prefix) {} |
112 | 187 |
113 std::string file_path_template; | 188 std::string file_path_template; |
114 GURL http_prefix; | 189 GURL http_prefix; |
115 }; | 190 }; |
116 | 191 |
(...skipping 17 matching lines...) Expand all Loading... |
134 g_request_context_params->cache_mode, | 209 g_request_context_params->cache_mode, |
135 g_request_context_params->no_proxy); | 210 g_request_context_params->no_proxy); |
136 delete g_request_context_params; | 211 delete g_request_context_params; |
137 g_request_context_params = NULL; | 212 g_request_context_params = NULL; |
138 } else { | 213 } else { |
139 g_request_context = new TestShellRequestContext(); | 214 g_request_context = new TestShellRequestContext(); |
140 } | 215 } |
141 | 216 |
142 g_request_context->AddRef(); | 217 g_request_context->AddRef(); |
143 | 218 |
| 219 g_network_delegate = new TestShellNetworkDelegate(); |
| 220 g_request_context->set_network_delegate(g_network_delegate); |
| 221 |
144 SimpleAppCacheSystem::InitializeOnIOThread(g_request_context); | 222 SimpleAppCacheSystem::InitializeOnIOThread(g_request_context); |
145 SimpleSocketStreamBridge::InitializeOnIOThread(g_request_context); | 223 SimpleSocketStreamBridge::InitializeOnIOThread(g_request_context); |
146 SimpleFileWriter::InitializeOnIOThread(g_request_context); | 224 SimpleFileWriter::InitializeOnIOThread(g_request_context); |
147 SimpleFileSystem::InitializeOnIOThread( | 225 SimpleFileSystem::InitializeOnIOThread( |
148 g_request_context->blob_storage_controller()); | 226 g_request_context->blob_storage_controller()); |
149 TestShellWebBlobRegistryImpl::InitializeOnIOThread( | 227 TestShellWebBlobRegistryImpl::InitializeOnIOThread( |
150 g_request_context->blob_storage_controller()); | 228 g_request_context->blob_storage_controller()); |
151 } | 229 } |
152 | 230 |
153 virtual void CleanUp() { | 231 virtual void CleanUp() { |
154 // In reverse order of initialization. | 232 // In reverse order of initialization. |
155 TestShellWebBlobRegistryImpl::Cleanup(); | 233 TestShellWebBlobRegistryImpl::Cleanup(); |
156 SimpleFileSystem::CleanupOnIOThread(); | 234 SimpleFileSystem::CleanupOnIOThread(); |
157 SimpleFileWriter::CleanupOnIOThread(); | 235 SimpleFileWriter::CleanupOnIOThread(); |
158 SimpleSocketStreamBridge::Cleanup(); | 236 SimpleSocketStreamBridge::Cleanup(); |
159 SimpleAppCacheSystem::CleanupOnIOThread(); | 237 SimpleAppCacheSystem::CleanupOnIOThread(); |
160 | 238 |
161 if (g_request_context) { | 239 if (g_request_context) { |
| 240 g_request_context->set_network_delegate(NULL); |
162 g_request_context->Release(); | 241 g_request_context->Release(); |
163 g_request_context = NULL; | 242 g_request_context = NULL; |
164 } | 243 } |
| 244 |
| 245 if (g_network_delegate) { |
| 246 delete g_network_delegate; |
| 247 g_network_delegate = NULL; |
| 248 } |
165 } | 249 } |
166 }; | 250 }; |
167 | 251 |
168 IOThread* g_io_thread = NULL; | 252 IOThread* g_io_thread = NULL; |
169 | 253 |
170 //----------------------------------------------------------------------------- | 254 //----------------------------------------------------------------------------- |
171 | 255 |
172 struct RequestParams { | 256 struct RequestParams { |
173 std::string method; | 257 std::string method; |
174 GURL url; | 258 GURL url; |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 } | 553 } |
470 } | 554 } |
471 | 555 |
472 virtual void OnSSLCertificateError(net::URLRequest* request, | 556 virtual void OnSSLCertificateError(net::URLRequest* request, |
473 const net::SSLInfo& ssl_info, | 557 const net::SSLInfo& ssl_info, |
474 bool fatal) OVERRIDE { | 558 bool fatal) OVERRIDE { |
475 // Allow all certificate errors. | 559 // Allow all certificate errors. |
476 request->ContinueDespiteLastError(); | 560 request->ContinueDespiteLastError(); |
477 } | 561 } |
478 | 562 |
479 virtual bool CanGetCookies( | |
480 const net::URLRequest* request, | |
481 const net::CookieList& cookie_list) const OVERRIDE { | |
482 StaticCookiePolicy::Type policy_type = g_accept_all_cookies ? | |
483 StaticCookiePolicy::ALLOW_ALL_COOKIES : | |
484 StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES; | |
485 | |
486 StaticCookiePolicy policy(policy_type); | |
487 int rv = policy.CanGetCookies( | |
488 request->url(), request->first_party_for_cookies()); | |
489 return rv == net::OK; | |
490 } | |
491 | |
492 virtual bool CanSetCookie(const net::URLRequest* request, | |
493 const std::string& cookie_line, | |
494 net::CookieOptions* options) const OVERRIDE { | |
495 StaticCookiePolicy::Type policy_type = g_accept_all_cookies ? | |
496 StaticCookiePolicy::ALLOW_ALL_COOKIES : | |
497 StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES; | |
498 | |
499 StaticCookiePolicy policy(policy_type); | |
500 int rv = policy.CanSetCookie( | |
501 request->url(), request->first_party_for_cookies()); | |
502 return rv == net::OK; | |
503 } | |
504 | |
505 virtual void OnReadCompleted(net::URLRequest* request, | 563 virtual void OnReadCompleted(net::URLRequest* request, |
506 int bytes_read) OVERRIDE { | 564 int bytes_read) OVERRIDE { |
507 if (request->status().is_success() && bytes_read > 0) { | 565 if (request->status().is_success() && bytes_read > 0) { |
508 OnReceivedData(bytes_read); | 566 OnReceivedData(bytes_read); |
509 } else { | 567 } else { |
510 Done(); | 568 Done(); |
511 } | 569 } |
512 } | 570 } |
513 | 571 |
514 // -------------------------------------------------------------------------- | 572 // -------------------------------------------------------------------------- |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
917 void SimpleResourceLoaderBridge::Init( | 975 void SimpleResourceLoaderBridge::Init( |
918 const FilePath& cache_path, | 976 const FilePath& cache_path, |
919 net::HttpCache::Mode cache_mode, | 977 net::HttpCache::Mode cache_mode, |
920 bool no_proxy) { | 978 bool no_proxy) { |
921 // Make sure to stop any existing IO thread since it may be using the | 979 // Make sure to stop any existing IO thread since it may be using the |
922 // current request context. | 980 // current request context. |
923 Shutdown(); | 981 Shutdown(); |
924 | 982 |
925 DCHECK(!g_request_context_params); | 983 DCHECK(!g_request_context_params); |
926 DCHECK(!g_request_context); | 984 DCHECK(!g_request_context); |
| 985 DCHECK(!g_network_delegate); |
927 DCHECK(!g_io_thread); | 986 DCHECK(!g_io_thread); |
928 | 987 |
929 g_request_context_params = new TestShellRequestContextParams( | 988 g_request_context_params = new TestShellRequestContextParams( |
930 cache_path, cache_mode, no_proxy); | 989 cache_path, cache_mode, no_proxy); |
931 } | 990 } |
932 | 991 |
933 // static | 992 // static |
934 void SimpleResourceLoaderBridge::Shutdown() { | 993 void SimpleResourceLoaderBridge::Shutdown() { |
935 if (g_io_thread) { | 994 if (g_io_thread) { |
936 delete g_io_thread; | 995 delete g_io_thread; |
937 g_io_thread = NULL; | 996 g_io_thread = NULL; |
938 | 997 |
939 DCHECK(g_cache_thread); | 998 DCHECK(g_cache_thread); |
940 delete g_cache_thread; | 999 delete g_cache_thread; |
941 g_cache_thread = NULL; | 1000 g_cache_thread = NULL; |
942 | 1001 |
943 DCHECK(!g_request_context) << "should have been nulled by thread dtor"; | 1002 DCHECK(!g_request_context) << "should have been nulled by thread dtor"; |
| 1003 DCHECK(!g_network_delegate) << "should have been nulled by thread dtor"; |
944 } else { | 1004 } else { |
945 delete g_request_context_params; | 1005 delete g_request_context_params; |
946 g_request_context_params = NULL; | 1006 g_request_context_params = NULL; |
947 | 1007 |
948 if (g_file_over_http_params) { | 1008 if (g_file_over_http_params) { |
949 delete g_file_over_http_params; | 1009 delete g_file_over_http_params; |
950 g_file_over_http_params = NULL; | 1010 g_file_over_http_params = NULL; |
951 } | 1011 } |
952 } | 1012 } |
953 } | 1013 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1040 (http_prefix.SchemeIs("http") || http_prefix.SchemeIs("https"))); | 1100 (http_prefix.SchemeIs("http") || http_prefix.SchemeIs("https"))); |
1041 g_file_over_http_params = new FileOverHTTPParams(file_path_template, | 1101 g_file_over_http_params = new FileOverHTTPParams(file_path_template, |
1042 http_prefix); | 1102 http_prefix); |
1043 } | 1103 } |
1044 | 1104 |
1045 // static | 1105 // static |
1046 webkit_glue::ResourceLoaderBridge* SimpleResourceLoaderBridge::Create( | 1106 webkit_glue::ResourceLoaderBridge* SimpleResourceLoaderBridge::Create( |
1047 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info) { | 1107 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info) { |
1048 return new ResourceLoaderBridgeImpl(request_info); | 1108 return new ResourceLoaderBridgeImpl(request_info); |
1049 } | 1109 } |
OLD | NEW |