| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 | 7 |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/linked_ptr.h" |
| 9 #include "base/singleton.h" | 10 #include "base/singleton.h" |
| 10 #include "base/string_piece.h" | 11 #include "base/string_piece.h" |
| 11 #include "v8/include/v8.h" | 12 #include "v8/include/v8.h" |
| 12 | 13 |
| 14 #include <list> |
| 13 #include <string> | 15 #include <string> |
| 14 | 16 |
| 15 class RenderView; | 17 class RenderView; |
| 18 class WebFrame; |
| 19 |
| 20 namespace bindings_utils { |
| 21 |
| 22 // This is a base class for chrome extension bindings. Common features that |
| 23 // are shared by different modules go here. |
| 24 class ExtensionBase : public v8::Extension { |
| 25 public: |
| 26 ExtensionBase(const char* name, |
| 27 const char* source, |
| 28 int dep_count, |
| 29 const char** deps) |
| 30 : v8::Extension(name, source, dep_count, deps) {} |
| 31 |
| 32 // Derived classes should call this at the end of their implementation in |
| 33 // order to expose common native functions, like GetChromeHidden, to the |
| 34 // v8 extension. |
| 35 virtual v8::Handle<v8::FunctionTemplate> |
| 36 GetNativeFunction(v8::Handle<v8::String> name); |
| 37 |
| 38 protected: |
| 39 // Returns a hidden variable for use by the bindings that is unreachable |
| 40 // by the page. |
| 41 static v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args); |
| 42 |
| 43 // Starts an API request to the browser, with an optional callback. The |
| 44 // callback will be dispatched to EventBindings::HandleResponse. |
| 45 static v8::Handle<v8::Value> StartRequest(const v8::Arguments& args); |
| 46 }; |
| 16 | 47 |
| 17 template<int kResourceId> | 48 template<int kResourceId> |
| 18 struct StringResourceTemplate { | 49 struct StringResourceTemplate { |
| 19 StringResourceTemplate() | 50 StringResourceTemplate() |
| 20 : resource(ResourceBundle::GetSharedInstance().GetRawDataResource( | 51 : resource(ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 21 kResourceId).as_string()) { | 52 kResourceId).as_string()) { |
| 22 } | 53 } |
| 23 std::string resource; | 54 std::string resource; |
| 24 }; | 55 }; |
| 25 | 56 |
| 26 template<int kResourceId> | 57 template<int kResourceId> |
| 27 const char* GetStringResource() { | 58 const char* GetStringResource() { |
| 28 return | 59 return |
| 29 Singleton< StringResourceTemplate<kResourceId> >::get()->resource.c_str(); | 60 Singleton< StringResourceTemplate<kResourceId> >::get()->resource.c_str(); |
| 30 } | 61 } |
| 31 | 62 |
| 63 // Contains information about a single Javascript context. |
| 64 struct ContextInfo { |
| 65 v8::Persistent<v8::Context> context; |
| 66 std::string extension_id; // empty if the context is not an extension |
| 67 |
| 68 ContextInfo(v8::Persistent<v8::Context> context, |
| 69 const std::string& extension_id) |
| 70 : context(context), extension_id(extension_id) {} |
| 71 }; |
| 72 typedef std::list< linked_ptr<ContextInfo> > ContextList; |
| 73 |
| 74 // Returns a mutable reference to the ContextList. |
| 75 ContextList& GetContexts(); |
| 76 |
| 77 // Returns a (copied) list of contexts that have the given extension_id. |
| 78 ContextList GetContextsForExtension(const std::string& extension_id); |
| 79 |
| 80 // Returns the ContextInfo item that has the given context. |
| 81 ContextList::iterator FindContext(v8::Handle<v8::Context> context); |
| 82 |
| 83 // Contains info relevant to a pending API request. |
| 84 struct PendingRequest { |
| 85 public : |
| 86 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name) |
| 87 : context(context), name(name) { |
| 88 } |
| 89 v8::Persistent<v8::Context> context; |
| 90 std::string name; |
| 91 }; |
| 92 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap; |
| 93 |
| 94 // Returns a mutable reference to the PendingRequestMap. |
| 95 PendingRequestMap& GetPendingRequestMap(); |
| 96 |
| 32 // Returns the current RenderView, based on which V8 context is current. It is | 97 // Returns the current RenderView, based on which V8 context is current. It is |
| 33 // an error to call this when not in a V8 context. | 98 // an error to call this when not in a V8 context. |
| 34 RenderView* GetRenderViewForCurrentContext(); | 99 RenderView* GetRenderViewForCurrentContext(); |
| 35 | 100 |
| 36 // Call the named javascript function with the given arguments in a context. | 101 // Call the named javascript function with the given arguments in a context. |
| 37 // The function name should be reachable from the global object, and can be a | 102 // The function name should be reachable from the chromeHidden object, and can |
| 38 // sub-property like "chrome.handleResponse_". | 103 // be a sub-property like "Port.dispatchOnMessage". |
| 39 void CallFunctionInContext(v8::Handle<v8::Context> context, | 104 void CallFunctionInContext(v8::Handle<v8::Context> context, |
| 40 const std::string& function_name, int argc, | 105 const std::string& function_name, int argc, |
| 41 v8::Handle<v8::Value>* argv); | 106 v8::Handle<v8::Value>* argv); |
| 42 | 107 |
| 108 } // namespace bindings_utils |
| 109 |
| 43 #endif // CHROME_RENDERER_EXTENSIONS_BINDINGS_UTILS_H_ | 110 #endif // CHROME_RENDERER_EXTENSIONS_BINDINGS_UTILS_H_ |
| OLD | NEW |