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 #ifndef CHROME_RENDERER_EXTENSIONS_BINDINGS_UTILS_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_BINDINGS_UTILS_H_ |
6 #define CHROME_RENDERER_EXTENSIONS_BINDINGS_UTILS_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_BINDINGS_UTILS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/memory/linked_ptr.h" | 9 #include "base/memory/linked_ptr.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
11 #include "base/string_piece.h" | |
12 #include "ui/base/resource/resource_bundle.h" | |
13 #include "v8/include/v8.h" | 11 #include "v8/include/v8.h" |
14 | 12 |
15 #include <list> | 13 #include <list> |
| 14 #include <map> |
16 #include <string> | 15 #include <string> |
17 | 16 |
18 class Extension; | |
19 class ExtensionDispatcher; | |
20 class RenderView; | 17 class RenderView; |
21 | 18 |
22 namespace WebKit { | 19 namespace WebKit { |
23 class WebFrame; | 20 class WebFrame; |
24 } | 21 } |
25 | 22 |
26 namespace bindings_utils { | 23 namespace bindings_utils { |
27 | 24 |
28 // This is a base class for chrome extension bindings. Common features that | |
29 // are shared by different modules go here. | |
30 class ExtensionBase : public v8::Extension { | |
31 public: | |
32 ExtensionBase(const char* name, | |
33 const char* source, | |
34 int dep_count, | |
35 const char** deps, | |
36 ExtensionDispatcher* extension_dispatcher) | |
37 : v8::Extension(name, source, dep_count, deps), | |
38 extension_dispatcher_(extension_dispatcher) { | |
39 } | |
40 | |
41 // Derived classes should call this at the end of their implementation in | |
42 // order to expose common native functions, like GetChromeHidden, to the | |
43 // v8 extension. | |
44 virtual v8::Handle<v8::FunctionTemplate> | |
45 GetNativeFunction(v8::Handle<v8::String> name); | |
46 | |
47 // TODO(jstritar): Used for testing http://crbug.com/91582. Remove when done. | |
48 ExtensionDispatcher* extension_dispatcher() { return extension_dispatcher_; } | |
49 | |
50 protected: | |
51 template<class T> | |
52 static T* GetFromArguments(const v8::Arguments& args) { | |
53 CHECK(!args.Data().IsEmpty()); | |
54 T* result = static_cast<T*>(args.Data().As<v8::External>()->Value()); | |
55 return result; | |
56 } | |
57 | |
58 // Note: do not call this function before or during the chromeHidden.onLoad | |
59 // event dispatch. The URL might not have been committed yet and might not | |
60 // be an extension URL. | |
61 const ::Extension* GetExtensionForCurrentContext() const; | |
62 | |
63 // Checks that the current context contains an extension that has permission | |
64 // to execute the specified function. If it does not, a v8 exception is thrown | |
65 // and the method returns false. Otherwise returns true. | |
66 bool CheckPermissionForCurrentContext(const std::string& function_name) const; | |
67 | |
68 // Returns a hidden variable for use by the bindings that is unreachable | |
69 // by the page. | |
70 static v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args); | |
71 | |
72 ExtensionDispatcher* extension_dispatcher_; | |
73 | |
74 private: | |
75 // Helper to print from bindings javascript. | |
76 static v8::Handle<v8::Value> Print(const v8::Arguments& args); | |
77 }; | |
78 | |
79 const char* GetStringResource(int resource_id); | |
80 | |
81 // Contains information about a JavaScript context that is hosting extension | 25 // Contains information about a JavaScript context that is hosting extension |
82 // bindings. | 26 // bindings. |
83 struct ContextInfo { | 27 struct ContextInfo { |
84 ContextInfo(v8::Persistent<v8::Context> context, | 28 ContextInfo(v8::Persistent<v8::Context> context, |
85 const std::string& extension_id, | 29 const std::string& extension_id, |
86 WebKit::WebFrame* frame); | 30 WebKit::WebFrame* frame); |
87 ~ContextInfo(); | 31 ~ContextInfo(); |
88 | 32 |
89 v8::Persistent<v8::Context> context; | 33 v8::Persistent<v8::Context> context; |
90 | 34 |
(...skipping 15 matching lines...) Expand all Loading... |
106 // Calling into javascript may result in the list being modified, so don't rely | 50 // Calling into javascript may result in the list being modified, so don't rely |
107 // on iterators remaining valid between calls to javascript. | 51 // on iterators remaining valid between calls to javascript. |
108 ContextList& GetContexts(); | 52 ContextList& GetContexts(); |
109 | 53 |
110 // Returns the ContextInfo item that has the given context. | 54 // Returns the ContextInfo item that has the given context. |
111 ContextList::iterator FindContext(v8::Handle<v8::Context> context); | 55 ContextList::iterator FindContext(v8::Handle<v8::Context> context); |
112 | 56 |
113 // Returns the ContextInfo for the current v8 context. | 57 // Returns the ContextInfo for the current v8 context. |
114 ContextInfo* GetInfoForCurrentContext(); | 58 ContextInfo* GetInfoForCurrentContext(); |
115 | 59 |
| 60 // Returns the 'chromeHidden' object for the specified context. |
| 61 v8::Handle<v8::Object> GetChromeHiddenForContext( |
| 62 v8::Handle<v8::Context> context); |
| 63 |
116 // Contains info relevant to a pending API request. | 64 // Contains info relevant to a pending API request. |
117 struct PendingRequest { | 65 struct PendingRequest { |
118 public : | 66 public : |
119 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name); | 67 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name); |
120 ~PendingRequest(); | 68 ~PendingRequest(); |
121 | 69 |
122 v8::Persistent<v8::Context> context; | 70 v8::Persistent<v8::Context> context; |
123 std::string name; | 71 std::string name; |
124 }; | 72 }; |
125 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap; | 73 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap; |
(...skipping 10 matching lines...) Expand all Loading... |
136 // be a sub-property like "Port.dispatchOnMessage". Returns the result of | 84 // be a sub-property like "Port.dispatchOnMessage". Returns the result of |
137 // the function call. If an exception is thrown an empty Handle will be | 85 // the function call. If an exception is thrown an empty Handle will be |
138 // returned. | 86 // returned. |
139 v8::Handle<v8::Value> CallFunctionInContext(v8::Handle<v8::Context> context, | 87 v8::Handle<v8::Value> CallFunctionInContext(v8::Handle<v8::Context> context, |
140 const std::string& function_name, int argc, | 88 const std::string& function_name, int argc, |
141 v8::Handle<v8::Value>* argv); | 89 v8::Handle<v8::Value>* argv); |
142 | 90 |
143 } // namespace bindings_utils | 91 } // namespace bindings_utils |
144 | 92 |
145 #endif // CHROME_RENDERER_EXTENSIONS_BINDINGS_UTILS_H_ | 93 #endif // CHROME_RENDERER_EXTENSIONS_BINDINGS_UTILS_H_ |
OLD | NEW |