OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "extensions/browser/api/web_request/web_request_event_details.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/resource_request_info.h" |
| 12 #include "content/public/common/child_process_host.h" |
| 13 #include "extensions/browser/api/web_request/upload_data_presenter.h" |
| 14 #include "extensions/browser/api/web_request/web_request_api_constants.h" |
| 15 #include "extensions/browser/api/web_request/web_request_api_helpers.h" |
| 16 #include "extensions/browser/extension_api_frame_id_map.h" |
| 17 #include "ipc/ipc_message.h" |
| 18 #include "net/base/auth.h" |
| 19 #include "net/base/upload_data_stream.h" |
| 20 #include "net/http/http_request_headers.h" |
| 21 #include "net/http/http_response_headers.h" |
| 22 #include "net/url_request/url_request.h" |
| 23 |
| 24 using extension_web_request_api_helpers::ExtraInfoSpec; |
| 25 |
| 26 namespace helpers = extension_web_request_api_helpers; |
| 27 namespace keys = extension_web_request_api_constants; |
| 28 |
| 29 namespace extensions { |
| 30 |
| 31 WebRequestEventDetails::WebRequestEventDetails(const net::URLRequest* request, |
| 32 int extra_info_spec) |
| 33 : extra_info_spec_(extra_info_spec), |
| 34 render_process_id_(content::ChildProcessHost::kInvalidUniqueID), |
| 35 render_frame_id_(MSG_ROUTING_NONE) { |
| 36 content::ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE; |
| 37 const content::ResourceRequestInfo* info = |
| 38 content::ResourceRequestInfo::ForRequest(request); |
| 39 if (info) { |
| 40 render_process_id_ = info->GetChildID(); |
| 41 render_frame_id_ = info->GetRenderFrameID(); |
| 42 resource_type = info->GetResourceType(); |
| 43 } |
| 44 |
| 45 dict_.SetString(keys::kMethodKey, request->method()); |
| 46 dict_.SetString(keys::kRequestIdKey, |
| 47 base::Uint64ToString(request->identifier())); |
| 48 dict_.SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); |
| 49 dict_.SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); |
| 50 dict_.SetString(keys::kUrlKey, request->url().spec()); |
| 51 } |
| 52 |
| 53 WebRequestEventDetails::~WebRequestEventDetails() {} |
| 54 |
| 55 void WebRequestEventDetails::SetRequestBody(const net::URLRequest* request) { |
| 56 if (!(extra_info_spec_ & ExtraInfoSpec::REQUEST_BODY)) |
| 57 return; |
| 58 |
| 59 const net::UploadDataStream* upload_data = request->get_upload(); |
| 60 if (!upload_data || |
| 61 (request->method() != "POST" && request->method() != "PUT")) |
| 62 return; |
| 63 |
| 64 base::DictionaryValue* request_body = new base::DictionaryValue(); |
| 65 request_body_.reset(request_body); |
| 66 |
| 67 // Get the data presenters, ordered by how specific they are. |
| 68 ParsedDataPresenter parsed_data_presenter(*request); |
| 69 RawDataPresenter raw_data_presenter; |
| 70 UploadDataPresenter* const presenters[] = { |
| 71 &parsed_data_presenter, // 1: any parseable forms? (Specific to forms.) |
| 72 &raw_data_presenter // 2: any data at all? (Non-specific.) |
| 73 }; |
| 74 // Keys for the results of the corresponding presenters. |
| 75 static const char* const kKeys[] = {keys::kRequestBodyFormDataKey, |
| 76 keys::kRequestBodyRawKey}; |
| 77 |
| 78 const std::vector<scoped_ptr<net::UploadElementReader>>* readers = |
| 79 upload_data->GetElementReaders(); |
| 80 bool some_succeeded = false; |
| 81 if (readers) { |
| 82 for (size_t i = 0; i < arraysize(presenters); ++i) { |
| 83 for (const auto& reader : *readers) |
| 84 presenters[i]->FeedNext(*reader); |
| 85 if (presenters[i]->Succeeded()) { |
| 86 request_body->Set(kKeys[i], presenters[i]->Result()); |
| 87 some_succeeded = true; |
| 88 break; |
| 89 } |
| 90 } |
| 91 } |
| 92 if (!some_succeeded) |
| 93 request_body->SetString(keys::kRequestBodyErrorKey, "Unknown error."); |
| 94 } |
| 95 |
| 96 void WebRequestEventDetails::SetRequestHeaders( |
| 97 const net::HttpRequestHeaders& request_headers) { |
| 98 if (!(extra_info_spec_ & ExtraInfoSpec::REQUEST_HEADERS)) |
| 99 return; |
| 100 |
| 101 base::ListValue* headers = new base::ListValue(); |
| 102 for (net::HttpRequestHeaders::Iterator it(request_headers); it.GetNext();) |
| 103 headers->Append(helpers::CreateHeaderDictionary(it.name(), it.value())); |
| 104 request_headers_.reset(headers); |
| 105 } |
| 106 |
| 107 void WebRequestEventDetails::SetAuthInfo( |
| 108 const net::AuthChallengeInfo& auth_info) { |
| 109 dict_.SetBoolean(keys::kIsProxyKey, auth_info.is_proxy); |
| 110 if (!auth_info.scheme.empty()) |
| 111 dict_.SetString(keys::kSchemeKey, auth_info.scheme); |
| 112 if (!auth_info.realm.empty()) |
| 113 dict_.SetString(keys::kRealmKey, auth_info.realm); |
| 114 base::DictionaryValue* challenger = new base::DictionaryValue(); |
| 115 challenger->SetString(keys::kHostKey, auth_info.challenger.host()); |
| 116 challenger->SetInteger(keys::kPortKey, auth_info.challenger.port()); |
| 117 dict_.Set(keys::kChallengerKey, challenger); |
| 118 } |
| 119 |
| 120 void WebRequestEventDetails::SetResponseHeaders( |
| 121 const net::URLRequest* request, |
| 122 const net::HttpResponseHeaders* response_headers) { |
| 123 if (!response_headers) { |
| 124 // Not all URLRequestJobs specify response headers. E.g. URLRequestFTPJob, |
| 125 // URLRequestFileJob and some redirects. |
| 126 dict_.SetInteger(keys::kStatusCodeKey, request->GetResponseCode()); |
| 127 dict_.SetString(keys::kStatusLineKey, ""); |
| 128 } else { |
| 129 dict_.SetInteger(keys::kStatusCodeKey, response_headers->response_code()); |
| 130 dict_.SetString(keys::kStatusLineKey, response_headers->GetStatusLine()); |
| 131 } |
| 132 |
| 133 if (extra_info_spec_ & ExtraInfoSpec::RESPONSE_HEADERS) { |
| 134 base::ListValue* headers = new base::ListValue(); |
| 135 if (response_headers) { |
| 136 void* iter = nullptr; |
| 137 std::string name; |
| 138 std::string value; |
| 139 while (response_headers->EnumerateHeaderLines(&iter, &name, &value)) |
| 140 headers->Append(helpers::CreateHeaderDictionary(name, value)); |
| 141 } |
| 142 response_headers_.reset(headers); |
| 143 } |
| 144 } |
| 145 |
| 146 void WebRequestEventDetails::SetResponseSource(const net::URLRequest* request) { |
| 147 dict_.SetBoolean(keys::kFromCache, request->was_cached()); |
| 148 const std::string response_ip = request->GetSocketAddress().host(); |
| 149 if (!response_ip.empty()) |
| 150 dict_.SetString(keys::kIpKey, response_ip); |
| 151 } |
| 152 |
| 153 void WebRequestEventDetails::DetermineFrameIdOnUI() { |
| 154 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 155 content::RenderFrameHost* rfh = |
| 156 content::RenderFrameHost::FromID(render_process_id_, render_frame_id_); |
| 157 dict_.SetInteger(keys::kFrameIdKey, ExtensionApiFrameIdMap::GetFrameId(rfh)); |
| 158 dict_.SetInteger(keys::kParentFrameIdKey, |
| 159 ExtensionApiFrameIdMap::GetParentFrameId(rfh)); |
| 160 } |
| 161 |
| 162 void WebRequestEventDetails::DetermineFrameIdOnIO( |
| 163 const DeterminedFrameIdCallback& callback) { |
| 164 scoped_ptr<WebRequestEventDetails> self(this); |
| 165 ExtensionApiFrameIdMap::Get()->GetFrameIdOnIO( |
| 166 render_process_id_, render_frame_id_, |
| 167 base::Bind(&WebRequestEventDetails::OnDeterminedFrameId, |
| 168 base::Unretained(this), base::Passed(&self), callback)); |
| 169 } |
| 170 |
| 171 scoped_ptr<base::DictionaryValue> WebRequestEventDetails::GetFilteredDict( |
| 172 int extra_info_spec) const { |
| 173 scoped_ptr<base::DictionaryValue> result = dict_.CreateDeepCopy(); |
| 174 if ((extra_info_spec & ExtraInfoSpec::REQUEST_BODY) && request_body_) |
| 175 result->Set(keys::kRequestBodyKey, request_body_->CreateDeepCopy()); |
| 176 if ((extra_info_spec & ExtraInfoSpec::REQUEST_HEADERS) && request_headers_) |
| 177 result->Set(keys::kRequestHeadersKey, request_headers_->CreateDeepCopy()); |
| 178 if ((extra_info_spec & ExtraInfoSpec::RESPONSE_HEADERS) && response_headers_) |
| 179 result->Set(keys::kResponseHeadersKey, response_headers_->CreateDeepCopy()); |
| 180 return result; |
| 181 } |
| 182 |
| 183 scoped_ptr<base::DictionaryValue> WebRequestEventDetails::GetAndClearDict() { |
| 184 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); |
| 185 dict_.Swap(result.get()); |
| 186 return result; |
| 187 } |
| 188 |
| 189 void WebRequestEventDetails::OnDeterminedFrameId( |
| 190 scoped_ptr<WebRequestEventDetails> self, |
| 191 const DeterminedFrameIdCallback& callback, |
| 192 int extension_api_frame_id, |
| 193 int extension_api_parent_frame_id) { |
| 194 dict_.SetInteger(keys::kFrameIdKey, extension_api_frame_id); |
| 195 dict_.SetInteger(keys::kParentFrameIdKey, extension_api_parent_frame_id); |
| 196 callback.Run(std::move(self)); |
| 197 } |
| 198 |
| 199 } // namespace extensions |
OLD | NEW |