| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/extension_webrequest_api.h" | 5 #include "chrome/browser/extensions/extension_webrequest_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/browser_thread.h" | 11 #include "chrome/browser/browser_thread.h" |
| 12 #include "chrome/browser/extensions/extension_io_event_router.h" | 12 #include "chrome/browser/extensions/extension_event_router_forwarder.h" |
| 13 #include "chrome/browser/extensions/extension_webrequest_api_constants.h" | 13 #include "chrome/browser/extensions/extension_webrequest_api_constants.h" |
| 14 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_extent.h" | 15 #include "chrome/common/extensions/extension_extent.h" |
| 16 #include "chrome/common/extensions/url_pattern.h" | 16 #include "chrome/common/extensions/url_pattern.h" |
| 17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 18 | 18 |
| 19 namespace keys = extension_webrequest_api_constants; | 19 namespace keys = extension_webrequest_api_constants; |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 void ExtensionWebRequestEventRouter::RemoveEventListenerOnUIThread( | 202 void ExtensionWebRequestEventRouter::RemoveEventListenerOnUIThread( |
| 203 const std::string& extension_id, const std::string& sub_event_name) { | 203 const std::string& extension_id, const std::string& sub_event_name) { |
| 204 BrowserThread::PostTask( | 204 BrowserThread::PostTask( |
| 205 BrowserThread::IO, FROM_HERE, | 205 BrowserThread::IO, FROM_HERE, |
| 206 NewRunnableFunction( | 206 NewRunnableFunction( |
| 207 &RemoveEventListenerOnIOThread, | 207 &RemoveEventListenerOnIOThread, |
| 208 extension_id, sub_event_name)); | 208 extension_id, sub_event_name)); |
| 209 } | 209 } |
| 210 | 210 |
| 211 void ExtensionWebRequestEventRouter::OnBeforeRequest( | 211 void ExtensionWebRequestEventRouter::OnBeforeRequest( |
| 212 const ExtensionIOEventRouter* event_router, | 212 ExtensionEventRouterForwarder* event_router, |
| 213 ProfileId profile_id, |
| 213 const GURL& url, | 214 const GURL& url, |
| 214 const std::string& method) { | 215 const std::string& method) { |
| 216 // TODO(jochen): Figure out what to do with events from the system context. |
| 217 if (profile_id == Profile::InvalidProfileId) |
| 218 return; |
| 215 std::vector<const EventListener*> listeners = | 219 std::vector<const EventListener*> listeners = |
| 216 GetMatchingListeners(keys::kOnBeforeRequest, url); | 220 GetMatchingListeners(keys::kOnBeforeRequest, url); |
| 217 if (listeners.empty()) | 221 if (listeners.empty()) |
| 218 return; | 222 return; |
| 219 | 223 |
| 220 ListValue args; | 224 ListValue args; |
| 221 DictionaryValue* dict = new DictionaryValue(); | 225 DictionaryValue* dict = new DictionaryValue(); |
| 222 dict->SetString(keys::kUrlKey, url.spec()); | 226 dict->SetString(keys::kUrlKey, url.spec()); |
| 223 dict->SetString(keys::kMethodKey, method); | 227 dict->SetString(keys::kMethodKey, method); |
| 224 // TODO(mpcomplete): implement | 228 // TODO(mpcomplete): implement |
| 225 dict->SetInteger(keys::kTabIdKey, 0); | 229 dict->SetInteger(keys::kTabIdKey, 0); |
| 226 dict->SetInteger(keys::kRequestIdKey, 0); | 230 dict->SetInteger(keys::kRequestIdKey, 0); |
| 227 dict->SetString(keys::kTypeKey, "main_frame"); | 231 dict->SetString(keys::kTypeKey, "main_frame"); |
| 228 dict->SetInteger(keys::kTimeStampKey, 1); | 232 dict->SetInteger(keys::kTimeStampKey, 1); |
| 229 args.Append(dict); | 233 args.Append(dict); |
| 230 | 234 |
| 231 std::string json_args; | 235 std::string json_args; |
| 232 base::JSONWriter::Write(&args, false, &json_args); | 236 base::JSONWriter::Write(&args, false, &json_args); |
| 233 | 237 |
| 234 for (std::vector<const EventListener*>::iterator it = listeners.begin(); | 238 for (std::vector<const EventListener*>::iterator it = listeners.begin(); |
| 235 it != listeners.end(); ++it) { | 239 it != listeners.end(); ++it) { |
| 240 |
| 236 event_router->DispatchEventToExtension( | 241 event_router->DispatchEventToExtension( |
| 237 (*it)->extension_id, (*it)->sub_event_name, json_args); | 242 (*it)->extension_id, (*it)->sub_event_name, json_args, |
| 243 profile_id, true, GURL()); |
| 238 } | 244 } |
| 239 } | 245 } |
| 240 | 246 |
| 241 void ExtensionWebRequestEventRouter::AddEventListener( | 247 void ExtensionWebRequestEventRouter::AddEventListener( |
| 242 const std::string& extension_id, | 248 const std::string& extension_id, |
| 243 const std::string& event_name, | 249 const std::string& event_name, |
| 244 const std::string& sub_event_name, | 250 const std::string& sub_event_name, |
| 245 const RequestFilter& filter, | 251 const RequestFilter& filter, |
| 246 int extra_info_spec) { | 252 int extra_info_spec) { |
| 247 if (!IsWebRequestEvent(event_name)) | 253 if (!IsWebRequestEvent(event_name)) |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 EXTENSION_FUNCTION_VALIDATE(args_->GetString(4, &sub_event_name)); | 321 EXTENSION_FUNCTION_VALIDATE(args_->GetString(4, &sub_event_name)); |
| 316 | 322 |
| 317 BrowserThread::PostTask( | 323 BrowserThread::PostTask( |
| 318 BrowserThread::IO, FROM_HERE, | 324 BrowserThread::IO, FROM_HERE, |
| 319 NewRunnableFunction( | 325 NewRunnableFunction( |
| 320 &AddEventListenerOnIOThread, | 326 &AddEventListenerOnIOThread, |
| 321 extension_id(), event_name, sub_event_name, filter, extra_info_spec)); | 327 extension_id(), event_name, sub_event_name, filter, extra_info_spec)); |
| 322 | 328 |
| 323 return true; | 329 return true; |
| 324 } | 330 } |
| OLD | NEW |