| 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" | |
| 14 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
| 15 | 14 |
| 16 #include <map> | 15 #include <map> |
| 17 #include <set> | 16 #include <set> |
| 18 #include <string> | 17 #include <string> |
| 19 | 18 |
| 20 class ChromeV8Context; | 19 class ChromeV8Context; |
| 21 class Extension; | 20 class Extension; |
| 22 class ExtensionDispatcher; | 21 class ExtensionDispatcher; |
| 23 | 22 |
| 24 namespace content { | 23 namespace content { |
| 25 class RenderView; | 24 class RenderView; |
| 26 } | 25 } |
| 27 | 26 |
| 28 // This is a base class for chrome extension bindings. Common features that | 27 // This is a base class for chrome extension bindings. Common features that |
| 29 // are shared by different modules go here. | 28 // are shared by different modules go here. |
| 30 // TODO(koz): Rename this to ExtensionNativeModule. | 29 class ChromeV8Extension : public v8::Extension { |
| 31 class ChromeV8Extension : public NativeHandler { | |
| 32 public: | 30 public: |
| 33 typedef std::set<ChromeV8Extension*> InstanceSet; | 31 typedef std::set<ChromeV8Extension*> InstanceSet; |
| 34 static const InstanceSet& GetAll(); | 32 static const InstanceSet& GetAll(); |
| 35 | 33 |
| 36 explicit ChromeV8Extension(ExtensionDispatcher* extension_dispatcher); | 34 ChromeV8Extension(const char* name, |
| 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); |
| 37 virtual ~ChromeV8Extension(); | 42 virtual ~ChromeV8Extension(); |
| 38 | 43 |
| 39 ExtensionDispatcher* extension_dispatcher() { return extension_dispatcher_; } | 44 ExtensionDispatcher* extension_dispatcher() { return extension_dispatcher_; } |
| 40 | 45 |
| 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 |
| 41 protected: | 54 protected: |
| 42 template<class T> | 55 template<class T> |
| 43 static T* GetFromArguments(const v8::Arguments& args) { | 56 static T* GetFromArguments(const v8::Arguments& args) { |
| 44 CHECK(!args.Data().IsEmpty()); | 57 CHECK(!args.Data().IsEmpty()); |
| 45 T* result = static_cast<T*>(args.Data().As<v8::External>()->Value()); | 58 T* result = static_cast<T*>(args.Data().As<v8::External>()->Value()); |
| 46 return result; | 59 return result; |
| 47 } | 60 } |
| 48 | 61 |
| 49 // Gets the render view for the current v8 context. | 62 // Gets the render view for the current v8 context. |
| 50 static content::RenderView* GetCurrentRenderView(); | 63 static content::RenderView* GetCurrentRenderView(); |
| 51 | 64 |
| 52 // Note: do not call this function before or during the chromeHidden.onLoad | 65 // Note: do not call this function before or during the chromeHidden.onLoad |
| 53 // event dispatch. The URL might not have been committed yet and might not | 66 // event dispatch. The URL might not have been committed yet and might not |
| 54 // be an extension URL. | 67 // be an extension URL. |
| 55 const ::Extension* GetExtensionForCurrentRenderView() const; | 68 const ::Extension* GetExtensionForCurrentRenderView() const; |
| 56 | 69 |
| 57 // Checks that the current context contains an extension that has permission | 70 // Checks that the current context contains an extension that has permission |
| 58 // to execute the specified function. If it does not, a v8 exception is thrown | 71 // to execute the specified function. If it does not, a v8 exception is thrown |
| 59 // and the method returns false. Otherwise returns true. | 72 // and the method returns false. Otherwise returns true. |
| 60 bool CheckCurrentContextAccessToExtensionAPI( | 73 bool CheckCurrentContextAccessToExtensionAPI( |
| 61 const std::string& function_name) const; | 74 const std::string& function_name) const; |
| 62 | 75 |
| 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 |
| 63 // Returns the chromeHidden object for the current context. | 80 // Returns the chromeHidden object for the current context. |
| 64 static v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args); | 81 static v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args); |
| 65 | 82 |
| 66 ExtensionDispatcher* extension_dispatcher_; | 83 ExtensionDispatcher* extension_dispatcher_; |
| 67 | 84 |
| 68 private: | 85 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 |
| 69 DISALLOW_COPY_AND_ASSIGN(ChromeV8Extension); | 102 DISALLOW_COPY_AND_ASSIGN(ChromeV8Extension); |
| 70 }; | 103 }; |
| 71 | 104 |
| 72 #endif // CHROME_RENDERER_EXTENSIONS_CHROME_V8_EXTENSION_H_ | 105 #endif // CHROME_RENDERER_EXTENSIONS_CHROME_V8_EXTENSION_H_ |
| OLD | NEW |