summaryrefslogtreecommitdiffstats
path: root/chromium/extensions/browser/api/web_request/web_request_info.cc
blob: 7df82afbaf6ad2215dd2c27308b5ef3eda1501c6 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2017 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 "extensions/browser/api/web_request/web_request_info.h"

#include <memory>
#include <string>

#include "base/files/file_path.h"
#include "base/stl_util.h"
#include "base/values.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/websocket_handshake_request_info.h"
#include "extensions/browser/api/web_request/upload_data_presenter.h"
#include "extensions/browser/api/web_request/web_request_api_constants.h"
#include "extensions/browser/extension_api_frame_id_map.h"
#include "extensions/browser/extension_navigation_ui_data.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/guest_view/web_view/web_view_renderer_state.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"
#include "net/base/upload_file_element_reader.h"
#include "net/log/net_log_with_source.h"
#include "net/url_request/url_request.h"
#include "services/network/public/cpp/resource_response.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/url_loader.h"

namespace keys = extension_web_request_api_constants;

namespace extensions {

namespace {

// UploadDataSource abstracts an interface for feeding an arbitrary data element
// to an UploadDataPresenter. This is helpful because in the Network Service vs
// non-Network Service case, upload data comes from different types of source
// objects, but we'd like to share parsing code.
class UploadDataSource {
 public:
  virtual ~UploadDataSource() {}

  virtual void FeedToPresenter(UploadDataPresenter* presenter) = 0;
};

class BytesUploadDataSource : public UploadDataSource {
 public:
  BytesUploadDataSource(const base::StringPiece& bytes) : bytes_(bytes) {}
  ~BytesUploadDataSource() override = default;

  // UploadDataSource:
  void FeedToPresenter(UploadDataPresenter* presenter) override {
    presenter->FeedBytes(bytes_);
  }

 private:
  base::StringPiece bytes_;

  DISALLOW_COPY_AND_ASSIGN(BytesUploadDataSource);
};

class FileUploadDataSource : public UploadDataSource {
 public:
  FileUploadDataSource(const base::FilePath& path) : path_(path) {}
  ~FileUploadDataSource() override = default;

  // UploadDataSource:
  void FeedToPresenter(UploadDataPresenter* presenter) override {
    presenter->FeedFile(path_);
  }

 private:
  base::FilePath path_;

  DISALLOW_COPY_AND_ASSIGN(FileUploadDataSource);
};

std::unique_ptr<base::Value> NetLogExtensionIdCallback(
    const std::string& extension_id,
    net::NetLogCaptureMode capture_mode) {
  auto params = std::make_unique<base::DictionaryValue>();
  params->SetString("extension_id", extension_id);
  return params;
}

// Implements Logger using NetLog, mirroring the logging facilities used prior
// to the introduction of WebRequestInfo.
// TODO(crbug.com/721414): Transition away from using NetLog.
class NetLogLogger : public WebRequestInfo::Logger {
 public:
  explicit NetLogLogger(net::URLRequest* request) : request_(request) {}
  ~NetLogLogger() override = default;

  // WebRequestInfo::Logger:
  void LogEvent(net::NetLogEventType event_type,
                const std::string& extension_id) override {
    request_->net_log().AddEvent(
        event_type,
        base::BindRepeating(&NetLogExtensionIdCallback, extension_id));
  }

  void LogBlockedBy(const std::string& blocker_info) override {
    // LogAndReport allows extensions that block requests to be displayed in the
    // load status bar.
    request_->LogAndReportBlockedBy(blocker_info.c_str());
  }

  void LogUnblocked() override { request_->LogUnblocked(); }

 private:
  net::URLRequest* const request_;

  DISALLOW_COPY_AND_ASSIGN(NetLogLogger);
};

// TODO(https://crbug.com/721414): Need a real implementation here to support
// the Network Service case. For now this is only to prevent crashing.
class NetworkServiceLogger : public WebRequestInfo::Logger {
 public:
  NetworkServiceLogger() = default;
  ~NetworkServiceLogger() override = default;

  // WebRequestInfo::Logger:
  void LogEvent(net::NetLogEventType event_type,
                const std::string& extension_id) override {}
  void LogBlockedBy(const std::string& blocker_info) override {}
  void LogUnblocked() override {}

 private:
  DISALLOW_COPY_AND_ASSIGN(NetworkServiceLogger);
};

bool CreateUploadDataSourcesFromURLRequest(
    net::URLRequest* url_request,
    std::vector<std::unique_ptr<UploadDataSource>>* data_sources) {
  const net::UploadDataStream* upload_data = url_request->get_upload();
  if (!upload_data)
    return false;

  const std::vector<std::unique_ptr<net::UploadElementReader>>* readers =
      upload_data->GetElementReaders();
  if (!readers)
    return true;

  for (const auto& reader : *readers) {
    if (const auto* bytes_reader = reader->AsBytesReader()) {
      data_sources->push_back(std::make_unique<BytesUploadDataSource>(
          base::StringPiece(bytes_reader->bytes(), bytes_reader->length())));
    } else if (const auto* file_reader = reader->AsFileReader()) {
      data_sources->push_back(
          std::make_unique<FileUploadDataSource>(file_reader->path()));
    } else {
      DVLOG(1) << "Ignoring upsupported upload data type for WebRequest API.";
    }
  }

  return true;
}

bool CreateUploadDataSourcesFromResourceRequest(
    const network::ResourceRequest& request,
    std::vector<std::unique_ptr<UploadDataSource>>* data_sources) {
  if (!request.request_body)
    return false;

  for (auto& element : *request.request_body->elements()) {
    switch (element.type()) {
      case network::mojom::DataElementType::kDataPipe:
        // TODO(https://crbug.com/721414): Support data pipe elements.
        break;

      case network::mojom::DataElementType::kBytes:
        data_sources->push_back(std::make_unique<BytesUploadDataSource>(
            base::StringPiece(element.bytes(), element.length())));
        break;

      case network::mojom::DataElementType::kFile:
        // TODO(https://crbug.com/715679): This may not work when network
        // process is sandboxed.
        data_sources->push_back(
            std::make_unique<FileUploadDataSource>(element.path()));
        break;

      default:
        NOTIMPLEMENTED();
        break;
    }
  }

  return true;
}

std::unique_ptr<base::DictionaryValue> CreateRequestBodyData(
    const std::string& method,
    const net::HttpRequestHeaders& request_headers,
    const std::vector<std::unique_ptr<UploadDataSource>>& data_sources) {
  if (method != "POST" && method != "PUT")
    return nullptr;

  auto request_body_data = std::make_unique<base::DictionaryValue>();

  // Get the data presenters, ordered by how specific they are.
  ParsedDataPresenter parsed_data_presenter(request_headers);
  RawDataPresenter raw_data_presenter;
  UploadDataPresenter* const presenters[] = {
      &parsed_data_presenter,  // 1: any parseable forms? (Specific to forms.)
      &raw_data_presenter      // 2: any data at all? (Non-specific.)
  };
  // Keys for the results of the corresponding presenters.
  static const char* const kKeys[] = {keys::kRequestBodyFormDataKey,
                                      keys::kRequestBodyRawKey};
  bool some_succeeded = false;
  if (!data_sources.empty()) {
    for (size_t i = 0; i < base::size(presenters); ++i) {
      for (auto& source : data_sources)
        source->FeedToPresenter(presenters[i]);
      if (presenters[i]->Succeeded()) {
        request_body_data->Set(kKeys[i], presenters[i]->Result());
        some_succeeded = true;
        break;
      }
    }
  }

  if (!some_succeeded)
    request_body_data->SetString(keys::kRequestBodyErrorKey, "Unknown error.");

  return request_body_data;
}

}  // namespace

WebRequestInfo::WebRequestInfo() = default;
WebRequestInfo::WebRequestInfo(WebRequestInfo&& other) = default;
WebRequestInfo& WebRequestInfo::operator=(WebRequestInfo&& other) = default;

WebRequestInfo::WebRequestInfo(net::URLRequest* url_request)
    : id(url_request->identifier()),
      url(url_request->url()),
      site_for_cookies(url_request->site_for_cookies()),
      method(url_request->method()),
      initiator(url_request->initiator()),
      extra_request_headers(url_request->extra_request_headers()),
      is_pac_request(url_request->is_pac_request()),
      logger(std::make_unique<NetLogLogger>(url_request)) {
  if (url.SchemeIsWSOrWSS()) {
    web_request_type = WebRequestResourceType::WEB_SOCKET;

    // TODO(pkalinnikov): Consider embedding WebSocketHandshakeRequestInfo into
    // UrlRequestUserData.
    const content::WebSocketHandshakeRequestInfo* ws_info =
        content::WebSocketHandshakeRequestInfo::ForRequest(url_request);
    if (ws_info) {
      render_process_id = ws_info->GetChildId();
      frame_id = ws_info->GetRenderFrameId();
    }
  } else if (auto* info =
                 content::ResourceRequestInfo::ForRequest(url_request)) {
    render_process_id = info->GetChildID();
    routing_id = info->GetRouteID();
    frame_id = info->GetRenderFrameID();
    type = info->GetResourceType();
    web_request_type = ToWebRequestResourceType(type.value());
    is_async = info->IsAsync();
    resource_context = info->GetContext();
  } else if (auto* url_loader = network::URLLoader::ForRequest(*url_request)) {
    // This is reached only in the SimpleURLLoader case (since network service
    // is disabled if we're in this constructor). Only set the IDs if they're
    // non-zero, since almost all requests come from the browser and aren't
    // associated with a frame. In the case that the browser wants this
    // SimpleURLLoader associated with a frame, the process ID will be non-zero.
    if (url_loader->GetProcessId() != network::mojom::kBrowserProcessId) {
      render_process_id = url_loader->GetProcessId();
      frame_id = url_loader->GetRenderFrameId();
    }
  } else {
    // There may be basic process and frame info associated with the request
    // even when |info| is null. Attempt to grab it as a last ditch effort. If
    // this fails, we have no frame info.
    content::ResourceRequestInfo::GetRenderFrameForRequest(
        url_request, &render_process_id, &frame_id);
  }

  ExtensionsBrowserClient* browser_client = ExtensionsBrowserClient::Get();
  ExtensionNavigationUIData* navigation_ui_data =
      browser_client ? browser_client->GetExtensionNavigationUIData(url_request)
                     : nullptr;
  if (navigation_ui_data)
    is_browser_side_navigation = true;

  InitializeWebViewAndFrameData(navigation_ui_data);

  std::vector<std::unique_ptr<UploadDataSource>> data_sources;
  if (CreateUploadDataSourcesFromURLRequest(url_request, &data_sources)) {
    request_body_data =
        CreateRequestBodyData(method, extra_request_headers, data_sources);
  }
}

WebRequestInfo::WebRequestInfo(
    uint64_t request_id,
    int render_process_id,
    int render_frame_id,
    std::unique_ptr<ExtensionNavigationUIData> navigation_ui_data,
    int32_t routing_id,
    content::ResourceContext* resource_context,
    const network::ResourceRequest& request,
    bool is_download,
    bool is_async)
    : id(request_id),
      url(request.url),
      site_for_cookies(request.site_for_cookies),
      render_process_id(render_process_id),
      routing_id(routing_id),
      frame_id(render_frame_id),
      method(request.method),
      is_browser_side_navigation(!!navigation_ui_data),
      initiator(request.request_initiator),
      type(static_cast<content::ResourceType>(request.resource_type)),
      is_async(is_async),
      extra_request_headers(request.headers),
      logger(std::make_unique<NetworkServiceLogger>()),
      resource_context(resource_context) {
  if (url.SchemeIsWSOrWSS())
    web_request_type = WebRequestResourceType::WEB_SOCKET;
  else if (is_download)
    web_request_type = WebRequestResourceType::OTHER;
  else
    web_request_type = ToWebRequestResourceType(type.value());

  InitializeWebViewAndFrameData(navigation_ui_data.get());

  std::vector<std::unique_ptr<UploadDataSource>> data_sources;
  if (CreateUploadDataSourcesFromResourceRequest(request, &data_sources)) {
    request_body_data =
        CreateRequestBodyData(method, extra_request_headers, data_sources);
  }

  // TODO(https://crbug.com/721414): For this constructor (i.e. the Network
  // Service case), we are still missing information for |is_async| and
  // |is_pac_request|.
}

WebRequestInfo::~WebRequestInfo() = default;

void WebRequestInfo::AddResponseInfoFromURLRequest(
    net::URLRequest* url_request) {
  response_code = url_request->GetResponseCode();
  response_headers = url_request->response_headers();
  response_ip = url_request->GetSocketAddress().host();
  response_from_cache = url_request->was_cached();
}

void WebRequestInfo::AddResponseInfoFromResourceResponse(
    const network::ResourceResponseHead& response) {
  response_headers = response.headers;
  if (response_headers)
    response_code = response_headers->response_code();
  response_ip = response.socket_address.host();
  response_from_cache = response.was_fetched_via_cache;
}

void WebRequestInfo::InitializeWebViewAndFrameData(
    const ExtensionNavigationUIData* navigation_ui_data) {
  if (navigation_ui_data) {
    is_web_view = navigation_ui_data->is_web_view();
    web_view_instance_id = navigation_ui_data->web_view_instance_id();
    web_view_rules_registry_id =
        navigation_ui_data->web_view_rules_registry_id();
    frame_data = navigation_ui_data->frame_data();
  } else if (frame_id >= 0) {
    // Grab any WebView-related information if relevant.
    WebViewRendererState::WebViewInfo web_view_info;
    if (WebViewRendererState::GetInstance()->GetInfo(
            render_process_id, routing_id, &web_view_info)) {
      is_web_view = true;
      web_view_instance_id = web_view_info.instance_id;
      web_view_rules_registry_id = web_view_info.rules_registry_id;
      web_view_embedder_process_id = web_view_info.embedder_process_id;
    }

    // For subresource loads we attempt to resolve the FrameData immediately
    // anyway using cached information.
    ExtensionApiFrameIdMap::FrameData data;
    bool was_cached = ExtensionApiFrameIdMap::Get()->GetCachedFrameDataOnIO(
        render_process_id, frame_id, &data);
    // TODO(crbug.com/843762): Investigate when |was_cached| can be false. It
    // seems we are not tracking all WebContents or that the corresponding
    // render frame was destroyed. Track where this can occur, this should help
    // in minimizing IO->UI->IO thread that the web request API performs to
    // fetch the frame data.
    if (was_cached)
      frame_data = data;
  }
}

}  // namespace extensions