| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 /* | 5 /* |
| 6 CppBoundClass class: | 6 CppBoundClass class: |
| 7 This base class serves as a parent for C++ classes designed to be bound to | 7 This base class serves as a parent for C++ classes designed to be bound to |
| 8 JavaScript objects. | 8 JavaScript objects. |
| 9 | 9 |
| 10 Subclasses should define the constructor to build the property and method | 10 Subclasses should define the constructor to build the property and method |
| 11 lists needed to bind this class to a JS object. They should also declare | 11 lists needed to bind this class to a JS object. They should also declare |
| 12 and define member variables and methods to be exposed to JS through | 12 and define member variables and methods to be exposed to JS through |
| 13 that object. | 13 that object. |
| 14 | 14 |
| 15 See cpp_binding_example.{h|cc} for an example. | 15 See cpp_binding_example.{h|cc} for an example. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #ifndef WEBKIT_GLUE_CPP_BOUNDCLASS_H__ | 18 #ifndef WEBKIT_GLUE_CPP_BOUNDCLASS_H__ |
| 19 #define WEBKIT_GLUE_CPP_BOUNDCLASS_H__ | 19 #define WEBKIT_GLUE_CPP_BOUNDCLASS_H__ |
| 20 | 20 |
| 21 #include <map> | 21 #include <map> |
| 22 #include <vector> | 22 #include <vector> |
| 23 | 23 |
| 24 #include "webkit/glue/cpp_variant.h" | 24 #include "webkit/glue/cpp_variant.h" |
| 25 | 25 |
| 26 #include "base/scoped_ptr.h" | 26 #include "base/scoped_ptr.h" |
| 27 #include "base/task.h" | 27 #include "base/task.h" |
| 28 | 28 |
| 29 namespace WebKit { |
| 29 class WebFrame; | 30 class WebFrame; |
| 31 } |
| 30 | 32 |
| 31 typedef std::vector<CppVariant> CppArgumentList; | 33 typedef std::vector<CppVariant> CppArgumentList; |
| 32 | 34 |
| 33 // CppBoundClass lets you map Javascript method calls and property accesses | 35 // CppBoundClass lets you map Javascript method calls and property accesses |
| 34 // directly to C++ method calls and CppVariant* variable access. | 36 // directly to C++ method calls and CppVariant* variable access. |
| 35 class CppBoundClass { | 37 class CppBoundClass { |
| 36 public: | 38 public: |
| 37 // The constructor should call BindMethod, BindProperty, and | 39 // The constructor should call BindMethod, BindProperty, and |
| 38 // SetFallbackMethod as needed to set up the methods, properties, and | 40 // SetFallbackMethod as needed to set up the methods, properties, and |
| 39 // fallback method. | 41 // fallback method. |
| 40 CppBoundClass() : bound_to_frame_(false) { } | 42 CppBoundClass() : bound_to_frame_(false) { } |
| 41 virtual ~CppBoundClass(); | 43 virtual ~CppBoundClass(); |
| 42 | 44 |
| 43 // Return a CppVariant representing this class, for use with BindProperty(). | 45 // Return a CppVariant representing this class, for use with BindProperty(). |
| 44 // The variant type is guaranteed to be NPVariantType_Object. | 46 // The variant type is guaranteed to be NPVariantType_Object. |
| 45 CppVariant* GetAsCppVariant(); | 47 CppVariant* GetAsCppVariant(); |
| 46 | 48 |
| 47 // Given a WebFrame, BindToJavascript builds the NPObject that will represent | 49 // Given a WebFrame, BindToJavascript builds the NPObject that will represent |
| 48 // the class and binds it to the frame's window under the given name. This | 50 // the class and binds it to the frame's window under the given name. This |
| 49 // should generally be called from the WebView delegate's | 51 // should generally be called from the WebView delegate's |
| 50 // WindowObjectCleared(). A class so bound will be accessible to JavaScript | 52 // WindowObjectCleared(). A class so bound will be accessible to JavaScript |
| 51 // as window.<classname>. The owner of the CppBoundObject is responsible for | 53 // as window.<classname>. The owner of the CppBoundObject is responsible for |
| 52 // keeping the object around while the frame is alive, and for destroying it | 54 // keeping the object around while the frame is alive, and for destroying it |
| 53 // afterwards. | 55 // afterwards. |
| 54 void BindToJavascript(WebFrame* frame, const std::wstring& classname); | 56 void BindToJavascript( |
| 57 WebKit::WebFrame* frame, const std::wstring& classname); |
| 55 | 58 |
| 56 // The type of callbacks. | 59 // The type of callbacks. |
| 57 typedef Callback2<const CppArgumentList&, CppVariant*>::Type Callback; | 60 typedef Callback2<const CppArgumentList&, CppVariant*>::Type Callback; |
| 58 | 61 |
| 59 // Used by a test. Returns true if a method with name |name| exists, | 62 // Used by a test. Returns true if a method with name |name| exists, |
| 60 // regardless of whether a fallback is registered. | 63 // regardless of whether a fallback is registered. |
| 61 bool IsMethodRegistered(std::string name) const; | 64 bool IsMethodRegistered(std::string name) const; |
| 62 | 65 |
| 63 protected: | 66 protected: |
| 64 // Bind the Javascript method called |name| to the C++ callback |callback|. | 67 // Bind the Javascript method called |name| to the C++ callback |callback|. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 CppVariant self_variant_; | 141 CppVariant self_variant_; |
| 139 | 142 |
| 140 // True if our np_object has been bound to a WebFrame, in which case it must | 143 // True if our np_object has been bound to a WebFrame, in which case it must |
| 141 // be unregistered with V8 when we delete it. | 144 // be unregistered with V8 when we delete it. |
| 142 bool bound_to_frame_; | 145 bool bound_to_frame_; |
| 143 | 146 |
| 144 DISALLOW_EVIL_CONSTRUCTORS(CppBoundClass); | 147 DISALLOW_EVIL_CONSTRUCTORS(CppBoundClass); |
| 145 }; | 148 }; |
| 146 | 149 |
| 147 #endif // CPP_BOUNDCLASS_H__ | 150 #endif // CPP_BOUNDCLASS_H__ |
| OLD | NEW |