summaryrefslogtreecommitdiffstats
path: root/android_webview/browser/aw_request_interceptor.cc
blob: 0e5e28395f3cc7dcc5b53e9013c6e6e498895c63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "android_webview/browser/aw_request_interceptor.h"

#include "android_webview/browser/aw_contents_io_thread_client.h"
#include "android_webview/browser/intercepted_request_data.h"
#include "base/android/jni_string.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/resource_request_info.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_job.h"

using content::BrowserThread;
using content::RenderViewHost;
using content::ResourceRequestInfo;

namespace android_webview {

namespace {

const void* kURLRequestUserDataKey = &kURLRequestUserDataKey;

class URLRequestUserData : public base::SupportsUserData::Data {
 public:
    URLRequestUserData(
        scoped_ptr<InterceptedRequestData> intercepted_request_data)
        : intercepted_request_data_(intercepted_request_data.Pass()) {
    }

    static URLRequestUserData* Get(net::URLRequest* request) {
      return reinterpret_cast<URLRequestUserData*>(
          request->GetUserData(kURLRequestUserDataKey));
    }

    const InterceptedRequestData* intercepted_request_data() const {
      return intercepted_request_data_.get();
    }

 private:
  scoped_ptr<InterceptedRequestData> intercepted_request_data_;
};

} // namespace

AwRequestInterceptor::AwRequestInterceptor() {
}

AwRequestInterceptor::~AwRequestInterceptor() {
}

scoped_ptr<InterceptedRequestData>
AwRequestInterceptor::QueryForInterceptedRequestData(
    const GURL& location,
    net::URLRequest* request) const {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  int render_process_id, render_view_id;
  if (!ResourceRequestInfo::GetRenderViewForRequest(
      request, &render_process_id, &render_view_id))
    return scoped_ptr<InterceptedRequestData>();

  scoped_ptr<AwContentsIoThreadClient> io_thread_client =
    AwContentsIoThreadClient::FromID(render_process_id, render_view_id);

  if (!io_thread_client.get())
    return scoped_ptr<InterceptedRequestData>();

  return io_thread_client->ShouldInterceptRequest(location, request).Pass();
}

net::URLRequestJob* AwRequestInterceptor::MaybeCreateJob(
    net::URLRequest* request,
    net::NetworkDelegate* network_delegate) const {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  // See if we've already found out the intercepted_request_data for this
  // request.
  // This is done not only for efficiency reasons, but also for correctness
  // as it is possible for the Interceptor chain to be invoked more than once
  // (in which case we don't want to query the embedder multiple times).
  URLRequestUserData* user_data = URLRequestUserData::Get(request);

  if (!user_data) {
    // To ensure we only query the embedder once, we rely on the fact that the
    // user_data object will be created and attached to the URLRequest after a
    // call to QueryForInterceptedRequestData is made (regardless of whether
    // the result of that call is a valid InterceptedRequestData* pointer or
    // NULL.
    user_data = new URLRequestUserData(
        QueryForInterceptedRequestData(request->url(), request));
    request->SetUserData(kURLRequestUserDataKey, user_data);
  }

  const InterceptedRequestData* intercepted_request_data =
      user_data->intercepted_request_data();

  if (!intercepted_request_data)
    return NULL;
  return intercepted_request_data->CreateJobFor(request, network_delegate);
}

} // namespace android_webview