| 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 #ifndef CHROME_RENDERER_EXTENSIONS_CHROME_V8_EXTENSION_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_CHROME_V8_EXTENSION_H_ |
| 6 #define CHROME_RENDERER_EXTENSIONS_CHROME_V8_EXTENSION_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_CHROME_V8_EXTENSION_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/linked_ptr.h" | 10 #include "base/memory/linked_ptr.h" |
| 11 #include "base/string_piece.h" | 11 #include "base/string_piece.h" |
| 12 #include "chrome/renderer/extensions/chrome_v8_extension_handler.h" | 12 #include "chrome/renderer/extensions/chrome_v8_extension_handler.h" |
| 13 #include "chrome/renderer/native_handler.h" |
| 13 #include "v8/include/v8.h" | 14 #include "v8/include/v8.h" |
| 14 | 15 |
| 15 #include <map> | 16 #include <map> |
| 16 #include <set> | 17 #include <set> |
| 17 #include <string> | 18 #include <string> |
| 18 | 19 |
| 19 class ChromeV8Context; | 20 class ChromeV8Context; |
| 20 class Extension; | 21 class Extension; |
| 21 class ExtensionDispatcher; | 22 class ExtensionDispatcher; |
| 22 | 23 |
| 23 namespace content { | 24 namespace content { |
| 24 class RenderView; | 25 class RenderView; |
| 25 } | 26 } |
| 26 | 27 |
| 27 // This is a base class for chrome extension bindings. Common features that | 28 // This is a base class for chrome extension bindings. Common features that |
| 28 // are shared by different modules go here. | 29 // are shared by different modules go here. |
| 29 class ChromeV8Extension : public v8::Extension { | 30 // TODO(koz): Rename this to ExtensionNativeModule. |
| 31 class ChromeV8Extension : public NativeHandler { |
| 30 public: | 32 public: |
| 31 typedef std::set<ChromeV8Extension*> InstanceSet; | 33 typedef std::set<ChromeV8Extension*> InstanceSet; |
| 32 static const InstanceSet& GetAll(); | 34 static const InstanceSet& GetAll(); |
| 33 | 35 |
| 34 ChromeV8Extension(const char* name, | 36 explicit ChromeV8Extension(ExtensionDispatcher* extension_dispatcher); |
| 35 int resource_id, | |
| 36 ExtensionDispatcher* extension_dispatcher); | |
| 37 ChromeV8Extension(const char* name, | |
| 38 int resource_id, | |
| 39 int dependency_count, | |
| 40 const char** dependencies, | |
| 41 ExtensionDispatcher* extension_dispatcher); | |
| 42 virtual ~ChromeV8Extension(); | 37 virtual ~ChromeV8Extension(); |
| 43 | 38 |
| 44 ExtensionDispatcher* extension_dispatcher() { return extension_dispatcher_; } | 39 ExtensionDispatcher* extension_dispatcher() { return extension_dispatcher_; } |
| 45 | 40 |
| 46 void ContextWillBeReleased(ChromeV8Context* context); | |
| 47 | |
| 48 // Derived classes should call this at the end of their implementation in | |
| 49 // order to expose common native functions, like GetChromeHidden, to the | |
| 50 // v8 extension. | |
| 51 virtual v8::Handle<v8::FunctionTemplate> | |
| 52 GetNativeFunction(v8::Handle<v8::String> name) OVERRIDE; | |
| 53 | |
| 54 protected: | 41 protected: |
| 55 template<class T> | 42 template<class T> |
| 56 static T* GetFromArguments(const v8::Arguments& args) { | 43 static T* GetFromArguments(const v8::Arguments& args) { |
| 57 CHECK(!args.Data().IsEmpty()); | 44 CHECK(!args.Data().IsEmpty()); |
| 58 T* result = static_cast<T*>(args.Data().As<v8::External>()->Value()); | 45 T* result = static_cast<T*>(args.Data().As<v8::External>()->Value()); |
| 59 return result; | 46 return result; |
| 60 } | 47 } |
| 61 | 48 |
| 62 // Gets the render view for the current v8 context. | 49 // Gets the render view for the current v8 context. |
| 63 static content::RenderView* GetCurrentRenderView(); | 50 static content::RenderView* GetCurrentRenderView(); |
| 64 | 51 |
| 65 // Note: do not call this function before or during the chromeHidden.onLoad | 52 // Note: do not call this function before or during the chromeHidden.onLoad |
| 66 // event dispatch. The URL might not have been committed yet and might not | 53 // event dispatch. The URL might not have been committed yet and might not |
| 67 // be an extension URL. | 54 // be an extension URL. |
| 68 const ::Extension* GetExtensionForCurrentRenderView() const; | 55 const ::Extension* GetExtensionForCurrentRenderView() const; |
| 69 | 56 |
| 70 // Checks that the current context contains an extension that has permission | 57 // Checks that the current context contains an extension that has permission |
| 71 // to execute the specified function. If it does not, a v8 exception is thrown | 58 // to execute the specified function. If it does not, a v8 exception is thrown |
| 72 // and the method returns false. Otherwise returns true. | 59 // and the method returns false. Otherwise returns true. |
| 73 bool CheckCurrentContextAccessToExtensionAPI( | 60 bool CheckCurrentContextAccessToExtensionAPI( |
| 74 const std::string& function_name) const; | 61 const std::string& function_name) const; |
| 75 | 62 |
| 76 // Create a handler for |context|. If a subclass of ChromeV8Extension wishes | |
| 77 // to support handlers, it should override this. | |
| 78 virtual ChromeV8ExtensionHandler* CreateHandler(ChromeV8Context* context); | |
| 79 | |
| 80 // Returns the chromeHidden object for the current context. | 63 // Returns the chromeHidden object for the current context. |
| 81 static v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args); | 64 static v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args); |
| 82 | 65 |
| 83 ExtensionDispatcher* extension_dispatcher_; | 66 ExtensionDispatcher* extension_dispatcher_; |
| 84 | 67 |
| 85 private: | 68 private: |
| 86 static base::StringPiece GetStringResource(int resource_id); | |
| 87 | |
| 88 // Helper to print from bindings javascript. | |
| 89 static v8::Handle<v8::Value> Print(const v8::Arguments& args); | |
| 90 | |
| 91 // Handle a native function call from JavaScript. | |
| 92 static v8::Handle<v8::Value> HandleNativeFunction(const v8::Arguments& args); | |
| 93 | |
| 94 // Get the handler instance for |context|, or NULL if no such context exists. | |
| 95 ChromeV8ExtensionHandler* GetHandler(ChromeV8Context* context) const; | |
| 96 | |
| 97 // Map of all current handlers. | |
| 98 typedef std::map<ChromeV8Context*, | |
| 99 linked_ptr<ChromeV8ExtensionHandler> > HandlerMap; | |
| 100 HandlerMap handlers_; | |
| 101 | |
| 102 DISALLOW_COPY_AND_ASSIGN(ChromeV8Extension); | 69 DISALLOW_COPY_AND_ASSIGN(ChromeV8Extension); |
| 103 }; | 70 }; |
| 104 | 71 |
| 105 #endif // CHROME_RENDERER_EXTENSIONS_CHROME_V8_EXTENSION_H_ | 72 #endif // CHROME_RENDERER_EXTENSIONS_CHROME_V8_EXTENSION_H_ |
| OLD | NEW |