f55b36aac95f50e37add07d8180d5d7eef770676
[platform/framework/web/crosswalk.git] / src / android_webview / browser / renderer_host / aw_resource_dispatcher_host_delegate.cc
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
3 // found in the LICENSE file.
4
5 #include "android_webview/browser/renderer_host/aw_resource_dispatcher_host_delegate.h"
6
7 #include <string>
8
9 #include "android_webview/browser/aw_contents_io_thread_client.h"
10 #include "android_webview/browser/aw_login_delegate.h"
11 #include "android_webview/browser/aw_resource_context.h"
12 #include "android_webview/common/url_constants.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "components/auto_login_parser/auto_login_parser.h"
16 #include "components/navigation_interception/intercept_navigation_delegate.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/resource_controller.h"
19 #include "content/public/browser/resource_dispatcher_host.h"
20 #include "content/public/browser/resource_dispatcher_host_login_delegate.h"
21 #include "content/public/browser/resource_request_info.h"
22 #include "content/public/browser/resource_throttle.h"
23 #include "net/base/load_flags.h"
24 #include "net/base/net_errors.h"
25 #include "net/http/http_response_headers.h"
26 #include "net/url_request/url_request.h"
27 #include "url/url_constants.h"
28
29 using android_webview::AwContentsIoThreadClient;
30 using content::BrowserThread;
31 using content::ResourceType;
32 using navigation_interception::InterceptNavigationDelegate;
33
34 namespace {
35
36 base::LazyInstance<android_webview::AwResourceDispatcherHostDelegate>
37     g_webview_resource_dispatcher_host_delegate = LAZY_INSTANCE_INITIALIZER;
38
39 void SetCacheControlFlag(
40     net::URLRequest* request, int flag) {
41   const int all_cache_control_flags = net::LOAD_BYPASS_CACHE |
42       net::LOAD_VALIDATE_CACHE |
43       net::LOAD_PREFERRING_CACHE |
44       net::LOAD_ONLY_FROM_CACHE;
45   DCHECK((flag & all_cache_control_flags) == flag);
46   int load_flags = request->load_flags();
47   load_flags &= ~all_cache_control_flags;
48   load_flags |= flag;
49   request->SetLoadFlags(load_flags);
50 }
51
52 }  // namespace
53
54 namespace android_webview {
55
56 // Calls through the IoThreadClient to check the embedders settings to determine
57 // if the request should be cancelled. There may not always be an IoThreadClient
58 // available for the |render_process_id|, |render_frame_id| pair (in the case of
59 // newly created pop up windows, for example) and in that case the request and
60 // the client callbacks will be deferred the request until a client is ready.
61 class IoThreadClientThrottle : public content::ResourceThrottle {
62  public:
63   IoThreadClientThrottle(int render_process_id,
64                          int render_frame_id,
65                          net::URLRequest* request);
66   virtual ~IoThreadClientThrottle();
67
68   // From content::ResourceThrottle
69   virtual void WillStartRequest(bool* defer) OVERRIDE;
70   virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE;
71   virtual const char* GetNameForLogging() const OVERRIDE;
72
73   void OnIoThreadClientReady(int new_render_process_id,
74                              int new_render_frame_id);
75   bool MaybeBlockRequest();
76   bool ShouldBlockRequest();
77   int render_process_id() const { return render_process_id_; }
78   int render_frame_id() const { return render_frame_id_; }
79
80  private:
81   int render_process_id_;
82   int render_frame_id_;
83   net::URLRequest* request_;
84 };
85
86 IoThreadClientThrottle::IoThreadClientThrottle(int render_process_id,
87                                                int render_frame_id,
88                                                net::URLRequest* request)
89     : render_process_id_(render_process_id),
90       render_frame_id_(render_frame_id),
91       request_(request) { }
92
93 IoThreadClientThrottle::~IoThreadClientThrottle() {
94   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
95   g_webview_resource_dispatcher_host_delegate.Get().
96       RemovePendingThrottleOnIoThread(this);
97 }
98
99 const char* IoThreadClientThrottle::GetNameForLogging() const {
100   return "IoThreadClientThrottle";
101 }
102
103 void IoThreadClientThrottle::WillStartRequest(bool* defer) {
104   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
105   // TODO(sgurun): This block can be removed when crbug.com/277937 is fixed.
106   if (render_frame_id_ < 1) {
107     // OPTIONS is used for preflighted requests which are generated internally.
108     DCHECK_EQ("OPTIONS", request_->method());
109     return;
110   }
111   DCHECK(render_process_id_);
112   *defer = false;
113
114   // Defer all requests of a pop up that is still not associated with Java
115   // client so that the client will get a chance to override requests.
116   scoped_ptr<AwContentsIoThreadClient> io_client =
117       AwContentsIoThreadClient::FromID(render_process_id_, render_frame_id_);
118   if (io_client && io_client->PendingAssociation()) {
119     *defer = true;
120     AwResourceDispatcherHostDelegate::AddPendingThrottle(
121         render_process_id_, render_frame_id_, this);
122   } else {
123     MaybeBlockRequest();
124   }
125 }
126
127 void IoThreadClientThrottle::WillRedirectRequest(const GURL& new_url,
128                                                  bool* defer) {
129   WillStartRequest(defer);
130 }
131
132 void IoThreadClientThrottle::OnIoThreadClientReady(int new_render_process_id,
133                                                    int new_render_frame_id) {
134   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
135
136   if (!MaybeBlockRequest()) {
137     controller()->Resume();
138   }
139 }
140
141 bool IoThreadClientThrottle::MaybeBlockRequest() {
142   if (ShouldBlockRequest()) {
143     controller()->CancelWithError(net::ERR_ACCESS_DENIED);
144     return true;
145   }
146   return false;
147 }
148
149 bool IoThreadClientThrottle::ShouldBlockRequest() {
150   scoped_ptr<AwContentsIoThreadClient> io_client =
151       AwContentsIoThreadClient::FromID(render_process_id_, render_frame_id_);
152   if (!io_client)
153     return false;
154
155   // Part of implementation of WebSettings.allowContentAccess.
156   if (request_->url().SchemeIs(android_webview::kContentScheme) &&
157       io_client->ShouldBlockContentUrls()) {
158     return true;
159   }
160
161   // Part of implementation of WebSettings.allowFileAccess.
162   if (request_->url().SchemeIsFile() &&
163       io_client->ShouldBlockFileUrls()) {
164     const GURL& url = request_->url();
165     if (!url.has_path() ||
166         // Application's assets and resources are always available.
167         (url.path().find(android_webview::kAndroidResourcePath) != 0 &&
168          url.path().find(android_webview::kAndroidAssetPath) != 0)) {
169       return true;
170     }
171   }
172
173   if (io_client->ShouldBlockNetworkLoads()) {
174     if (request_->url().SchemeIs(url::kFtpScheme)) {
175       return true;
176     }
177     SetCacheControlFlag(request_, net::LOAD_ONLY_FROM_CACHE);
178   } else {
179     AwContentsIoThreadClient::CacheMode cache_mode = io_client->GetCacheMode();
180     switch(cache_mode) {
181       case AwContentsIoThreadClient::LOAD_CACHE_ELSE_NETWORK:
182         SetCacheControlFlag(request_, net::LOAD_PREFERRING_CACHE);
183         break;
184       case AwContentsIoThreadClient::LOAD_NO_CACHE:
185         SetCacheControlFlag(request_, net::LOAD_BYPASS_CACHE);
186         break;
187       case AwContentsIoThreadClient::LOAD_CACHE_ONLY:
188         SetCacheControlFlag(request_, net::LOAD_ONLY_FROM_CACHE);
189         break;
190       default:
191         break;
192     }
193   }
194   return false;
195 }
196
197 // static
198 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() {
199   content::ResourceDispatcherHost::Get()->SetDelegate(
200       &g_webview_resource_dispatcher_host_delegate.Get());
201 }
202
203 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate()
204     : content::ResourceDispatcherHostDelegate() {
205 }
206
207 AwResourceDispatcherHostDelegate::~AwResourceDispatcherHostDelegate() {
208 }
209
210 void AwResourceDispatcherHostDelegate::RequestBeginning(
211     net::URLRequest* request,
212     content::ResourceContext* resource_context,
213     content::AppCacheService* appcache_service,
214     ResourceType resource_type,
215     int child_id,
216     int route_id,
217     ScopedVector<content::ResourceThrottle>* throttles) {
218
219   AddExtraHeadersIfNeeded(request, resource_context);
220
221   const content::ResourceRequestInfo* request_info =
222       content::ResourceRequestInfo::ForRequest(request);
223
224   // We always push the throttles here. Checking the existence of io_client
225   // is racy when a popup window is created. That is because RequestBeginning
226   // is called whether or not requests are blocked via BlockRequestForRoute()
227   // however io_client may or may not be ready at the time depending on whether
228   // webcontents is created.
229   throttles->push_back(new IoThreadClientThrottle(
230       child_id, request_info->GetRenderFrameID(), request));
231
232   // We allow intercepting only navigations within main frames. This
233   // is used to post onPageStarted. We handle shouldOverrideUrlLoading
234   // via a sync IPC.
235   if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME)
236     throttles->push_back(InterceptNavigationDelegate::CreateThrottleFor(
237         request));
238 }
239
240 void AwResourceDispatcherHostDelegate::OnRequestRedirected(
241     const GURL& redirect_url,
242     net::URLRequest* request,
243     content::ResourceContext* resource_context,
244     content::ResourceResponse* response) {
245   AddExtraHeadersIfNeeded(request, resource_context);
246 }
247
248
249 void AwResourceDispatcherHostDelegate::DownloadStarting(
250     net::URLRequest* request,
251     content::ResourceContext* resource_context,
252     int child_id,
253     int route_id,
254     int request_id,
255     bool is_content_initiated,
256     bool must_download,
257     ScopedVector<content::ResourceThrottle>* throttles) {
258   GURL url(request->url());
259   std::string user_agent;
260   std::string content_disposition;
261   std::string mime_type;
262   int64 content_length = request->GetExpectedContentSize();
263
264   request->extra_request_headers().GetHeader(
265       net::HttpRequestHeaders::kUserAgent, &user_agent);
266
267
268   net::HttpResponseHeaders* response_headers = request->response_headers();
269   if (response_headers) {
270     response_headers->GetNormalizedHeader("content-disposition",
271         &content_disposition);
272     response_headers->GetMimeType(&mime_type);
273   }
274
275   request->Cancel();
276
277   const content::ResourceRequestInfo* request_info =
278       content::ResourceRequestInfo::ForRequest(request);
279
280   scoped_ptr<AwContentsIoThreadClient> io_client =
281       AwContentsIoThreadClient::FromID(
282           child_id, request_info->GetRenderFrameID());
283
284   // POST request cannot be repeated in general, so prevent client from
285   // retrying the same request, even if it is with a GET.
286   if ("GET" == request->method() && io_client) {
287     io_client->NewDownload(url,
288                            user_agent,
289                            content_disposition,
290                            mime_type,
291                            content_length);
292   }
293 }
294
295 content::ResourceDispatcherHostLoginDelegate*
296     AwResourceDispatcherHostDelegate::CreateLoginDelegate(
297         net::AuthChallengeInfo* auth_info,
298         net::URLRequest* request) {
299   return new AwLoginDelegate(auth_info, request);
300 }
301
302 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url,
303                                                               int child_id,
304                                                               int route_id) {
305   // The AwURLRequestJobFactory implementation should ensure this method never
306   // gets called.
307   NOTREACHED();
308   return false;
309 }
310
311 void AwResourceDispatcherHostDelegate::OnResponseStarted(
312     net::URLRequest* request,
313     content::ResourceContext* resource_context,
314     content::ResourceResponse* response,
315     IPC::Sender* sender) {
316   const content::ResourceRequestInfo* request_info =
317       content::ResourceRequestInfo::ForRequest(request);
318   if (!request_info) {
319     DLOG(FATAL) << "Started request without associated info: " <<
320         request->url();
321     return;
322   }
323
324   if (request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) {
325     // Check for x-auto-login header.
326     auto_login_parser::HeaderData header_data;
327     if (auto_login_parser::ParserHeaderInResponse(
328             request, auto_login_parser::ALLOW_ANY_REALM, &header_data)) {
329       scoped_ptr<AwContentsIoThreadClient> io_client =
330           AwContentsIoThreadClient::FromID(request_info->GetChildID(),
331                                            request_info->GetRenderFrameID());
332       if (io_client) {
333         io_client->NewLoginRequest(
334             header_data.realm, header_data.account, header_data.args);
335       }
336     }
337   }
338 }
339
340 void AwResourceDispatcherHostDelegate::RemovePendingThrottleOnIoThread(
341     IoThreadClientThrottle* throttle) {
342   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
343   PendingThrottleMap::iterator it = pending_throttles_.find(
344       FrameRouteIDPair(throttle->render_process_id(),
345                        throttle->render_frame_id()));
346   if (it != pending_throttles_.end()) {
347     pending_throttles_.erase(it);
348   }
349 }
350
351 // static
352 void AwResourceDispatcherHostDelegate::OnIoThreadClientReady(
353     int new_render_process_id,
354     int new_render_frame_id) {
355   BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
356       base::Bind(
357           &AwResourceDispatcherHostDelegate::OnIoThreadClientReadyInternal,
358           base::Unretained(
359               g_webview_resource_dispatcher_host_delegate.Pointer()),
360           new_render_process_id, new_render_frame_id));
361 }
362
363 // static
364 void AwResourceDispatcherHostDelegate::AddPendingThrottle(
365     int render_process_id,
366     int render_frame_id,
367     IoThreadClientThrottle* pending_throttle) {
368   BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
369       base::Bind(
370           &AwResourceDispatcherHostDelegate::AddPendingThrottleOnIoThread,
371           base::Unretained(
372               g_webview_resource_dispatcher_host_delegate.Pointer()),
373           render_process_id, render_frame_id, pending_throttle));
374 }
375
376 void AwResourceDispatcherHostDelegate::AddPendingThrottleOnIoThread(
377     int render_process_id,
378     int render_frame_id_id,
379     IoThreadClientThrottle* pending_throttle) {
380   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
381   pending_throttles_.insert(
382       std::pair<FrameRouteIDPair, IoThreadClientThrottle*>(
383           FrameRouteIDPair(render_process_id, render_frame_id_id),
384           pending_throttle));
385 }
386
387 void AwResourceDispatcherHostDelegate::OnIoThreadClientReadyInternal(
388     int new_render_process_id,
389     int new_render_frame_id) {
390   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
391   PendingThrottleMap::iterator it = pending_throttles_.find(
392       FrameRouteIDPair(new_render_process_id, new_render_frame_id));
393
394   if (it != pending_throttles_.end()) {
395     IoThreadClientThrottle* throttle = it->second;
396     throttle->OnIoThreadClientReady(new_render_process_id, new_render_frame_id);
397     pending_throttles_.erase(it);
398   }
399 }
400
401 void AwResourceDispatcherHostDelegate::AddExtraHeadersIfNeeded(
402     net::URLRequest* request,
403     content::ResourceContext* resource_context) {
404   const content::ResourceRequestInfo* request_info =
405       content::ResourceRequestInfo::ForRequest(request);
406   if (!request_info)
407     return;
408   if (request_info->GetResourceType() != content::RESOURCE_TYPE_MAIN_FRAME)
409     return;
410
411   const content::PageTransition transition = request_info->GetPageTransition();
412   const bool is_load_url =
413       transition & content::PAGE_TRANSITION_FROM_API;
414   const bool is_go_back_forward =
415       transition & content::PAGE_TRANSITION_FORWARD_BACK;
416   const bool is_reload = content::PageTransitionCoreTypeIs(
417       transition, content::PAGE_TRANSITION_RELOAD);
418   if (is_load_url || is_go_back_forward || is_reload) {
419     AwResourceContext* awrc = static_cast<AwResourceContext*>(resource_context);
420     std::string extra_headers = awrc->GetExtraHeaders(request->url());
421     if (!extra_headers.empty()) {
422       net::HttpRequestHeaders headers;
423       headers.AddHeadersFromString(extra_headers);
424       for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext(); ) {
425         request->SetExtraRequestHeaderByName(it.name(), it.value(), false);
426       }
427     }
428   }
429 }
430
431 }  // namespace android_webview