Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: extensions/browser/api/web_request/web_request_api.cc

Issue 1413543005: Use FrameTreeNode ID as frameId in extension APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Guarantee callback order, remove deleted frames from map Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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 // Extract a pair of IDs to identify the RenderFrameHost and store it in |dict|.
223 // The caller will then map this pair of IDs frame IDs for the public extension
224 // API, and finally store these IDs in |dict| using ExtractApiFrameIdInfo.
225 void ExtractRenderFrameInfo(base::DictionaryValue* dict,
226 int* render_process_id,
227 int* render_frame_id) {
228 if (!dict->GetInteger(keys::kFrameIdKey, render_frame_id) ||
229 !dict->GetInteger(keys::kProcessIdKey, render_process_id)) {
230 *render_process_id = -1;
231 *render_frame_id = -1;
232 }
233 // ExtractApiFrameIdInfo will overwrite kFrameIdKey, so it's not removed here.
234 dict->Remove(keys::kProcessIdKey, nullptr);
235 }
236
237 // Extracts the extension frame IDs from |extension_api_frame_id| and store it
238 // |dict|. See ExtractRenderFrameInfo() for more details.
239 void ExtractApiFrameIdInfo(const ExtensionApiFrameId& extension_api_frame_id,
240 base::DictionaryValue* dict) {
241 dict->SetInteger(keys::kFrameIdKey, extension_api_frame_id.frame_id);
242 dict->SetInteger(keys::kParentFrameIdKey,
243 extension_api_frame_id.parent_frame_id);
244 }
245
229 // Extracts the body from |request| and writes the data into |out|. 246 // Extracts the body from |request| and writes the data into |out|.
230 void ExtractRequestInfoBody(const net::URLRequest* request, 247 void ExtractRequestInfoBody(const net::URLRequest* request,
231 base::DictionaryValue* out) { 248 base::DictionaryValue* out) {
232 const net::UploadDataStream* upload_data = request->get_upload(); 249 const net::UploadDataStream* upload_data = request->get_upload();
233 if (!upload_data || 250 if (!upload_data ||
234 (request->method() != "POST" && request->method() != "PUT")) { 251 (request->method() != "POST" && request->method() != "PUT")) {
235 return; // Need to exit without "out->Set(keys::kRequestBodyKey, ...);" . 252 return; // Need to exit without "out->Set(keys::kRequestBodyKey, ...);" .
236 } 253 }
237 254
238 base::DictionaryValue* request_body = new base::DictionaryValue(); 255 base::DictionaryValue* request_body = new base::DictionaryValue();
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 const WebViewRendererState::WebViewInfo& web_view_info, 368 const WebViewRendererState::WebViewInfo& web_view_info,
352 scoped_ptr<base::DictionaryValue> event_argument) { 369 scoped_ptr<base::DictionaryValue> event_argument) {
353 DCHECK_CURRENTLY_ON(BrowserThread::UI); 370 DCHECK_CURRENTLY_ON(BrowserThread::UI);
354 371
355 content::BrowserContext* browser_context = 372 content::BrowserContext* browser_context =
356 reinterpret_cast<content::BrowserContext*>(browser_context_id); 373 reinterpret_cast<content::BrowserContext*>(browser_context_id);
357 if (!ExtensionsBrowserClient::Get()->IsValidContext(browser_context)) 374 if (!ExtensionsBrowserClient::Get()->IsValidContext(browser_context))
358 return; 375 return;
359 376
360 scoped_ptr<base::ListValue> event_args(new base::ListValue); 377 scoped_ptr<base::ListValue> event_args(new base::ListValue);
378 int render_process_host_id = -1;
379 int render_frame_id = -1;
380 ExtractRenderFrameInfo(event_argument.get(), &render_process_host_id,
381 &render_frame_id);
382 const ExtensionApiFrameId& extension_api_frame_id =
383 ExtensionApiFrameIdMap::Get()->GetFrameId(render_process_host_id,
384 render_frame_id);
385 ExtractApiFrameIdInfo(extension_api_frame_id, event_argument.get());
361 event_args->Append(event_argument.release()); 386 event_args->Append(event_argument.release());
362 387
363 EventRouter* event_router = EventRouter::Get(browser_context); 388 EventRouter* event_router = EventRouter::Get(browser_context);
364 389
365 EventFilteringInfo event_filtering_info; 390 EventFilteringInfo event_filtering_info;
366 391
367 events::HistogramValue histogram_value = events::UNKNOWN; 392 events::HistogramValue histogram_value = events::UNKNOWN;
368 std::string event_name; 393 std::string event_name;
369 // The instance ID uniquely identifies a <webview> instance within an embedder 394 // 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 395 // 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
517 return sub_event_name < that.sub_event_name; 542 return sub_event_name < that.sub_event_name;
518 543
519 if (web_view_instance_id != that.web_view_instance_id) 544 if (web_view_instance_id != that.web_view_instance_id)
520 return web_view_instance_id < that.web_view_instance_id; 545 return web_view_instance_id < that.web_view_instance_id;
521 546
522 if (web_view_instance_id == 0) { 547 if (web_view_instance_id == 0) {
523 // Do not filter by process ID for non-webviews, because this comparator 548 // 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 549 // 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 550 // 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. 551 // the original process, so 0 is used instead of the actual process ID.
527 DCHECK(embedder_process_id == 0 || that.embedder_process_id == 0); 552 if (embedder_process_id == 0 || that.embedder_process_id == 0)
528 return false; 553 return false;
529 } 554 }
530 555
531 if (embedder_process_id != that.embedder_process_id) 556 if (embedder_process_id != that.embedder_process_id)
532 return embedder_process_id < that.embedder_process_id; 557 return embedder_process_id < that.embedder_process_id;
533 558
534 return false; 559 return false;
535 } 560 }
536 561
537 EventListener() 562 EventListener()
538 : histogram_value(events::UNKNOWN), 563 : histogram_value(events::UNKNOWN),
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 if (rules_registry.get()) 759 if (rules_registry.get())
735 rules_registries_[key] = rules_registry; 760 rules_registries_[key] = rules_registry;
736 else 761 else
737 rules_registries_.erase(key); 762 rules_registries_.erase(key);
738 } 763 }
739 764
740 void ExtensionWebRequestEventRouter::ExtractRequestInfo( 765 void ExtensionWebRequestEventRouter::ExtractRequestInfo(
741 const net::URLRequest* request, 766 const net::URLRequest* request,
742 base::DictionaryValue* out) { 767 base::DictionaryValue* out) {
743 bool is_main_frame = false; 768 bool is_main_frame = false;
744 int frame_id = -1; 769 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; 770 int render_process_host_id = -1;
750 int routing_id = -1; 771 int routing_id = -1;
751 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE; 772 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE;
752 ExtractRequestInfoDetails(request, &is_main_frame, &frame_id, 773 ExtractRequestInfoDetails(request, &is_main_frame, &render_frame_id,
753 &parent_is_main_frame, &parent_frame_id,
754 &render_process_host_id, &routing_id, 774 &render_process_host_id, &routing_id,
755 &resource_type); 775 &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 776
760 out->SetString(keys::kRequestIdKey, 777 out->SetString(keys::kRequestIdKey,
761 base::Uint64ToString(request->identifier())); 778 base::Uint64ToString(request->identifier()));
762 out->SetString(keys::kUrlKey, request->url().spec()); 779 out->SetString(keys::kUrlKey, request->url().spec());
763 out->SetString(keys::kMethodKey, request->method()); 780 out->SetString(keys::kMethodKey, request->method());
764 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); 781 // Note: This (frameId, processId) pair is removed by ExtractRenderFrameInfo,
765 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); 782 // and then finally set by ExtractApiFrameIdInfo.
783 // TODO(robwu): This is ugly. Create a proper data structure to separate these
784 // two IDs from the dictionary, so that kFrameIdKey has only one meaning.
785 out->SetInteger(keys::kFrameIdKey, render_frame_id);
786 out->SetInteger(keys::kProcessIdKey, render_process_host_id);
766 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); 787 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type));
767 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); 788 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000);
768 if (web_request_event_router_delegate_) { 789 if (web_request_event_router_delegate_) {
769 web_request_event_router_delegate_->ExtractExtraRequestDetails( 790 web_request_event_router_delegate_->ExtractExtraRequestDetails(
770 request, out); 791 request, out);
771 } 792 }
772 } 793 }
773 794
774 int ExtensionWebRequestEventRouter::OnBeforeRequest( 795 int ExtensionWebRequestEventRouter::OnBeforeRequest(
775 void* browser_context, 796 void* browser_context,
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
1234 } 1255 }
1235 1256
1236 bool ExtensionWebRequestEventRouter::DispatchEvent( 1257 bool ExtensionWebRequestEventRouter::DispatchEvent(
1237 void* browser_context, 1258 void* browser_context,
1238 net::URLRequest* request, 1259 net::URLRequest* request,
1239 const std::vector<const EventListener*>& listeners, 1260 const std::vector<const EventListener*>& listeners,
1240 const base::ListValue& args) { 1261 const base::ListValue& args) {
1241 // TODO(mpcomplete): Consider consolidating common (extension_id,json_args) 1262 // TODO(mpcomplete): Consider consolidating common (extension_id,json_args)
1242 // pairs into a single message sent to a list of sub_event_names. 1263 // pairs into a single message sent to a list of sub_event_names.
1243 int num_handlers_blocking = 0; 1264 int num_handlers_blocking = 0;
1265
1266 scoped_ptr<std::vector<EventListener>> listeners_to_dispatch(
1267 new std::vector<EventListener>());
1268 listeners_to_dispatch->reserve(listeners.size());
1244 for (const EventListener* listener : listeners) { 1269 for (const EventListener* listener : listeners) {
1245 // Filter out the optional keys that this listener didn't request. 1270 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 & 1271 if (listener->extra_info_spec &
1260 (ExtraInfoSpec::BLOCKING | ExtraInfoSpec::ASYNC_BLOCKING)) { 1272 (ExtraInfoSpec::BLOCKING | ExtraInfoSpec::ASYNC_BLOCKING)) {
1261 listener->blocked_requests.insert(request->identifier()); 1273 listener->blocked_requests.insert(request->identifier());
1262 // If this is the first delegate blocking the request, go ahead and log 1274 // If this is the first delegate blocking the request, go ahead and log
1263 // it. 1275 // it.
1264 if (num_handlers_blocking == 0) { 1276 if (num_handlers_blocking == 0) {
1265 std::string delegate_info = l10n_util::GetStringFUTF8( 1277 std::string delegate_info = l10n_util::GetStringFUTF8(
1266 IDS_LOAD_STATE_PARAMETER_EXTENSION, 1278 IDS_LOAD_STATE_PARAMETER_EXTENSION,
1267 base::UTF8ToUTF16(listener->extension_name)); 1279 base::UTF8ToUTF16(listener->extension_name));
1268 // LobAndReport allows extensions that block requests to be displayed in 1280 // LobAndReport allows extensions that block requests to be displayed in
1269 // the load status bar. 1281 // the load status bar.
1270 request->LogAndReportBlockedBy(delegate_info.c_str()); 1282 request->LogAndReportBlockedBy(delegate_info.c_str());
1271 } 1283 }
1272 ++num_handlers_blocking; 1284 ++num_handlers_blocking;
1273 } 1285 }
1274 } 1286 }
1275 1287
1288 // TODO(robwu): Avoid unnecessary copy, by changing |args| to be a
1289 // scoped_ptr<base::DictionaryValue> and transferring the ownership.
1290 const base::DictionaryValue* dict = nullptr;
1291 CHECK(args.GetDictionary(0, &dict) && dict);
1292 base::DictionaryValue* args_copy = dict->DeepCopy();
1293
1294 int render_process_host_id = -1;
1295 int render_frame_id = -1;
1296 ExtractRenderFrameInfo(args_copy, &render_process_host_id, &render_frame_id);
1297
1298 ExtensionApiFrameIdMap::Get()->GetFrameIdOnIO(
1299 render_process_host_id, render_frame_id,
1300 base::Bind(&ExtensionWebRequestEventRouter::DispatchEventToListeners,
1301 AsWeakPtr(), browser_context,
1302 base::Passed(&listeners_to_dispatch), base::Owned(args_copy)));
1303
1276 if (num_handlers_blocking > 0) { 1304 if (num_handlers_blocking > 0) {
1277 BlockedRequest& blocked_request = blocked_requests_[request->identifier()]; 1305 BlockedRequest& blocked_request = blocked_requests_[request->identifier()];
1278 blocked_request.request = request; 1306 blocked_request.request = request;
1279 blocked_request.is_incognito |= IsIncognitoBrowserContext(browser_context); 1307 blocked_request.is_incognito |= IsIncognitoBrowserContext(browser_context);
1280 blocked_request.num_handlers_blocking += num_handlers_blocking; 1308 blocked_request.num_handlers_blocking += num_handlers_blocking;
1281 blocked_request.blocking_time = base::Time::Now(); 1309 blocked_request.blocking_time = base::Time::Now();
1282 return true; 1310 return true;
1283 } 1311 }
1284 1312
1285 return false; 1313 return false;
1286 } 1314 }
1287 1315
1316 void ExtensionWebRequestEventRouter::DispatchEventToListeners(
1317 void* browser_context,
1318 scoped_ptr<std::vector<EventListener>> listeners,
1319 base::DictionaryValue* dict,
1320 const ExtensionApiFrameId extension_api_frame_id) {
1321 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1322 DCHECK(listeners.get());
1323 DCHECK_GT(listeners->size(), 0UL);
1324 DCHECK(dict);
1325
1326 ExtractApiFrameIdInfo(extension_api_frame_id, dict);
1327
1328 std::string event_name =
1329 EventRouter::GetBaseEventName((*listeners)[0].sub_event_name);
1330 DCHECK(IsWebRequestEvent(event_name));
1331
1332 const std::set<EventListener>& event_listeners =
1333 listeners_[browser_context][event_name];
1334 void* cross_browser_context = GetCrossBrowserContext(browser_context);
1335 const std::set<EventListener>* cross_event_listeners =
1336 cross_browser_context ? &listeners_[cross_browser_context][event_name]
1337 : nullptr;
1338
1339 for (const EventListener& target : *listeners) {
1340 std::set<EventListener>::const_iterator listener =
1341 event_listeners.find(target);
1342 // Ignore listener if it was removed between the thread hops.
1343 if (listener == event_listeners.end()) {
1344 if (!cross_event_listeners)
1345 continue;
1346 listener = cross_event_listeners->find(target);
1347 if (listener == cross_event_listeners->end())
1348 continue;
1349 }
1350
1351 // Filter out the optional keys that this listener didn't request.
1352 scoped_ptr<base::ListValue> args_filtered(new base::ListValue);
1353 args_filtered->Append(dict->DeepCopy());
1354 if (!(listener->extra_info_spec & ExtraInfoSpec::REQUEST_HEADERS))
1355 dict->Remove(keys::kRequestHeadersKey, nullptr);
1356 if (!(listener->extra_info_spec & ExtraInfoSpec::RESPONSE_HEADERS))
1357 dict->Remove(keys::kResponseHeadersKey, nullptr);
1358
1359 EventRouter::DispatchEventToSender(
1360 listener->ipc_sender.get(), browser_context, listener->extension_id,
1361 listener->histogram_value, listener->sub_event_name,
1362 args_filtered.Pass(), EventRouter::USER_GESTURE_UNKNOWN,
1363 EventFilteringInfo());
1364 }
1365 }
1366
1288 void ExtensionWebRequestEventRouter::OnEventHandled( 1367 void ExtensionWebRequestEventRouter::OnEventHandled(
1289 void* browser_context, 1368 void* browser_context,
1290 const std::string& extension_id, 1369 const std::string& extension_id,
1291 const std::string& event_name, 1370 const std::string& event_name,
1292 const std::string& sub_event_name, 1371 const std::string& sub_event_name,
1293 uint64_t request_id, 1372 uint64_t request_id,
1294 EventResponse* response) { 1373 EventResponse* response) {
1295 // TODO(robwu): Does this also work with webviews? operator< (used by find) 1374 // 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|. 1375 // takes the webview ID into account, which is not set on |listener|.
1297 EventListener listener; 1376 EventListener listener;
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
1431 } 1510 }
1432 1511
1433 void ExtensionWebRequestEventRouter::AddCallbackForPageLoad( 1512 void ExtensionWebRequestEventRouter::AddCallbackForPageLoad(
1434 const base::Closure& callback) { 1513 const base::Closure& callback) {
1435 callbacks_for_page_load_.push_back(callback); 1514 callbacks_for_page_load_.push_back(callback);
1436 } 1515 }
1437 1516
1438 bool ExtensionWebRequestEventRouter::IsPageLoad( 1517 bool ExtensionWebRequestEventRouter::IsPageLoad(
1439 const net::URLRequest* request) const { 1518 const net::URLRequest* request) const {
1440 bool is_main_frame = false; 1519 bool is_main_frame = false;
1441 int frame_id = -1; 1520 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; 1521 int render_process_host_id = -1;
1445 int routing_id = -1; 1522 int routing_id = -1;
1446 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE; 1523 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE;
1447 1524
1448 ExtractRequestInfoDetails(request, &is_main_frame, &frame_id, 1525 ExtractRequestInfoDetails(request, &is_main_frame, &render_frame_id,
1449 &parent_is_main_frame, &parent_frame_id, 1526 &render_process_host_id, &routing_id,
1450 &render_process_host_id, 1527 &resource_type);
1451 &routing_id, &resource_type);
1452 1528
1453 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME; 1529 return resource_type == content::RESOURCE_TYPE_MAIN_FRAME;
1454 } 1530 }
1455 1531
1456 void ExtensionWebRequestEventRouter::NotifyPageLoad() { 1532 void ExtensionWebRequestEventRouter::NotifyPageLoad() {
1457 for (const auto& callback : callbacks_for_page_load_) 1533 for (const auto& callback : callbacks_for_page_load_)
1458 callback.Run(); 1534 callback.Run();
1459 callbacks_for_page_load_.clear(); 1535 callbacks_for_page_load_.clear();
1460 } 1536 }
1461 1537
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
1581 void* browser_context, 1657 void* browser_context,
1582 const InfoMap* extension_info_map, 1658 const InfoMap* extension_info_map,
1583 const std::string& event_name, 1659 const std::string& event_name,
1584 const net::URLRequest* request, 1660 const net::URLRequest* request,
1585 int* extra_info_spec) { 1661 int* extra_info_spec) {
1586 // TODO(mpcomplete): handle browser_context == NULL (should collect all 1662 // TODO(mpcomplete): handle browser_context == NULL (should collect all
1587 // listeners). 1663 // listeners).
1588 *extra_info_spec = 0; 1664 *extra_info_spec = 0;
1589 1665
1590 bool is_main_frame = false; 1666 bool is_main_frame = false;
1591 int frame_id = -1; 1667 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; 1668 int render_process_host_id = -1;
1595 int routing_id = -1; 1669 int routing_id = -1;
1596 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE; 1670 ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE;
1597 const GURL& url = request->url(); 1671 const GURL& url = request->url();
1598 1672
1599 ExtractRequestInfoDetails(request, &is_main_frame, &frame_id, 1673 ExtractRequestInfoDetails(request, &is_main_frame, &render_frame_id,
1600 &parent_is_main_frame, &parent_frame_id, 1674 &render_process_host_id, &routing_id,
1601 &render_process_host_id, 1675 &resource_type);
1602 &routing_id, &resource_type);
1603 1676
1604 bool is_request_from_extension = 1677 bool is_request_from_extension =
1605 IsRequestFromExtension(request, extension_info_map); 1678 IsRequestFromExtension(request, extension_info_map);
1606 1679
1607 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); 1680 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
1608 // We are conservative here and assume requests are asynchronous in case 1681 // 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. 1682 // we don't have an info object. We don't want to risk a deadlock.
1610 bool is_async_request = !info || info->IsAsync(); 1683 bool is_async_request = !info || info->IsAsync();
1611 1684
1612 EventListeners matching_listeners; 1685 EventListeners matching_listeners;
(...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after
2484 // Continue gracefully. 2557 // Continue gracefully.
2485 RunSync(); 2558 RunSync();
2486 } 2559 }
2487 2560
2488 bool WebRequestHandlerBehaviorChangedFunction::RunSync() { 2561 bool WebRequestHandlerBehaviorChangedFunction::RunSync() {
2489 helpers::ClearCacheOnNavigation(); 2562 helpers::ClearCacheOnNavigation();
2490 return true; 2563 return true;
2491 } 2564 }
2492 2565
2493 } // namespace extensions 2566 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698