| 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 #ifndef CEEE_COMMON_NP_BROWSER_FUNCTIONS_H_ | |
| 5 #define CEEE_COMMON_NP_BROWSER_FUNCTIONS_H_ | |
| 6 | |
| 7 #include <string> | |
| 8 #include "base/logging.h" | |
| 9 #include "third_party/npapi/bindings/nphostapi.h" | |
| 10 | |
| 11 namespace npapi { | |
| 12 | |
| 13 // Must be called prior to calling any of the browser functions below. | |
| 14 void InitializeBrowserFunctions(NPNetscapeFuncs* functions); | |
| 15 void UninitializeBrowserFunctions(); | |
| 16 | |
| 17 // Returns true iff InitializeBrowserFunctions has been called successully. | |
| 18 bool IsInitialized(); | |
| 19 | |
| 20 // Function stubs for functions that the host browser implements. | |
| 21 | |
| 22 NPError GetURL(NPP instance, const char* URL, const char* window); | |
| 23 | |
| 24 NPError PostURL(NPP instance, const char* URL, const char* window, uint32 len, | |
| 25 const char* buf, NPBool file); | |
| 26 | |
| 27 NPError RequestRead(NPStream* stream, NPByteRange* rangeList); | |
| 28 | |
| 29 NPError NewStream(NPP instance, NPMIMEType type, const char* window, | |
| 30 NPStream** stream); | |
| 31 | |
| 32 int32 Write(NPP instance, NPStream* stream, int32 len, void* buffer); | |
| 33 | |
| 34 NPError DestroyStream(NPP instance, NPStream* stream, NPReason reason); | |
| 35 | |
| 36 void Status(NPP instance, const char* message); | |
| 37 | |
| 38 const char* UserAgent(NPP instance); | |
| 39 | |
| 40 void* MemAlloc(uint32 size); | |
| 41 | |
| 42 void MemFree(void* ptr); | |
| 43 | |
| 44 uint32 MemFlush(uint32 size); | |
| 45 | |
| 46 void ReloadPlugins(NPBool reloadPages); | |
| 47 | |
| 48 void* GetJavaEnv(); | |
| 49 | |
| 50 void* GetJavaPeer(NPP instance); | |
| 51 | |
| 52 NPError GetURLNotify(NPP instance, const char* URL, const char* window, | |
| 53 void* notifyData); | |
| 54 | |
| 55 NPError PostURLNotify(NPP instance, const char* URL, const char* window, | |
| 56 uint32 len, const char* buf, NPBool file, | |
| 57 void* notifyData); | |
| 58 | |
| 59 NPError GetValue(NPP instance, NPNVariable variable, void* ret_value); | |
| 60 | |
| 61 NPError SetValue(NPP instance, NPPVariable variable, void* value); | |
| 62 | |
| 63 void InvalidateRect(NPP instance, NPRect* rect); | |
| 64 | |
| 65 void InvalidateRegion(NPP instance, NPRegion region); | |
| 66 | |
| 67 void ForceRedraw(NPP instance); | |
| 68 | |
| 69 void ReleaseVariantValue(NPVariant* variant); | |
| 70 | |
| 71 NPIdentifier GetStringIdentifier(const NPUTF8* name); | |
| 72 | |
| 73 void GetStringIdentifiers(const NPUTF8** names, int32_t nameCount, | |
| 74 NPIdentifier* identifiers); | |
| 75 | |
| 76 NPIdentifier GetIntIdentifier(int32_t intid); | |
| 77 | |
| 78 int32_t IntFromIdentifier(NPIdentifier identifier); | |
| 79 | |
| 80 bool IdentifierIsString(NPIdentifier identifier); | |
| 81 | |
| 82 NPUTF8* UTF8FromIdentifier(NPIdentifier identifier); | |
| 83 | |
| 84 NPObject* CreateObject(NPP, NPClass* aClass); | |
| 85 | |
| 86 NPObject* RetainObject(NPObject* obj); | |
| 87 | |
| 88 void ReleaseObject(NPObject* obj); | |
| 89 | |
| 90 bool Invoke(NPP npp, NPObject* obj, NPIdentifier methodName, | |
| 91 const NPVariant* args, unsigned argCount, NPVariant* result); | |
| 92 | |
| 93 bool InvokeDefault(NPP npp, NPObject* obj, const NPVariant* args, | |
| 94 unsigned argCount, NPVariant* result); | |
| 95 | |
| 96 bool Evaluate(NPP npp, NPObject* obj, NPString* script, NPVariant* result); | |
| 97 | |
| 98 bool GetProperty(NPP npp, NPObject* obj, NPIdentifier propertyName, | |
| 99 NPVariant* result); | |
| 100 | |
| 101 bool SetProperty(NPP npp, NPObject* obj, NPIdentifier propertyName, | |
| 102 const NPVariant* value); | |
| 103 | |
| 104 bool HasProperty(NPP npp, NPObject* npobj, NPIdentifier propertyName); | |
| 105 | |
| 106 bool HasMethod(NPP npp, NPObject* npobj, NPIdentifier methodName); | |
| 107 | |
| 108 bool RemoveProperty(NPP npp, NPObject* obj, NPIdentifier propertyName); | |
| 109 | |
| 110 void SetException(NPObject* obj, const NPUTF8* message); | |
| 111 | |
| 112 void PushPopupsEnabledState(NPP npp, NPBool enabled); | |
| 113 | |
| 114 void PopPopupsEnabledState(NPP npp); | |
| 115 | |
| 116 bool Enumerate(NPP npp, NPObject* obj, NPIdentifier** identifier, | |
| 117 uint32_t* count); | |
| 118 | |
| 119 void PluginThreadAsyncCall(NPP instance, | |
| 120 void (*func)(void*), // NOLINT | |
| 121 void* userData); | |
| 122 | |
| 123 bool Construct(NPP npp, NPObject* obj, const NPVariant* args, uint32_t argCount, | |
| 124 NPVariant* result); | |
| 125 | |
| 126 // Helper routine that wraps UTF8FromIdentifier to convert a string identifier | |
| 127 // to an STL string. It's not super efficient since it could possibly do two | |
| 128 // heap allocations (STL string has a stack based buffer for smaller strings). | |
| 129 // For debugging purposes it is useful. | |
| 130 std::string StringFromIdentifier(NPIdentifier identifier); | |
| 131 | |
| 132 } // namespace npapi | |
| 133 | |
| 134 // Simple helper class for freeing NPVariants at the end of a scope. | |
| 135 class ScopedNpVariant : public NPVariant { | |
| 136 public: | |
| 137 ScopedNpVariant() { | |
| 138 VOID_TO_NPVARIANT(*this); | |
| 139 } | |
| 140 | |
| 141 ~ScopedNpVariant() { | |
| 142 Free(); | |
| 143 } | |
| 144 | |
| 145 void Free() { | |
| 146 npapi::ReleaseVariantValue(this); | |
| 147 VOID_TO_NPVARIANT(*this); | |
| 148 } | |
| 149 | |
| 150 private: | |
| 151 DISALLOW_COPY_AND_ASSIGN(ScopedNpVariant); | |
| 152 }; | |
| 153 | |
| 154 // Simple helper class for freeing NPObjects at the end of a scope. | |
| 155 template <typename NpoType = NPObject> | |
| 156 class ScopedNpObject { | |
| 157 public: | |
| 158 ScopedNpObject() : npo_(NULL) { | |
| 159 } | |
| 160 | |
| 161 explicit ScopedNpObject(NpoType* npo) : npo_(npo) { | |
| 162 } | |
| 163 | |
| 164 ~ScopedNpObject() { | |
| 165 Free(); | |
| 166 } | |
| 167 | |
| 168 NpoType* get() const { | |
| 169 return npo_; | |
| 170 } | |
| 171 | |
| 172 operator NpoType*() const { | |
| 173 return npo_; | |
| 174 } | |
| 175 | |
| 176 NpoType* operator->() const { | |
| 177 return npo_; | |
| 178 } | |
| 179 | |
| 180 ScopedNpObject<NpoType>& operator=(NpoType* npo) { | |
| 181 if (npo != npo_) { | |
| 182 DCHECK(npo_ == NULL); | |
| 183 npapi::RetainObject(npo); | |
| 184 npo_ = npo; | |
| 185 } | |
| 186 return *this; | |
| 187 } | |
| 188 | |
| 189 void Free() { | |
| 190 if (npo_) { | |
| 191 npapi::ReleaseObject(npo_); | |
| 192 npo_ = NULL; | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 NpoType** Receive() { | |
| 197 DCHECK(npo_ == NULL) << "Object leak. Pointer must be NULL"; | |
| 198 return &npo_; | |
| 199 } | |
| 200 | |
| 201 NpoType* Detach() { | |
| 202 NpoType* p = npo_; | |
| 203 npo_ = NULL; | |
| 204 return p; | |
| 205 } | |
| 206 | |
| 207 void Attach(NpoType* p) { | |
| 208 DCHECK(npo_ == NULL); | |
| 209 npo_ = p; | |
| 210 } | |
| 211 | |
| 212 NpoType* Copy() const { | |
| 213 if (npo_ != NULL) | |
| 214 npapi::RetainObject(npo_); | |
| 215 return npo_; | |
| 216 } | |
| 217 | |
| 218 bool Invoke0(NPP npp, NPIdentifier id, NPVariant* result) { | |
| 219 return npapi::Invoke(npp, npo_, id, NULL, 0, result); | |
| 220 } | |
| 221 bool Invoke0(NPP npp, const NPUTF8* name, NPVariant* result) { | |
| 222 return Invoke0(npp, npapi::GetStringIdentifier(name), result); | |
| 223 } | |
| 224 | |
| 225 bool Invoke1(NPP npp, NPIdentifier id, const NPVariant &arg1, | |
| 226 NPVariant* result) { | |
| 227 return npapi::Invoke(npp, npo_, id, &arg1, 1, result); | |
| 228 } | |
| 229 bool Invoke1(NPP npp, const NPUTF8* name, const NPVariant &arg1, | |
| 230 NPVariant* result) { | |
| 231 return Invoke1(npp, npapi::GetStringIdentifier(name), arg1, result); | |
| 232 } | |
| 233 bool InvokeN(NPP npp, NPIdentifier id, const NPVariant* args, unsigned argc, | |
| 234 NPVariant* result) { | |
| 235 return npapi::Invoke(npp, npo_, id, args, argc, result); | |
| 236 } | |
| 237 bool InvokeN(NPP npp, const NPUTF8* name, const NPVariant* args, | |
| 238 unsigned argc, NPVariant* result) { | |
| 239 return Invoke1(npp, npapi::GetStringIdentifier(name), args, argc, result); | |
| 240 } | |
| 241 | |
| 242 bool GetProperty(NPP npp, NPIdentifier id, NPVariant* result) { | |
| 243 return npapi::GetProperty(npp, npo_, id, result); | |
| 244 } | |
| 245 | |
| 246 bool GetProperty(NPP npp, const NPUTF8* name, NPVariant* result) { | |
| 247 return GetProperty(npp, npapi::GetStringIdentifier(name), result); | |
| 248 } | |
| 249 | |
| 250 private: | |
| 251 NpoType* npo_; | |
| 252 DISALLOW_COPY_AND_ASSIGN(ScopedNpObject); | |
| 253 }; | |
| 254 | |
| 255 // Allocates a new NPUTF8 string and assigns it to the variant. | |
| 256 // If memory allocation fails, the variant type will be set to NULL. | |
| 257 // The memory allocation is done via the npapi browser functions. | |
| 258 void AllocateStringVariant(const std::string& str, NPVariant* var); | |
| 259 | |
| 260 #endif // CEEE_COMMON_NP_BROWSER_FUNCTIONS_H_ | |
| OLD | NEW |