Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/api/web_request/web_request_api.h" | 5 #include "extensions/browser/api/web_request/web_request_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 #include "extensions/browser/api/declarative_webrequest/request_stage.h" | 30 #include "extensions/browser/api/declarative_webrequest/request_stage.h" |
| 31 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" | 31 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" |
| 32 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registr y.h" | 32 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registr y.h" |
| 33 #include "extensions/browser/api/extensions_api_client.h" | 33 #include "extensions/browser/api/extensions_api_client.h" |
| 34 #include "extensions/browser/api/web_request/upload_data_presenter.h" | 34 #include "extensions/browser/api/web_request/upload_data_presenter.h" |
| 35 #include "extensions/browser/api/web_request/web_request_api_constants.h" | 35 #include "extensions/browser/api/web_request/web_request_api_constants.h" |
| 36 #include "extensions/browser/api/web_request/web_request_api_helpers.h" | 36 #include "extensions/browser/api/web_request/web_request_api_helpers.h" |
| 37 #include "extensions/browser/api/web_request/web_request_event_router_delegate.h " | 37 #include "extensions/browser/api/web_request/web_request_event_router_delegate.h " |
| 38 #include "extensions/browser/api/web_request/web_request_time_tracker.h" | 38 #include "extensions/browser/api/web_request/web_request_time_tracker.h" |
| 39 #include "extensions/browser/event_router.h" | 39 #include "extensions/browser/event_router.h" |
| 40 #include "extensions/browser/extension_api_frame_id_map.h" | |
| 40 #include "extensions/browser/extension_prefs.h" | 41 #include "extensions/browser/extension_prefs.h" |
| 41 #include "extensions/browser/extension_registry.h" | 42 #include "extensions/browser/extension_registry.h" |
| 42 #include "extensions/browser/extension_system.h" | 43 #include "extensions/browser/extension_system.h" |
| 43 #include "extensions/browser/extensions_browser_client.h" | 44 #include "extensions/browser/extensions_browser_client.h" |
| 44 #include "extensions/browser/guest_view/guest_view_events.h" | 45 #include "extensions/browser/guest_view/guest_view_events.h" |
| 45 #include "extensions/browser/guest_view/web_view/web_view_constants.h" | 46 #include "extensions/browser/guest_view/web_view/web_view_constants.h" |
| 46 #include "extensions/browser/guest_view/web_view/web_view_renderer_state.h" | 47 #include "extensions/browser/guest_view/web_view/web_view_renderer_state.h" |
| 47 #include "extensions/browser/info_map.h" | 48 #include "extensions/browser/info_map.h" |
| 48 #include "extensions/browser/io_thread_extension_message_filter.h" | 49 #include "extensions/browser/io_thread_extension_message_filter.h" |
| 49 #include "extensions/browser/runtime_data.h" | 50 #include "extensions/browser/runtime_data.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 return keys::kOnResponseStarted; | 122 return keys::kOnResponseStarted; |
| 122 case ExtensionWebRequestEventRouter::kOnErrorOccurred: | 123 case ExtensionWebRequestEventRouter::kOnErrorOccurred: |
| 123 return keys::kOnErrorOccurred; | 124 return keys::kOnErrorOccurred; |
| 124 case ExtensionWebRequestEventRouter::kOnCompleted: | 125 case ExtensionWebRequestEventRouter::kOnCompleted: |
| 125 return keys::kOnCompleted; | 126 return keys::kOnCompleted; |
| 126 } | 127 } |
| 127 NOTREACHED(); | 128 NOTREACHED(); |
| 128 return "Not reached"; | 129 return "Not reached"; |
| 129 } | 130 } |
| 130 | 131 |
| 131 int GetFrameId(bool is_main_frame, int frame_id) { | |
| 132 return is_main_frame ? 0 : frame_id; | |
| 133 } | |
| 134 | |
| 135 bool IsWebRequestEvent(const std::string& event_name) { | 132 bool IsWebRequestEvent(const std::string& event_name) { |
| 136 std::string web_request_event_name(event_name); | 133 std::string web_request_event_name(event_name); |
| 137 if (base::StartsWith(web_request_event_name, | 134 if (base::StartsWith(web_request_event_name, |
| 138 webview::kWebViewEventPrefix, | 135 webview::kWebViewEventPrefix, |
| 139 base::CompareCase::SENSITIVE)) { | 136 base::CompareCase::SENSITIVE)) { |
| 140 web_request_event_name.replace( | 137 web_request_event_name.replace( |
| 141 0, strlen(webview::kWebViewEventPrefix), kWebRequestEventPrefix); | 138 0, strlen(webview::kWebViewEventPrefix), kWebRequestEventPrefix); |
| 142 } | 139 } |
| 143 const auto web_request_events_end = | 140 const auto web_request_events_end = |
| 144 kWebRequestEvents + arraysize(kWebRequestEvents); | 141 kWebRequestEvents + arraysize(kWebRequestEvents); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 WebViewRendererState::WebViewInfo* web_view_info) { | 192 WebViewRendererState::WebViewInfo* web_view_info) { |
| 196 int render_process_host_id = -1; | 193 int render_process_host_id = -1; |
| 197 int routing_id = -1; | 194 int routing_id = -1; |
| 198 ExtractRequestRoutingInfo(request, &render_process_host_id, &routing_id); | 195 ExtractRequestRoutingInfo(request, &render_process_host_id, &routing_id); |
| 199 return WebViewRendererState::GetInstance()->GetInfo( | 196 return WebViewRendererState::GetInstance()->GetInfo( |
| 200 render_process_host_id, routing_id, web_view_info); | 197 render_process_host_id, routing_id, web_view_info); |
| 201 } | 198 } |
| 202 | 199 |
| 203 void ExtractRequestInfoDetails(const net::URLRequest* request, | 200 void ExtractRequestInfoDetails(const net::URLRequest* request, |
| 204 bool* is_main_frame, | 201 bool* is_main_frame, |
| 205 int* frame_id, | 202 int* render_frame_id, |
| 206 bool* parent_is_main_frame, | |
| 207 int* parent_frame_id, | |
| 208 int* render_process_host_id, | 203 int* render_process_host_id, |
| 209 int* routing_id, | 204 int* routing_id, |
| 210 ResourceType* resource_type) { | 205 ResourceType* resource_type) { |
| 211 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | 206 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
| 212 if (!info) | 207 if (!info) |
| 213 return; | 208 return; |
| 214 | 209 |
| 215 *frame_id = info->GetRenderFrameID(); | 210 *render_frame_id = info->GetRenderFrameID(); |
| 216 *is_main_frame = info->IsMainFrame(); | 211 *is_main_frame = info->IsMainFrame(); |
| 217 *parent_frame_id = info->GetParentRenderFrameID(); | |
| 218 *parent_is_main_frame = info->ParentIsMainFrame(); | |
| 219 *render_process_host_id = info->GetChildID(); | 212 *render_process_host_id = info->GetChildID(); |
| 220 *routing_id = info->GetRouteID(); | 213 *routing_id = info->GetRouteID(); |
| 221 | 214 |
| 222 // Restrict the resource type to the values we care about. | 215 // Restrict the resource type to the values we care about. |
| 223 if (helpers::IsRelevantResourceType(info->GetResourceType())) | 216 if (helpers::IsRelevantResourceType(info->GetResourceType())) |
| 224 *resource_type = info->GetResourceType(); | 217 *resource_type = info->GetResourceType(); |
| 225 else | 218 else |
| 226 *resource_type = content::RESOURCE_TYPE_LAST_TYPE; | 219 *resource_type = content::RESOURCE_TYPE_LAST_TYPE; |
| 227 } | 220 } |
| 228 | 221 |
| 222 void ExtractRenderFrameInfo(base::DictionaryValue* dict, | |
| 223 int* render_process_id, | |
| 224 int* render_frame_id) { | |
| 225 if (dict->GetInteger(keys::kFrameIdKey, render_frame_id) && | |
| 226 dict->GetInteger(keys::kProcessIdKey, render_process_id)) { | |
| 227 } else { | |
| 228 *render_process_id = -1; | |
| 229 *render_frame_id = -1; | |
| 230 } | |
| 231 // ExtractFrameIdInfo will overwrite kFrameIdKey, so it is not removed here. | |
|
battre
2015/12/10 15:31:39
nit: ExtractApiFrameFrameIdInfo?
robwu
2015/12/10 16:39:29
Done (using 1x Frame though: ExtractApiFrameIdInfo
| |
| 232 dict->Remove(keys::kProcessIdKey, nullptr); | |
|
battre
2015/12/10 15:31:39
It feels weird to me that the extract function has
robwu
2015/12/10 16:39:29
Indeed. That's why I document that processId is mo
| |
| 233 } | |
| 234 | |
| 235 void ExtractApiFrameIdInfo(base::DictionaryValue* dict, | |
| 236 const ExtensionApiFrameId& extension_api_frame_id) { | |
|
battre
2015/12/10 15:31:39
nit: do you want to put the output parameter last?
robwu
2015/12/10 16:39:29
Done.
| |
| 237 dict->SetInteger(keys::kFrameIdKey, extension_api_frame_id.frame_id); | |
| 238 dict->SetInteger(keys::kParentFrameIdKey, | |
| 239 extension_api_frame_id.parent_frame_id); | |
| 240 } | |
| 241 | |
| 229 // Extracts the body from |request| and writes the data into |out|. | 242 // Extracts the body from |request| and writes the data into |out|. |
| 230 void ExtractRequestInfoBody(const net::URLRequest* request, | 243 void ExtractRequestInfoBody(const net::URLRequest* request, |
| 231 base::DictionaryValue* out) { | 244 base::DictionaryValue* out) { |
| 232 const net::UploadDataStream* upload_data = request->get_upload(); | 245 const net::UploadDataStream* upload_data = request->get_upload(); |
| 233 if (!upload_data || | 246 if (!upload_data || |
| 234 (request->method() != "POST" && request->method() != "PUT")) { | 247 (request->method() != "POST" && request->method() != "PUT")) { |
| 235 return; // Need to exit without "out->Set(keys::kRequestBodyKey, ...);" . | 248 return; // Need to exit without "out->Set(keys::kRequestBodyKey, ...);" . |
| 236 } | 249 } |
| 237 | 250 |
| 238 base::DictionaryValue* request_body = new base::DictionaryValue(); | 251 base::DictionaryValue* request_body = new base::DictionaryValue(); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 const WebViewRendererState::WebViewInfo& web_view_info, | 364 const WebViewRendererState::WebViewInfo& web_view_info, |
| 352 scoped_ptr<base::DictionaryValue> event_argument) { | 365 scoped_ptr<base::DictionaryValue> event_argument) { |
| 353 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 366 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 354 | 367 |
| 355 content::BrowserContext* browser_context = | 368 content::BrowserContext* browser_context = |
| 356 reinterpret_cast<content::BrowserContext*>(browser_context_id); | 369 reinterpret_cast<content::BrowserContext*>(browser_context_id); |
| 357 if (!ExtensionsBrowserClient::Get()->IsValidContext(browser_context)) | 370 if (!ExtensionsBrowserClient::Get()->IsValidContext(browser_context)) |
| 358 return; | 371 return; |
| 359 | 372 |
| 360 scoped_ptr<base::ListValue> event_args(new base::ListValue); | 373 scoped_ptr<base::ListValue> event_args(new base::ListValue); |
| 374 int render_process_host_id = -1; | |
| 375 int render_frame_id = -1; | |
| 376 ExtractRenderFrameInfo(event_argument.get(), &render_process_host_id, | |
| 377 &render_frame_id); | |
| 378 const ExtensionApiFrameId& extension_api_frame_id = | |
| 379 ExtensionApiFrameIdMap::GetFrameId(render_process_host_id, | |
| 380 render_frame_id); | |
| 381 ExtractApiFrameIdInfo(event_argument.get(), extension_api_frame_id); | |
| 361 event_args->Append(event_argument.release()); | 382 event_args->Append(event_argument.release()); |
| 362 | 383 |
| 363 EventRouter* event_router = EventRouter::Get(browser_context); | 384 EventRouter* event_router = EventRouter::Get(browser_context); |
| 364 | 385 |
| 365 EventFilteringInfo event_filtering_info; | 386 EventFilteringInfo event_filtering_info; |
| 366 | 387 |
| 367 events::HistogramValue histogram_value = events::UNKNOWN; | 388 events::HistogramValue histogram_value = events::UNKNOWN; |
| 368 std::string event_name; | 389 std::string event_name; |
| 369 // The instance ID uniquely identifies a <webview> instance within an embedder | 390 // The instance ID uniquely identifies a <webview> instance within an embedder |
| 370 // process. We use a filter here so that only event listeners for a particular | 391 // process. We use a filter here so that only event listeners for a particular |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 517 return sub_event_name < that.sub_event_name; | 538 return sub_event_name < that.sub_event_name; |
| 518 | 539 |
| 519 if (web_view_instance_id != that.web_view_instance_id) | 540 if (web_view_instance_id != that.web_view_instance_id) |
| 520 return web_view_instance_id < that.web_view_instance_id; | 541 return web_view_instance_id < that.web_view_instance_id; |
| 521 | 542 |
| 522 if (web_view_instance_id == 0) { | 543 if (web_view_instance_id == 0) { |
| 523 // Do not filter by process ID for non-webviews, because this comparator | 544 // Do not filter by process ID for non-webviews, because this comparator |
| 524 // is also used to find and remove an event listener when an extension is | 545 // is also used to find and remove an event listener when an extension is |
| 525 // unloaded. At this point, the event listener cannot be mapped back to | 546 // unloaded. At this point, the event listener cannot be mapped back to |
| 526 // the original process, so 0 is used instead of the actual process ID. | 547 // the original process, so 0 is used instead of the actual process ID. |
| 527 DCHECK(embedder_process_id == 0 || that.embedder_process_id == 0); | 548 if (embedder_process_id == 0 || that.embedder_process_id == 0) { |
| 528 return false; | 549 return false; |
| 550 } | |
|
battre
2015/12/10 15:31:39
nit: no {}?
robwu
2015/12/10 16:39:29
Done.
| |
| 529 } | 551 } |
| 530 | 552 |
| 531 if (embedder_process_id != that.embedder_process_id) | 553 if (embedder_process_id != that.embedder_process_id) |
| 532 return embedder_process_id < that.embedder_process_id; | 554 return embedder_process_id < that.embedder_process_id; |
| 533 | 555 |
| 534 return false; | 556 return false; |
| 535 } | 557 } |
| 536 | 558 |
| 537 EventListener() | 559 EventListener() |
| 538 : histogram_value(events::UNKNOWN), | 560 : histogram_value(events::UNKNOWN), |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 734 if (rules_registry.get()) | 756 if (rules_registry.get()) |
| 735 rules_registries_[key] = rules_registry; | 757 rules_registries_[key] = rules_registry; |
| 736 else | 758 else |
| 737 rules_registries_.erase(key); | 759 rules_registries_.erase(key); |
| 738 } | 760 } |
| 739 | 761 |
| 740 void ExtensionWebRequestEventRouter::ExtractRequestInfo( | 762 void ExtensionWebRequestEventRouter::ExtractRequestInfo( |
| 741 const net::URLRequest* request, | 763 const net::URLRequest* request, |
| 742 base::DictionaryValue* out) { | 764 base::DictionaryValue* out) { |
| 743 bool is_main_frame = false; | 765 bool is_main_frame = false; |
| 744 int frame_id = -1; | 766 int render_frame_id = -1; |
| 745 bool parent_is_main_frame = false; | |
| 746 int parent_frame_id = -1; | |
| 747 int frame_id_for_extension = -1; | |
| 748 int parent_frame_id_for_extension = -1; | |
| 749 int render_process_host_id = -1; | 767 int render_process_host_id = -1; |
| 750 int routing_id = -1; | 768 int routing_id = -1; |
| 751 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE; | 769 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE; |
| 752 ExtractRequestInfoDetails(request, &is_main_frame, &frame_id, | 770 ExtractRequestInfoDetails(request, &is_main_frame, &render_frame_id, |
| 753 &parent_is_main_frame, &parent_frame_id, | |
| 754 &render_process_host_id, &routing_id, | 771 &render_process_host_id, &routing_id, |
| 755 &resource_type); | 772 &resource_type); |
| 756 frame_id_for_extension = GetFrameId(is_main_frame, frame_id); | |
| 757 parent_frame_id_for_extension = GetFrameId(parent_is_main_frame, | |
| 758 parent_frame_id); | |
| 759 | 773 |
| 760 out->SetString(keys::kRequestIdKey, | 774 out->SetString(keys::kRequestIdKey, |
| 761 base::Uint64ToString(request->identifier())); | 775 base::Uint64ToString(request->identifier())); |
| 762 out->SetString(keys::kUrlKey, request->url().spec()); | 776 out->SetString(keys::kUrlKey, request->url().spec()); |
| 763 out->SetString(keys::kMethodKey, request->method()); | 777 out->SetString(keys::kMethodKey, request->method()); |
| 764 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); | 778 // Note: This (frameId, processId) pair is removed by ExtractRenderFrameInfo, |
| 765 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); | 779 // and then finally set by ExtractApiFrameIdInfo. |
| 780 // TODO(robwu): This is ugly. Create a proper data structure to separate these | |
| 781 // two IDs from the dictionary, so that kFrameIdKey has only one meaning. | |
| 782 out->SetInteger(keys::kFrameIdKey, render_frame_id); | |
| 783 out->SetInteger(keys::kProcessIdKey, render_process_host_id); | |
| 766 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); | 784 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); |
| 767 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); | 785 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); |
| 768 if (web_request_event_router_delegate_) { | 786 if (web_request_event_router_delegate_) { |
| 769 web_request_event_router_delegate_->ExtractExtraRequestDetails( | 787 web_request_event_router_delegate_->ExtractExtraRequestDetails( |
| 770 request, out); | 788 request, out); |
| 771 } | 789 } |
| 772 } | 790 } |
| 773 | 791 |
| 774 int ExtensionWebRequestEventRouter::OnBeforeRequest( | 792 int ExtensionWebRequestEventRouter::OnBeforeRequest( |
| 775 void* browser_context, | 793 void* browser_context, |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1234 } | 1252 } |
| 1235 | 1253 |
| 1236 bool ExtensionWebRequestEventRouter::DispatchEvent( | 1254 bool ExtensionWebRequestEventRouter::DispatchEvent( |
| 1237 void* browser_context, | 1255 void* browser_context, |
| 1238 net::URLRequest* request, | 1256 net::URLRequest* request, |
| 1239 const std::vector<const EventListener*>& listeners, | 1257 const std::vector<const EventListener*>& listeners, |
| 1240 const base::ListValue& args) { | 1258 const base::ListValue& args) { |
| 1241 // TODO(mpcomplete): Consider consolidating common (extension_id,json_args) | 1259 // TODO(mpcomplete): Consider consolidating common (extension_id,json_args) |
| 1242 // pairs into a single message sent to a list of sub_event_names. | 1260 // pairs into a single message sent to a list of sub_event_names. |
| 1243 int num_handlers_blocking = 0; | 1261 int num_handlers_blocking = 0; |
| 1262 | |
| 1263 scoped_ptr<std::vector<EventListener>> listeners_to_dispatch( | |
| 1264 new std::vector<EventListener>()); | |
| 1265 listeners_to_dispatch->reserve(listeners.size()); | |
| 1244 for (const EventListener* listener : listeners) { | 1266 for (const EventListener* listener : listeners) { |
| 1245 // Filter out the optional keys that this listener didn't request. | 1267 listeners_to_dispatch->push_back(*listener); |
| 1246 scoped_ptr<base::ListValue> args_filtered(args.DeepCopy()); | |
| 1247 base::DictionaryValue* dict = NULL; | |
| 1248 CHECK(args_filtered->GetDictionary(0, &dict) && dict); | |
| 1249 if (!(listener->extra_info_spec & ExtraInfoSpec::REQUEST_HEADERS)) | |
| 1250 dict->Remove(keys::kRequestHeadersKey, NULL); | |
| 1251 if (!(listener->extra_info_spec & ExtraInfoSpec::RESPONSE_HEADERS)) | |
| 1252 dict->Remove(keys::kResponseHeadersKey, NULL); | |
| 1253 | |
| 1254 EventRouter::DispatchEventToSender( | |
| 1255 listener->ipc_sender.get(), browser_context, listener->extension_id, | |
| 1256 listener->histogram_value, listener->sub_event_name, | |
| 1257 args_filtered.Pass(), EventRouter::USER_GESTURE_UNKNOWN, | |
| 1258 EventFilteringInfo()); | |
| 1259 if (listener->extra_info_spec & | 1268 if (listener->extra_info_spec & |
| 1260 (ExtraInfoSpec::BLOCKING | ExtraInfoSpec::ASYNC_BLOCKING)) { | 1269 (ExtraInfoSpec::BLOCKING | ExtraInfoSpec::ASYNC_BLOCKING)) { |
| 1261 listener->blocked_requests.insert(request->identifier()); | 1270 listener->blocked_requests.insert(request->identifier()); |
| 1262 // If this is the first delegate blocking the request, go ahead and log | 1271 // If this is the first delegate blocking the request, go ahead and log |
| 1263 // it. | 1272 // it. |
| 1264 if (num_handlers_blocking == 0) { | 1273 if (num_handlers_blocking == 0) { |
| 1265 std::string delegate_info = l10n_util::GetStringFUTF8( | 1274 std::string delegate_info = l10n_util::GetStringFUTF8( |
| 1266 IDS_LOAD_STATE_PARAMETER_EXTENSION, | 1275 IDS_LOAD_STATE_PARAMETER_EXTENSION, |
| 1267 base::UTF8ToUTF16(listener->extension_name)); | 1276 base::UTF8ToUTF16(listener->extension_name)); |
| 1268 // LobAndReport allows extensions that block requests to be displayed in | 1277 // LobAndReport allows extensions that block requests to be displayed in |
| 1269 // the load status bar. | 1278 // the load status bar. |
| 1270 request->LogAndReportBlockedBy(delegate_info.c_str()); | 1279 request->LogAndReportBlockedBy(delegate_info.c_str()); |
| 1271 } | 1280 } |
| 1272 ++num_handlers_blocking; | 1281 ++num_handlers_blocking; |
| 1273 } | 1282 } |
| 1274 } | 1283 } |
| 1275 | 1284 |
| 1285 // TODO(robwu): Avoid unnecessary copy, by changing |args| to be a | |
| 1286 // scoped_ptr<base::DictionaryValue> and transferring the ownership. | |
| 1287 const base::DictionaryValue* dict = nullptr; | |
| 1288 CHECK(args.GetDictionary(0, &dict) && dict); | |
| 1289 base::DictionaryValue* args_copy = dict->DeepCopy(); | |
| 1290 | |
| 1291 int render_process_host_id = -1; | |
| 1292 int render_frame_id = -1; | |
| 1293 ExtractRenderFrameInfo(args_copy, &render_process_host_id, &render_frame_id); | |
| 1294 | |
| 1295 ExtensionApiFrameIdMap::GetFrameIdOnIO( | |
| 1296 render_process_host_id, render_frame_id, | |
| 1297 base::Bind(&ExtensionWebRequestEventRouter::DispatchEventOnIO, | |
| 1298 AsWeakPtr(), browser_context, | |
| 1299 base::Passed(&listeners_to_dispatch), base::Owned(args_copy))); | |
|
battre
2015/12/10 15:31:39
Just checking: Can this asynchronicity rearrange e
robwu
2015/12/10 16:39:29
Good point. In theory, this is certainly possible
| |
| 1300 | |
| 1276 if (num_handlers_blocking > 0) { | 1301 if (num_handlers_blocking > 0) { |
| 1277 BlockedRequest& blocked_request = blocked_requests_[request->identifier()]; | 1302 BlockedRequest& blocked_request = blocked_requests_[request->identifier()]; |
| 1278 blocked_request.request = request; | 1303 blocked_request.request = request; |
| 1279 blocked_request.is_incognito |= IsIncognitoBrowserContext(browser_context); | 1304 blocked_request.is_incognito |= IsIncognitoBrowserContext(browser_context); |
| 1280 blocked_request.num_handlers_blocking += num_handlers_blocking; | 1305 blocked_request.num_handlers_blocking += num_handlers_blocking; |
| 1281 blocked_request.blocking_time = base::Time::Now(); | 1306 blocked_request.blocking_time = base::Time::Now(); |
| 1282 return true; | 1307 return true; |
| 1283 } | 1308 } |
| 1284 | 1309 |
| 1285 return false; | 1310 return false; |
| 1286 } | 1311 } |
| 1287 | 1312 |
| 1313 void ExtensionWebRequestEventRouter::DispatchEventOnIO( | |
|
battre
2015/12/10 15:31:39
I think this naming is confusing because the other
robwu
2015/12/10 16:39:29
I've renamed it to DispatchEventToListeners.
| |
| 1314 void* browser_context, | |
| 1315 scoped_ptr<std::vector<EventListener>> listeners, | |
| 1316 base::DictionaryValue* dict, | |
| 1317 const ExtensionApiFrameId& extension_api_frame_id) { | |
| 1318 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 1319 DCHECK(listeners.get()); | |
| 1320 DCHECK_GT(listeners->size(), 0UL); | |
| 1321 DCHECK(dict); | |
| 1322 | |
| 1323 ExtractApiFrameIdInfo(dict, extension_api_frame_id); | |
| 1324 | |
| 1325 std::string event_name = | |
| 1326 EventRouter::GetBaseEventName((*listeners)[0].sub_event_name); | |
| 1327 DCHECK(IsWebRequestEvent(event_name)); | |
| 1328 | |
| 1329 const std::set<EventListener>& event_listeners = | |
| 1330 listeners_[browser_context][event_name]; | |
| 1331 void* cross_browser_context = GetCrossBrowserContext(browser_context); | |
| 1332 const std::set<EventListener>* cross_event_listeners = | |
| 1333 cross_browser_context ? &listeners_[cross_browser_context][event_name] | |
| 1334 : nullptr; | |
| 1335 | |
| 1336 for (const EventListener& target : *listeners) { | |
| 1337 std::set<EventListener>::const_iterator listener = | |
| 1338 event_listeners.find(target); | |
| 1339 // Ignore listener if it was removed between the thread hops. | |
| 1340 if (listener == event_listeners.end()) { | |
| 1341 if (!cross_event_listeners) | |
| 1342 continue; | |
| 1343 listener = cross_event_listeners->find(target); | |
| 1344 if (listener == cross_event_listeners->end()) | |
| 1345 continue; | |
| 1346 } | |
| 1347 | |
| 1348 // Filter out the optional keys that this listener didn't request. | |
| 1349 scoped_ptr<base::ListValue> args_filtered(new base::ListValue); | |
| 1350 args_filtered->Append(dict->DeepCopy()); | |
| 1351 if (!(listener->extra_info_spec & ExtraInfoSpec::REQUEST_HEADERS)) | |
| 1352 dict->Remove(keys::kRequestHeadersKey, nullptr); | |
| 1353 if (!(listener->extra_info_spec & ExtraInfoSpec::RESPONSE_HEADERS)) | |
| 1354 dict->Remove(keys::kResponseHeadersKey, nullptr); | |
| 1355 | |
| 1356 EventRouter::DispatchEventToSender( | |
| 1357 listener->ipc_sender.get(), browser_context, listener->extension_id, | |
| 1358 listener->histogram_value, listener->sub_event_name, | |
| 1359 args_filtered.Pass(), EventRouter::USER_GESTURE_UNKNOWN, | |
| 1360 EventFilteringInfo()); | |
| 1361 } | |
| 1362 } | |
| 1363 | |
| 1288 void ExtensionWebRequestEventRouter::OnEventHandled( | 1364 void ExtensionWebRequestEventRouter::OnEventHandled( |
| 1289 void* browser_context, | 1365 void* browser_context, |
| 1290 const std::string& extension_id, | 1366 const std::string& extension_id, |
| 1291 const std::string& event_name, | 1367 const std::string& event_name, |
| 1292 const std::string& sub_event_name, | 1368 const std::string& sub_event_name, |
| 1293 uint64_t request_id, | 1369 uint64_t request_id, |
| 1294 EventResponse* response) { | 1370 EventResponse* response) { |
| 1295 // TODO(robwu): Does this also work with webviews? operator< (used by find) | 1371 // TODO(robwu): Does this also work with webviews? operator< (used by find) |
| 1296 // takes the webview ID into account, which is not set on |listener|. | 1372 // takes the webview ID into account, which is not set on |listener|. |
| 1297 EventListener listener; | 1373 EventListener listener; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1431 } | 1507 } |
| 1432 | 1508 |
| 1433 void ExtensionWebRequestEventRouter::AddCallbackForPageLoad( | 1509 void ExtensionWebRequestEventRouter::AddCallbackForPageLoad( |
| 1434 const base::Closure& callback) { | 1510 const base::Closure& callback) { |
| 1435 callbacks_for_page_load_.push_back(callback); | 1511 callbacks_for_page_load_.push_back(callback); |
| 1436 } | 1512 } |
| 1437 | 1513 |
| 1438 bool ExtensionWebRequestEventRouter::IsPageLoad( | 1514 bool ExtensionWebRequestEventRouter::IsPageLoad( |
| 1439 const net::URLRequest* request) const { | 1515 const net::URLRequest* request) const { |
| 1440 bool is_main_frame = false; | 1516 bool is_main_frame = false; |
| 1441 int frame_id = -1; | 1517 int render_frame_id = -1; |
| 1442 bool parent_is_main_frame = false; | |
| 1443 int parent_frame_id = -1; | |
| 1444 int render_process_host_id = -1; | 1518 int render_process_host_id = -1; |
| 1445 int routing_id = -1; | 1519 int routing_id = -1; |
| 1446 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE; | 1520 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE; |
| 1447 | 1521 |
| 1448 ExtractRequestInfoDetails(request, &is_main_frame, &frame_id, | 1522 ExtractRequestInfoDetails(request, &is_main_frame, &render_frame_id, |
| 1449 &parent_is_main_frame, &parent_frame_id, | 1523 &render_process_host_id, &routing_id, |
| 1450 &render_process_host_id, | 1524 &resource_type); |
| 1451 &routing_id, &resource_type); | |
| 1452 | 1525 |
| 1453 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME; | 1526 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME; |
| 1454 } | 1527 } |
| 1455 | 1528 |
| 1456 void ExtensionWebRequestEventRouter::NotifyPageLoad() { | 1529 void ExtensionWebRequestEventRouter::NotifyPageLoad() { |
| 1457 for (const auto& callback : callbacks_for_page_load_) | 1530 for (const auto& callback : callbacks_for_page_load_) |
| 1458 callback.Run(); | 1531 callback.Run(); |
| 1459 callbacks_for_page_load_.clear(); | 1532 callbacks_for_page_load_.clear(); |
| 1460 } | 1533 } |
| 1461 | 1534 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1581 void* browser_context, | 1654 void* browser_context, |
| 1582 const InfoMap* extension_info_map, | 1655 const InfoMap* extension_info_map, |
| 1583 const std::string& event_name, | 1656 const std::string& event_name, |
| 1584 const net::URLRequest* request, | 1657 const net::URLRequest* request, |
| 1585 int* extra_info_spec) { | 1658 int* extra_info_spec) { |
| 1586 // TODO(mpcomplete): handle browser_context == NULL (should collect all | 1659 // TODO(mpcomplete): handle browser_context == NULL (should collect all |
| 1587 // listeners). | 1660 // listeners). |
| 1588 *extra_info_spec = 0; | 1661 *extra_info_spec = 0; |
| 1589 | 1662 |
| 1590 bool is_main_frame = false; | 1663 bool is_main_frame = false; |
| 1591 int frame_id = -1; | 1664 int render_frame_id = -1; |
| 1592 bool parent_is_main_frame = false; | |
| 1593 int parent_frame_id = -1; | |
| 1594 int render_process_host_id = -1; | 1665 int render_process_host_id = -1; |
| 1595 int routing_id = -1; | 1666 int routing_id = -1; |
| 1596 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE; | 1667 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE; |
| 1597 const GURL& url = request->url(); | 1668 const GURL& url = request->url(); |
| 1598 | 1669 |
| 1599 ExtractRequestInfoDetails(request, &is_main_frame, &frame_id, | 1670 ExtractRequestInfoDetails(request, &is_main_frame, &render_frame_id, |
| 1600 &parent_is_main_frame, &parent_frame_id, | 1671 &render_process_host_id, &routing_id, |
| 1601 &render_process_host_id, | 1672 &resource_type); |
| 1602 &routing_id, &resource_type); | |
| 1603 | 1673 |
| 1604 bool is_request_from_extension = | 1674 bool is_request_from_extension = |
| 1605 IsRequestFromExtension(request, extension_info_map); | 1675 IsRequestFromExtension(request, extension_info_map); |
| 1606 | 1676 |
| 1607 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | 1677 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
| 1608 // We are conservative here and assume requests are asynchronous in case | 1678 // We are conservative here and assume requests are asynchronous in case |
| 1609 // we don't have an info object. We don't want to risk a deadlock. | 1679 // we don't have an info object. We don't want to risk a deadlock. |
| 1610 bool is_async_request = !info || info->IsAsync(); | 1680 bool is_async_request = !info || info->IsAsync(); |
| 1611 | 1681 |
| 1612 EventListeners matching_listeners; | 1682 EventListeners matching_listeners; |
| (...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2484 // Continue gracefully. | 2554 // Continue gracefully. |
| 2485 RunSync(); | 2555 RunSync(); |
| 2486 } | 2556 } |
| 2487 | 2557 |
| 2488 bool WebRequestHandlerBehaviorChangedFunction::RunSync() { | 2558 bool WebRequestHandlerBehaviorChangedFunction::RunSync() { |
| 2489 helpers::ClearCacheOnNavigation(); | 2559 helpers::ClearCacheOnNavigation(); |
| 2490 return true; | 2560 return true; |
| 2491 } | 2561 } |
| 2492 | 2562 |
| 2493 } // namespace extensions | 2563 } // namespace extensions |
| OLD | NEW |