1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ 7 #pragma once 8 9 #include <map> 10 #include <set> 11 #include <string> 12 #include <vector> 13 14 #include "base/memory/singleton.h" 15 #include "chrome/browser/extensions/extension_function.h" 16 #include "chrome/browser/profiles/profile.h" 17 #include "ipc/ipc_message.h" 18 #include "net/base/completion_callback.h" 19 #include "webkit/glue/resource_type.h" 20 21 class ExtensionEventRouterForwarder; 22 class GURL; 23 24 namespace net { 25 class HttpRequestHeaders; 26 class URLRequest; 27 } 28 29 // This class observes network events and routes them to the appropriate 30 // extensions listening to those events. All methods must be called on the IO 31 // thread unless otherwise specified. 32 class ExtensionWebRequestEventRouter { 33 public: 34 struct RequestFilter; 35 struct ExtraInfoSpec; 36 37 static ExtensionWebRequestEventRouter* GetInstance(); 38 39 // Dispatches the OnBeforeRequest event to any extensions whose filters match 40 // the given request. Returns net::ERR_IO_PENDING if an extension is 41 // intercepting the request, OK otherwise. 42 int OnBeforeRequest(ProfileId profile_id, 43 ExtensionEventRouterForwarder* event_router, 44 net::URLRequest* request, 45 net::CompletionCallback* callback, 46 GURL* new_url); 47 48 // Dispatches the onBeforeSendHeaders event. This is fired for HTTP(s) 49 // requests only, and allows modification of the outgoing request headers. 50 // Returns net::ERR_IO_PENDING if an extension is intercepting the request, OK 51 // otherwise. 52 int OnBeforeSendHeaders(ProfileId profile_id, 53 ExtensionEventRouterForwarder* event_router, 54 uint64 request_id, 55 net::CompletionCallback* callback, 56 net::HttpRequestHeaders* headers); 57 58 void OnURLRequestDestroyed(ProfileId profile_id, net::URLRequest* request); 59 60 // Called when an event listener handles a blocking event and responds. 61 // TODO(mpcomplete): modify request 62 void OnEventHandled( 63 ProfileId profile_id, 64 const std::string& extension_id, 65 const std::string& event_name, 66 const std::string& sub_event_name, 67 uint64 request_id, 68 bool cancel, 69 const GURL& new_url); 70 71 // Adds a listener to the given event. |event_name| specifies the event being 72 // listened to. |sub_event_name| is an internal event uniquely generated in 73 // the extension process to correspond to the given filter and 74 // extra_info_spec. 75 void AddEventListener( 76 ProfileId profile_id, 77 const std::string& extension_id, 78 const std::string& event_name, 79 const std::string& sub_event_name, 80 const RequestFilter& filter, 81 int extra_info_spec); 82 83 // Removes the listener for the given sub-event. 84 void RemoveEventListener( 85 ProfileId profile_id, 86 const std::string& extension_id, 87 const std::string& sub_event_name); 88 89 private: 90 friend struct DefaultSingletonTraits<ExtensionWebRequestEventRouter>; 91 struct EventListener; 92 struct BlockedRequest; 93 typedef std::map<std::string, std::set<EventListener> > ListenerMapForProfile; 94 typedef std::map<ProfileId, ListenerMapForProfile> ListenerMap; 95 typedef std::map<uint64, BlockedRequest> BlockedRequestMap; 96 typedef std::map<uint64, net::URLRequest*> HttpRequestMap; 97 98 ExtensionWebRequestEventRouter(); 99 ~ExtensionWebRequestEventRouter(); 100 101 bool DispatchEvent( 102 ProfileId profile_id, 103 ExtensionEventRouterForwarder* event_router, 104 net::URLRequest* request, 105 net::CompletionCallback* callback, 106 const std::vector<const EventListener*>& listeners, 107 const ListValue& args); 108 109 // Returns a list of event listeners that care about the given event, based 110 // on their filter parameters. 111 std::vector<const EventListener*> GetMatchingListeners( 112 ProfileId profile_id, 113 const std::string& event_name, 114 const GURL& url, 115 int tab_id, 116 int window_id, 117 ResourceType::Type resource_type); 118 119 // Same as above, but retrieves the filter parameters from the request. 120 std::vector<const EventListener*> GetMatchingListeners( 121 ProfileId profile_id, 122 const std::string& event_name, 123 net::URLRequest* request); 124 125 // Decrements the count of event handlers blocking the given request. When the 126 // count reaches 0 (or immediately if the request is being cancelled), we 127 // stop blocking the request and either resume or cancel it. 128 void DecrementBlockCount(uint64 request_id, bool cancel, const GURL& new_url); 129 130 void OnRequestDeleted(net::URLRequest* request); 131 132 // A map for each profile that maps an event name to a set of extensions that 133 // are listening to that event. 134 ListenerMap listeners_; 135 136 // A map of network requests that are waiting for at least one event handler 137 // to respond. 138 BlockedRequestMap blocked_requests_; 139 140 // A map of HTTP(s) network requests. We use this to look up the URLRequest 141 // from the request ID given to us for HTTP-specific events. 142 HttpRequestMap http_requests_; 143 144 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestEventRouter); 145 }; 146 147 class WebRequestAddEventListener : public SyncExtensionFunction { 148 public: 149 virtual bool RunImpl(); 150 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.addEventListener"); 151 }; 152 153 class WebRequestEventHandled : public SyncExtensionFunction { 154 public: 155 virtual bool RunImpl(); 156 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.eventHandled"); 157 }; 158 159 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ 160