OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef WEBKIT_PLUGINS_PPAPI_NPAPI_GLUE_H_ |
| 6 #define WEBKIT_PLUGINS_PPAPI_NPAPI_GLUE_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/scoped_ptr.h" |
| 10 #include "ppapi/c/pp_var.h" |
| 11 |
| 12 struct NPObject; |
| 13 typedef struct _NPVariant NPVariant; |
| 14 typedef void* NPIdentifier; |
| 15 |
| 16 namespace webkit { |
| 17 namespace plugins { |
| 18 namespace ppapi { |
| 19 |
| 20 class PluginModule; |
| 21 class PluginObject; |
| 22 |
| 23 // Utilities ------------------------------------------------------------------- |
| 24 |
| 25 // Converts the given PP_Var to an NPVariant, returning true on success. |
| 26 // False means that the given variant is invalid. In this case, the result |
| 27 // NPVariant will be set to a void one. |
| 28 // |
| 29 // The contents of the PP_Var will be copied unless the PP_Var corresponds to |
| 30 // an object. |
| 31 bool PPVarToNPVariant(PP_Var var, NPVariant* result); |
| 32 |
| 33 // PPResultAndExceptionToNPResult ---------------------------------------------- |
| 34 |
| 35 // Convenience object for converting a PPAPI call that can throw an exception |
| 36 // and optionally return a value, back to the NPAPI layer which expects a |
| 37 // NPVariant as a result. |
| 38 // |
| 39 // Normal usage is that you will pass the result of exception() to the |
| 40 // PPAPI function as the exception output parameter. Then you will either |
| 41 // call SetResult with the result of the PPAPI call, or |
| 42 // CheckExceptionForNoResult if the PPAPI call doesn't return a PP_Var. |
| 43 // |
| 44 // Both SetResult and CheckExceptionForNoResult will throw an exception to |
| 45 // the JavaScript library if the plugin reported an exception. SetResult |
| 46 // will additionally convert the result to an NPVariant and write it to the |
| 47 // output parameter given in the constructor. |
| 48 class PPResultAndExceptionToNPResult { |
| 49 public: |
| 50 // The object_var parameter is the object to associate any exception with. |
| 51 // It may not be NULL. |
| 52 // |
| 53 // The np_result parameter is the NPAPI result output parameter. This may be |
| 54 // NULL if there is no NPVariant result (like for HasProperty). If this is |
| 55 // specified, you must call SetResult() to set it. If it is not, you must |
| 56 // call CheckExceptionForNoResult to do the exception checking with no result |
| 57 // conversion. |
| 58 PPResultAndExceptionToNPResult(NPObject* object_var, NPVariant* np_result); |
| 59 |
| 60 ~PPResultAndExceptionToNPResult(); |
| 61 |
| 62 // Returns true if an exception has been set. |
| 63 bool has_exception() const { return exception_.type != PP_VARTYPE_UNDEFINED; } |
| 64 |
| 65 // Returns a pointer to the exception. You would pass this to the PPAPI |
| 66 // function as the exception parameter. If it is set to non-void, this object |
| 67 // will take ownership of destroying it. |
| 68 PP_Var* exception() { return &exception_; } |
| 69 |
| 70 // Returns true if everything succeeded with no exception. This is valid only |
| 71 // after calling SetResult/CheckExceptionForNoResult. |
| 72 bool success() const { |
| 73 return success_; |
| 74 } |
| 75 |
| 76 // Call this with the return value of the PPAPI function. It will convert |
| 77 // the result to the NPVariant output parameter and pass any exception on to |
| 78 // the JS engine. It will update the success flag and return it. |
| 79 bool SetResult(PP_Var result); |
| 80 |
| 81 // Call this after calling a PPAPI function that could have set the |
| 82 // exception. It will pass the exception on to the JS engine and update |
| 83 // the success flag. |
| 84 // |
| 85 // The success flag will be returned. |
| 86 bool CheckExceptionForNoResult(); |
| 87 |
| 88 // Call this to ignore any exception. This prevents the DCHECK from failing |
| 89 // in the destructor. |
| 90 void IgnoreException(); |
| 91 |
| 92 private: |
| 93 // Throws the current exception to JS. The exception must be set. |
| 94 void ThrowException(); |
| 95 |
| 96 NPObject* object_var_; // Non-owning ref (see constructor). |
| 97 NPVariant* np_result_; // Output value, possibly NULL (see constructor). |
| 98 PP_Var exception_; // Exception set by the PPAPI call. We own a ref to it. |
| 99 bool success_; // See the success() function above. |
| 100 bool checked_exception_; // SetResult/CheckExceptionForNoResult was called. |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(PPResultAndExceptionToNPResult); |
| 103 }; |
| 104 |
| 105 // PPVarArrayFromNPVariantArray ------------------------------------------------ |
| 106 |
| 107 // Converts an array of NPVariants to an array of PP_Var, and scopes the |
| 108 // ownership of the PP_Var. This is used when converting argument lists from |
| 109 // WebKit to the plugin. |
| 110 class PPVarArrayFromNPVariantArray { |
| 111 public: |
| 112 PPVarArrayFromNPVariantArray(PluginModule* module, |
| 113 size_t size, |
| 114 const NPVariant* variants); |
| 115 ~PPVarArrayFromNPVariantArray(); |
| 116 |
| 117 PP_Var* array() { return array_.get(); } |
| 118 |
| 119 private: |
| 120 size_t size_; |
| 121 scoped_array<PP_Var> array_; |
| 122 |
| 123 DISALLOW_COPY_AND_ASSIGN(PPVarArrayFromNPVariantArray); |
| 124 }; |
| 125 |
| 126 // PPVarFromNPObject ----------------------------------------------------------- |
| 127 |
| 128 // Converts an NPObject tp PP_Var, and scopes the ownership of the PP_Var. This |
| 129 // is used when converting 'this' pointer from WebKit to the plugin. |
| 130 class PPVarFromNPObject { |
| 131 public: |
| 132 PPVarFromNPObject(PluginModule* module, NPObject* object); |
| 133 ~PPVarFromNPObject(); |
| 134 |
| 135 PP_Var var() const { return var_; } |
| 136 |
| 137 private: |
| 138 const PP_Var var_; |
| 139 |
| 140 DISALLOW_COPY_AND_ASSIGN(PPVarFromNPObject); |
| 141 }; |
| 142 |
| 143 // NPObjectAccessorWithIdentifier ---------------------------------------------- |
| 144 |
| 145 // Helper class for our NPObject wrapper. This converts a call from WebKit |
| 146 // where it gives us an NPObject and an NPIdentifier to an easily-accessible |
| 147 // ObjectVar (corresponding to the NPObject) and PP_Var (corresponding to the |
| 148 // NPIdentifier). |
| 149 // |
| 150 // If the NPObject or identifier is invalid, we'll set is_valid() to false. |
| 151 // The caller should check is_valid() before doing anything with the class. |
| 152 // |
| 153 // JS can't have integer functions, so when dealing with these, we don't want |
| 154 // to allow integer identifiers. The calling code can decode if it wants to |
| 155 // allow integer identifiers (like for property access) or prohibit them |
| 156 // (like for method calling) by setting |allow_integer_identifier|. If this |
| 157 // is false and the identifier is an integer, we'll set is_valid() to false. |
| 158 // |
| 159 // Getting an integer identifier in this case should be impossible. V8 |
| 160 // shouldn't be allowing this, and the Pepper Var calls from the plugin are |
| 161 // supposed to error out before calling into V8 (which will then call us back). |
| 162 // Aside from an egregious error, the only time this could happen is an NPAPI |
| 163 // plugin calling us. |
| 164 class NPObjectAccessorWithIdentifier { |
| 165 public: |
| 166 NPObjectAccessorWithIdentifier(NPObject* object, |
| 167 NPIdentifier identifier, |
| 168 bool allow_integer_identifier); |
| 169 ~NPObjectAccessorWithIdentifier(); |
| 170 |
| 171 // Returns true if both the object and identifier are valid. |
| 172 bool is_valid() const { |
| 173 return object_ && identifier_.type != PP_VARTYPE_UNDEFINED; |
| 174 } |
| 175 |
| 176 PluginObject* object() { return object_; } |
| 177 PP_Var identifier() const { return identifier_; } |
| 178 |
| 179 private: |
| 180 PluginObject* object_; |
| 181 PP_Var identifier_; |
| 182 |
| 183 DISALLOW_COPY_AND_ASSIGN(NPObjectAccessorWithIdentifier); |
| 184 }; |
| 185 |
| 186 } // namespace ppapi |
| 187 } // namespace plugins |
| 188 } // namespace webkit |
| 189 |
| 190 #endif // WEBKIT_PLUGINS_PPAPI_NPAPI_GLUE_H_ |
OLD | NEW |