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 "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
14 | 14 |
15 #include <map> | 15 #include <map> |
16 #include <set> | 16 #include <set> |
17 #include <string> | 17 #include <string> |
18 | 18 |
19 class ChromeV8Context; | 19 class ChromeV8Context; |
20 class Extension; | 20 class Extension; |
21 class ExtensionDispatcher; | 21 class ExtensionDispatcher; |
22 | 22 |
23 namespace content { | 23 namespace content { |
24 class RenderView; | 24 class RenderView; |
25 } | 25 } |
26 | 26 |
27 // 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 |
28 // are shared by different modules go here. | 28 // are shared by different modules go here. |
29 class ChromeV8Extension : public v8::Extension { | 29 class ChromeV8Extension : public v8::Extension { |
Aaron Boodman
2012/02/16 01:54:42
The inheritance on v8::Extension no longer makes s
Aaron Boodman
2012/02/16 01:54:42
Now that this is no longer inheriting from v8::Ext
koz (OOO until 15th September)
2012/02/27 01:06:14
I've added a TODO for this.
| |
30 public: | 30 public: |
31 typedef std::set<ChromeV8Extension*> InstanceSet; | 31 typedef std::set<ChromeV8Extension*> InstanceSet; |
32 static const InstanceSet& GetAll(); | 32 static const InstanceSet& GetAll(); |
33 | 33 |
34 ChromeV8Extension(const char* name, | 34 ChromeV8Extension(const char* name, |
35 int resource_id, | 35 int resource_id, |
36 ExtensionDispatcher* extension_dispatcher); | 36 ExtensionDispatcher* extension_dispatcher); |
37 ChromeV8Extension(const char* name, | 37 ChromeV8Extension(const char* name, |
Aaron Boodman
2012/02/16 01:54:42
Do we still use the name and dependency params?
koz (OOO until 15th September)
2012/02/27 01:06:14
Removed.
| |
38 int resource_id, | 38 int resource_id, |
39 int dependency_count, | 39 int dependency_count, |
40 const char** dependencies, | 40 const char** dependencies, |
41 ExtensionDispatcher* extension_dispatcher); | 41 ExtensionDispatcher* extension_dispatcher); |
42 virtual ~ChromeV8Extension(); | 42 virtual ~ChromeV8Extension(); |
43 | 43 |
44 ExtensionDispatcher* extension_dispatcher() { return extension_dispatcher_; } | 44 ExtensionDispatcher* extension_dispatcher() { return extension_dispatcher_; } |
45 | 45 |
46 void ContextWillBeReleased(ChromeV8Context* context); | 46 void ContextWillBeReleased(ChromeV8Context* context); |
Aaron Boodman
2012/02/16 01:54:42
This might no longer be needed.
koz (OOO until 15th September)
2012/02/27 01:06:14
Removed.
| |
47 | 47 |
48 // Derived classes should call this at the end of their implementation in | 48 // Derived classes should override this to set up named functions on |
49 // order to expose common native functions, like GetChromeHidden, to the | 49 // |object|. |
50 // v8 extension. | 50 virtual void SetNativeFunctions(v8::Handle<v8::Object> object) {} |
51 virtual v8::Handle<v8::FunctionTemplate> | |
52 GetNativeFunction(v8::Handle<v8::String> name) OVERRIDE; | |
53 | 51 |
54 protected: | 52 protected: |
55 template<class T> | 53 template<class T> |
56 static T* GetFromArguments(const v8::Arguments& args) { | 54 static T* GetFromArguments(const v8::Arguments& args) { |
57 CHECK(!args.Data().IsEmpty()); | 55 CHECK(!args.Data().IsEmpty()); |
58 T* result = static_cast<T*>(args.Data().As<v8::External>()->Value()); | 56 T* result = static_cast<T*>(args.Data().As<v8::External>()->Value()); |
59 return result; | 57 return result; |
60 } | 58 } |
61 | 59 |
60 static v8::Handle<v8::Value> Router(const v8::Arguments& args); | |
Aaron Boodman
2012/02/16 01:54:42
This should not be exposed as interface. It can be
koz (OOO until 15th September)
2012/02/27 01:06:14
Done.
| |
61 | |
62 // Gets the render view for the current v8 context. | 62 // Gets the render view for the current v8 context. |
63 static content::RenderView* GetCurrentRenderView(); | 63 static content::RenderView* GetCurrentRenderView(); |
64 | 64 |
65 // 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 |
66 // 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 |
67 // be an extension URL. | 67 // be an extension URL. |
68 const ::Extension* GetExtensionForCurrentRenderView() const; | 68 const ::Extension* GetExtensionForCurrentRenderView() const; |
69 | 69 |
70 // Checks that the current context contains an extension that has permission | 70 // 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 | 71 // to execute the specified function. If it does not, a v8 exception is thrown |
72 // and the method returns false. Otherwise returns true. | 72 // and the method returns false. Otherwise returns true. |
73 bool CheckCurrentContextAccessToExtensionAPI( | 73 bool CheckCurrentContextAccessToExtensionAPI( |
74 const std::string& function_name) const; | 74 const std::string& function_name) const; |
75 | 75 |
76 // Create a handler for |context|. If a subclass of ChromeV8Extension wishes | 76 // Handle a native function call from JavaScript. |
77 // to support handlers, it should override this. | 77 virtual v8::Handle<v8::Value> HandleNativeFunction( |
78 virtual ChromeV8ExtensionHandler* CreateHandler(ChromeV8Context* context); | 78 const std::string& function_name, const v8::Arguments& args); |
79 | 79 |
80 // Returns the chromeHidden object for the current context. | 80 // Causes the object to set a function named |name| on |object| that gets |
81 static v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args); | 81 // handled by HandleNativeFunction(). |
82 void RouteFunctionHere(const char* name, v8::Handle<v8::Object> object); | |
Aaron Boodman
2012/02/16 01:54:42
I think something like AddNativeFunction would be
| |
83 | |
84 typedef v8::Handle<v8::Value> (*HandlerFunction)(const v8::Arguments& args); | |
85 // Causes the object to set |function| on |object| with name |name|. | |
86 void RouteFunctionToStatic(const char* name, HandlerFunction function, | |
87 v8::Handle<v8::Object> object); | |
82 | 88 |
83 ExtensionDispatcher* extension_dispatcher_; | 89 ExtensionDispatcher* extension_dispatcher_; |
84 | 90 |
85 private: | 91 private: |
86 static base::StringPiece GetStringResource(int resource_id); | 92 static base::StringPiece GetStringResource(int resource_id); |
87 | 93 |
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); | 94 DISALLOW_COPY_AND_ASSIGN(ChromeV8Extension); |
103 }; | 95 }; |
104 | 96 |
105 #endif // CHROME_RENDERER_EXTENSIONS_CHROME_V8_EXTENSION_H_ | 97 #endif // CHROME_RENDERER_EXTENSIONS_CHROME_V8_EXTENSION_H_ |
OLD | NEW |