| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_function_dispatcher.h" | 5 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/process_util.h" | 9 #include "base/process_util.h" |
| 10 #include "base/singleton.h" | 10 #include "base/singleton.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 // Template for defining ExtensionFunctionFactory. | 68 // Template for defining ExtensionFunctionFactory. |
| 69 template<class T> | 69 template<class T> |
| 70 ExtensionFunction* NewExtensionFunction() { | 70 ExtensionFunction* NewExtensionFunction() { |
| 71 return new T(); | 71 return new T(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Contains a list of all known extension functions and allows clients to | 74 // Contains a list of all known extension functions and allows clients to |
| 75 // create instances of them. | 75 // create instances of them. |
| 76 class FactoryRegistry { | 76 class FactoryRegistry { |
| 77 public: | 77 public: |
| 78 static FactoryRegistry* instance(); | 78 static FactoryRegistry* GetInstance(); |
| 79 FactoryRegistry() { ResetFunctions(); } | 79 FactoryRegistry() { ResetFunctions(); } |
| 80 | 80 |
| 81 // Resets all functions to their default values. | 81 // Resets all functions to their default values. |
| 82 void ResetFunctions(); | 82 void ResetFunctions(); |
| 83 | 83 |
| 84 // Adds all function names to 'names'. | 84 // Adds all function names to 'names'. |
| 85 void GetAllNames(std::vector<std::string>* names); | 85 void GetAllNames(std::vector<std::string>* names); |
| 86 | 86 |
| 87 // Allows overriding of specific functions (e.g. for testing). Functions | 87 // Allows overriding of specific functions (e.g. for testing). Functions |
| 88 // must be previously registered. Returns true if successful. | 88 // must be previously registered. Returns true if successful. |
| 89 bool OverrideFunction(const std::string& name, | 89 bool OverrideFunction(const std::string& name, |
| 90 ExtensionFunctionFactory factory); | 90 ExtensionFunctionFactory factory); |
| 91 | 91 |
| 92 // Factory method for the ExtensionFunction registered as 'name'. | 92 // Factory method for the ExtensionFunction registered as 'name'. |
| 93 ExtensionFunction* NewFunction(const std::string& name); | 93 ExtensionFunction* NewFunction(const std::string& name); |
| 94 | 94 |
| 95 private: | 95 private: |
| 96 template<class T> | 96 template<class T> |
| 97 void RegisterFunction() { | 97 void RegisterFunction() { |
| 98 factories_[T::function_name()] = &NewExtensionFunction<T>; | 98 factories_[T::function_name()] = &NewExtensionFunction<T>; |
| 99 } | 99 } |
| 100 | 100 |
| 101 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; | 101 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; |
| 102 FactoryMap factories_; | 102 FactoryMap factories_; |
| 103 }; | 103 }; |
| 104 | 104 |
| 105 FactoryRegistry* FactoryRegistry::instance() { | 105 FactoryRegistry* FactoryRegistry::GetInstance() { |
| 106 return Singleton<FactoryRegistry>::get(); | 106 return Singleton<FactoryRegistry>::get(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void FactoryRegistry::ResetFunctions() { | 109 void FactoryRegistry::ResetFunctions() { |
| 110 // Register all functions here. | 110 // Register all functions here. |
| 111 | 111 |
| 112 // Windows | 112 // Windows |
| 113 RegisterFunction<GetWindowFunction>(); | 113 RegisterFunction<GetWindowFunction>(); |
| 114 RegisterFunction<GetCurrentWindowFunction>(); | 114 RegisterFunction<GetCurrentWindowFunction>(); |
| 115 RegisterFunction<GetLastFocusedWindowFunction>(); | 115 RegisterFunction<GetLastFocusedWindowFunction>(); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 function->set_name(name); | 318 function->set_name(name); |
| 319 return function; | 319 return function; |
| 320 } | 320 } |
| 321 | 321 |
| 322 }; // namespace | 322 }; // namespace |
| 323 | 323 |
| 324 // ExtensionFunctionDispatcher ------------------------------------------------- | 324 // ExtensionFunctionDispatcher ------------------------------------------------- |
| 325 | 325 |
| 326 void ExtensionFunctionDispatcher::GetAllFunctionNames( | 326 void ExtensionFunctionDispatcher::GetAllFunctionNames( |
| 327 std::vector<std::string>* names) { | 327 std::vector<std::string>* names) { |
| 328 FactoryRegistry::instance()->GetAllNames(names); | 328 FactoryRegistry::GetInstance()->GetAllNames(names); |
| 329 } | 329 } |
| 330 | 330 |
| 331 bool ExtensionFunctionDispatcher::OverrideFunction( | 331 bool ExtensionFunctionDispatcher::OverrideFunction( |
| 332 const std::string& name, ExtensionFunctionFactory factory) { | 332 const std::string& name, ExtensionFunctionFactory factory) { |
| 333 return FactoryRegistry::instance()->OverrideFunction(name, factory); | 333 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory); |
| 334 } | 334 } |
| 335 | 335 |
| 336 void ExtensionFunctionDispatcher::ResetFunctions() { | 336 void ExtensionFunctionDispatcher::ResetFunctions() { |
| 337 FactoryRegistry::instance()->ResetFunctions(); | 337 FactoryRegistry::GetInstance()->ResetFunctions(); |
| 338 } | 338 } |
| 339 | 339 |
| 340 ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create( | 340 ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create( |
| 341 RenderViewHost* render_view_host, | 341 RenderViewHost* render_view_host, |
| 342 Delegate* delegate, | 342 Delegate* delegate, |
| 343 const GURL& url) { | 343 const GURL& url) { |
| 344 ExtensionsService* service = | 344 ExtensionsService* service = |
| 345 render_view_host->process()->profile()->GetExtensionsService(); | 345 render_view_host->process()->profile()->GetExtensionsService(); |
| 346 DCHECK(service); | 346 DCHECK(service); |
| 347 | 347 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 // before the browser is sufficiently initialized to return here. | 437 // before the browser is sufficiently initialized to return here. |
| 438 // A similar situation may arise during shutdown. | 438 // A similar situation may arise during shutdown. |
| 439 // TODO(rafaelw): Delay creation of background_page until the browser | 439 // TODO(rafaelw): Delay creation of background_page until the browser |
| 440 // is available. http://code.google.com/p/chromium/issues/detail?id=13284 | 440 // is available. http://code.google.com/p/chromium/issues/detail?id=13284 |
| 441 return browser; | 441 return browser; |
| 442 } | 442 } |
| 443 | 443 |
| 444 void ExtensionFunctionDispatcher::HandleRequest( | 444 void ExtensionFunctionDispatcher::HandleRequest( |
| 445 const ViewHostMsg_DomMessage_Params& params) { | 445 const ViewHostMsg_DomMessage_Params& params) { |
| 446 scoped_refptr<ExtensionFunction> function( | 446 scoped_refptr<ExtensionFunction> function( |
| 447 FactoryRegistry::instance()->NewFunction(params.name)); | 447 FactoryRegistry::GetInstance()->NewFunction(params.name)); |
| 448 function->set_dispatcher_peer(peer_); | 448 function->set_dispatcher_peer(peer_); |
| 449 function->set_profile(profile_); | 449 function->set_profile(profile_); |
| 450 function->set_extension_id(extension_id()); | 450 function->set_extension_id(extension_id()); |
| 451 function->SetArgs(¶ms.arguments); | 451 function->SetArgs(¶ms.arguments); |
| 452 function->set_source_url(params.source_url); | 452 function->set_source_url(params.source_url); |
| 453 function->set_request_id(params.request_id); | 453 function->set_request_id(params.request_id); |
| 454 function->set_has_callback(params.has_callback); | 454 function->set_has_callback(params.has_callback); |
| 455 function->set_user_gesture(params.user_gesture); | 455 function->set_user_gesture(params.user_gesture); |
| 456 ExtensionsService* service = profile()->GetExtensionsService(); | 456 ExtensionsService* service = profile()->GetExtensionsService(); |
| 457 DCHECK(service); | 457 DCHECK(service); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 } else { | 494 } else { |
| 495 NOTREACHED(); | 495 NOTREACHED(); |
| 496 base::KillProcess(render_view_host_->process()->GetHandle(), | 496 base::KillProcess(render_view_host_->process()->GetHandle(), |
| 497 ResultCodes::KILLED_BAD_MESSAGE, false); | 497 ResultCodes::KILLED_BAD_MESSAGE, false); |
| 498 } | 498 } |
| 499 } | 499 } |
| 500 | 500 |
| 501 Profile* ExtensionFunctionDispatcher::profile() { | 501 Profile* ExtensionFunctionDispatcher::profile() { |
| 502 return profile_; | 502 return profile_; |
| 503 } | 503 } |
| OLD | NEW |