summaryrefslogtreecommitdiffstats
path: root/android_webview/browser/net/aw_request_interceptor.cc
blob: 62e06197aeb93dba9f2fd91efc5ffcf3389d5125 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// 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/net/aw_request_interceptor.h"

#include <utility>

#include "android_webview/browser/aw_contents_io_thread_client.h"
#include "android_webview/browser/input_stream.h"
#include "android_webview/browser/net/android_stream_reader_url_request_job.h"
#include "android_webview/browser/net/aw_web_resource_response.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/supports_user_data.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_request_info.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request_job.h"

namespace android_webview {

namespace {

const void* const kRequestAlreadyHasJobDataKey = &kRequestAlreadyHasJobDataKey;

class StreamReaderJobDelegateImpl
    : public AndroidStreamReaderURLRequestJob::Delegate {
 public:
  StreamReaderJobDelegateImpl(
      scoped_ptr<AwWebResourceResponse> aw_web_resource_response)
      : aw_web_resource_response_(std::move(aw_web_resource_response)) {
    DCHECK(aw_web_resource_response_);
  }

  scoped_ptr<InputStream> OpenInputStream(JNIEnv* env,
                                          const GURL& url) override {
    return aw_web_resource_response_->GetInputStream(env);
  }

  void OnInputStreamOpenFailed(net::URLRequest* request,
                               bool* restart) override {
    *restart = false;
  }

  bool GetMimeType(JNIEnv* env,
                   net::URLRequest* request,
                   android_webview::InputStream* stream,
                   std::string* mime_type) override {
    return aw_web_resource_response_->GetMimeType(env, mime_type);
  }

  bool GetCharset(JNIEnv* env,
                  net::URLRequest* request,
                  android_webview::InputStream* stream,
                  std::string* charset) override {
    return aw_web_resource_response_->GetCharset(env, charset);
  }

  void AppendResponseHeaders(JNIEnv* env,
                             net::HttpResponseHeaders* headers) override {
    int status_code;
    std::string reason_phrase;
    if (aw_web_resource_response_->GetStatusInfo(
            env, &status_code, &reason_phrase)) {
      std::string status_line("HTTP/1.1 ");
      status_line.append(base::IntToString(status_code));
      status_line.append(" ");
      status_line.append(reason_phrase);
      headers->ReplaceStatusLine(status_line);
    }
    aw_web_resource_response_->GetResponseHeaders(env, headers);
  }

 private:
  scoped_ptr<AwWebResourceResponse> aw_web_resource_response_;
};

class ShouldInterceptRequestAdaptor
    : public AndroidStreamReaderURLRequestJob::DelegateObtainer {
 public:
  explicit ShouldInterceptRequestAdaptor(
      scoped_ptr<AwContentsIoThreadClient> io_thread_client)
      : io_thread_client_(std::move(io_thread_client)), weak_factory_(this) {}
   ~ShouldInterceptRequestAdaptor() override {}

  void ObtainDelegate(net::URLRequest* request,
                      const Callback& callback) override {
    callback_ = callback;
    io_thread_client_->ShouldInterceptRequestAsync(
        // The request is only used while preparing the call, not retained.
        request,
        base::Bind(&ShouldInterceptRequestAdaptor::WebResourceResponseObtained,
                   // The lifetime of the DelegateObtainer is managed by
                   // AndroidStreamReaderURLRequestJob, it might get deleted.
                   weak_factory_.GetWeakPtr()));
  }

 private:
  void WebResourceResponseObtained(
      scoped_ptr<AwWebResourceResponse> response) {
    if (response) {
      callback_.Run(make_scoped_ptr(
          new StreamReaderJobDelegateImpl(std::move(response))));
    } else {
      callback_.Run(nullptr);
    }
  }

  scoped_ptr<AwContentsIoThreadClient> io_thread_client_;
  Callback callback_;
  base::WeakPtrFactory<ShouldInterceptRequestAdaptor> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(ShouldInterceptRequestAdaptor);
};

scoped_ptr<AwContentsIoThreadClient> GetCorrespondingIoThreadClient(
    net::URLRequest* request) {
  if (content::ResourceRequestInfo::OriginatedFromServiceWorker(request))
    return AwContentsIoThreadClient::GetServiceWorkerIoThreadClient();

  int render_process_id, render_frame_id;
  if (!content::ResourceRequestInfo::GetRenderFrameForRequest(
      request, &render_process_id, &render_frame_id)) {
    return nullptr;
  }

  return AwContentsIoThreadClient::FromID(render_process_id, render_frame_id);
}

}  // namespace

AwRequestInterceptor::AwRequestInterceptor() {}

AwRequestInterceptor::~AwRequestInterceptor() {}

net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest(
    net::URLRequest* request,
    net::NetworkDelegate* network_delegate) const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

  // MaybeInterceptRequest can be called multiple times for the same request.
  if (request->GetUserData(kRequestAlreadyHasJobDataKey))
    return nullptr;

  scoped_ptr<AwContentsIoThreadClient> io_thread_client =
      GetCorrespondingIoThreadClient(request);

  if (!io_thread_client)
    return nullptr;

  GURL referrer(request->referrer());
  if (referrer.is_valid() &&
      (!request->is_pending() || request->is_redirecting())) {
    request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer,
                                         referrer.spec(), true);
  }
  request->SetUserData(kRequestAlreadyHasJobDataKey,
                       new base::SupportsUserData::Data());
  return new AndroidStreamReaderURLRequestJob(
      request, network_delegate,
      make_scoped_ptr(
          new ShouldInterceptRequestAdaptor(std::move(io_thread_client))),
      true);
}

}  // namespace android_webview