Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "content/renderer/browser_plugin/browser_plugin_bindings.h" | 5 #include "content/renderer/browser_plugin/browser_plugin_bindings.h" |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 using WebKit::WebDOMEvent; | 30 using WebKit::WebDOMEvent; |
| 31 using WebKit::WebDOMMessageEvent; | 31 using WebKit::WebDOMMessageEvent; |
| 32 using WebKit::WebPluginContainer; | 32 using WebKit::WebPluginContainer; |
| 33 using WebKit::WebSerializedScriptValue; | 33 using WebKit::WebSerializedScriptValue; |
| 34 using WebKit::WebString; | 34 using WebKit::WebString; |
| 35 | 35 |
| 36 namespace content { | 36 namespace content { |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 const char kAddEventListener[] = "addEventListener"; | |
| 41 const char kBackMethod[] = "back"; | |
| 42 const char kCanGoBack[] = "canGoBack"; | |
| 43 const char kCanGoForward[] = "canGoForward"; | |
| 44 const char kForwardMethod[] = "forward"; | |
| 45 const char kGetProcessId[] = "getProcessId"; | |
| 46 const char kGoMethod[] = "go"; | |
| 47 const char kPartitionAttribute[] = "partition"; | |
| 48 const char kReloadMethod[] = "reload"; | |
| 49 const char kRemoveEventListener[] = "removeEventListener"; | |
| 50 const char kSrcAttribute[] = "src"; | |
| 51 const char kStopMethod[] = "stop"; | |
| 52 const char kTerminateMethod[] = "terminate"; | |
| 53 | |
| 54 BrowserPluginBindings* GetBindings(NPObject* object) { | |
| 55 return static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object)-> | |
| 56 message_channel; | |
| 57 } | |
| 58 | |
| 59 bool IdentifierIsAddEventListener(NPIdentifier identifier) { | |
| 60 return WebBindings::getStringIdentifier(kAddEventListener) == identifier; | |
| 61 } | |
| 62 | |
| 63 bool IdentifierIsBackMethod(NPIdentifier identifier) { | |
| 64 return WebBindings::getStringIdentifier(kBackMethod) == identifier; | |
| 65 } | |
| 66 | |
| 67 bool IdentifierIsCanGoBack(NPIdentifier identifier) { | |
| 68 return WebBindings::getStringIdentifier(kCanGoBack) == identifier; | |
| 69 } | |
| 70 | |
| 71 bool IdentifierIsCanGoForward(NPIdentifier identifier) { | |
| 72 return WebBindings::getStringIdentifier(kCanGoForward) == identifier; | |
| 73 } | |
| 74 | |
| 75 bool IdentifierIsForwardMethod(NPIdentifier identifier) { | |
| 76 return WebBindings::getStringIdentifier(kForwardMethod) == identifier; | |
| 77 } | |
| 78 | |
| 79 bool IdentifierIsGetProcessID(NPIdentifier identifier) { | |
| 80 return WebBindings::getStringIdentifier(kGetProcessId) == identifier; | |
| 81 } | |
| 82 | |
| 83 bool IdentifierIsGoMethod(NPIdentifier identifier) { | |
| 84 return WebBindings::getStringIdentifier(kGoMethod) == identifier; | |
| 85 } | |
| 86 | |
| 87 bool IdentifierIsPartitionAttribute(NPIdentifier identifier) { | |
| 88 return WebBindings::getStringIdentifier(kPartitionAttribute) == identifier; | |
| 89 } | |
| 90 | |
| 91 bool IdentifierIsReload(NPIdentifier identifier) { | |
| 92 return WebBindings::getStringIdentifier(kReloadMethod) == identifier; | |
| 93 } | |
| 94 | |
| 95 bool IdentifierIsRemoveEventListener(NPIdentifier identifier) { | |
| 96 return WebBindings::getStringIdentifier(kRemoveEventListener) == identifier; | |
| 97 } | |
| 98 | |
| 99 bool IdentifierIsSrcAttribute(NPIdentifier identifier) { | |
| 100 return WebBindings::getStringIdentifier(kSrcAttribute) == identifier; | |
| 101 } | |
| 102 | |
| 103 bool IdentifierIsStop(NPIdentifier identifier) { | |
| 104 return WebBindings::getStringIdentifier(kStopMethod) == identifier; | |
| 105 } | |
| 106 | |
| 107 bool IdentifierIsTerminate(NPIdentifier identifier) { | |
| 108 return WebBindings::getStringIdentifier(kTerminateMethod) == identifier; | |
| 109 } | |
| 110 | |
| 111 int Int32FromNPVariant(const NPVariant& variant) { | 40 int Int32FromNPVariant(const NPVariant& variant) { |
| 112 if (NPVARIANT_IS_INT32(variant)) | 41 if (NPVARIANT_IS_INT32(variant)) |
| 113 return NPVARIANT_TO_INT32(variant); | 42 return NPVARIANT_TO_INT32(variant); |
| 114 | 43 |
| 115 if (NPVARIANT_IS_DOUBLE(variant)) | 44 if (NPVARIANT_IS_DOUBLE(variant)) |
| 116 return NPVARIANT_TO_DOUBLE(variant); | 45 return NPVARIANT_TO_DOUBLE(variant); |
| 117 | 46 |
| 118 return 0; | 47 return 0; |
| 119 } | 48 } |
| 120 | 49 |
| 121 std::string StringFromNPVariant(const NPVariant& variant) { | 50 std::string StringFromNPVariant(const NPVariant& variant) { |
| 122 if (!NPVARIANT_IS_STRING(variant)) | 51 if (!NPVARIANT_IS_STRING(variant)) |
| 123 return std::string(); | 52 return std::string(); |
| 124 const NPString& np_string = NPVARIANT_TO_STRING(variant); | 53 const NPString& np_string = NPVARIANT_TO_STRING(variant); |
| 125 return std::string(np_string.UTF8Characters, np_string.UTF8Length); | 54 return std::string(np_string.UTF8Characters, np_string.UTF8Length); |
| 126 } | 55 } |
| 127 | 56 |
| 128 bool StringToNPVariant(const std::string &in, NPVariant *variant) { | 57 bool StringToNPVariant(const std::string &in, NPVariant *variant) { |
| 129 size_t length = in.size(); | 58 size_t length = in.size(); |
| 130 NPUTF8 *chars = static_cast<NPUTF8 *>(malloc(length)); | 59 NPUTF8 *chars = static_cast<NPUTF8 *>(malloc(length)); |
| 131 if (!chars) { | 60 if (!chars) { |
| 132 VOID_TO_NPVARIANT(*variant); | 61 VOID_TO_NPVARIANT(*variant); |
| 133 return false; | 62 return false; |
| 134 } | 63 } |
| 135 memcpy(chars, in.c_str(), length); | 64 memcpy(chars, in.c_str(), length); |
| 136 STRINGN_TO_NPVARIANT(chars, length, *variant); | 65 STRINGN_TO_NPVARIANT(chars, length, *variant); |
| 137 return true; | 66 return true; |
| 138 } | 67 } |
| 139 | 68 |
| 69 const char kAddEventListener[] = "addEventListener"; | |
| 70 const char kBackMethod[] = "back"; | |
| 71 const char kCanGoBack[] = "canGoBack"; | |
| 72 const char kCanGoForward[] = "canGoForward"; | |
| 73 const char kForwardMethod[] = "forward"; | |
| 74 const char kGetProcessId[] = "getProcessId"; | |
| 75 const char kGoMethod[] = "go"; | |
| 76 const char kPartitionAttribute[] = "partition"; | |
| 77 const char kReloadMethod[] = "reload"; | |
| 78 const char kRemoveEventListener[] = "removeEventListener"; | |
| 79 const char kSrcAttribute[] = "src"; | |
| 80 const char kStopMethod[] = "stop"; | |
| 81 const char kTerminateMethod[] = "terminate"; | |
| 82 | |
| 83 BrowserPluginBindings* GetBindings(NPObject* object) { | |
| 84 return static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object)-> | |
| 85 message_channel; | |
| 86 } | |
| 87 | |
| 88 bool IdentifierIsPartitionAttribute(NPIdentifier identifier) { | |
| 89 return WebBindings::getStringIdentifier(kPartitionAttribute) == identifier; | |
| 90 } | |
| 91 | |
| 92 bool IdentifierIsSrcAttribute(NPIdentifier identifier) { | |
| 93 return WebBindings::getStringIdentifier(kSrcAttribute) == identifier; | |
| 94 } | |
|
Charlie Reis
2012/10/15 22:19:33
nit: Why move these? Nice that a lot of this was
sadrul
2012/10/15 22:37:12
Done.
| |
| 95 | |
| 140 //------------------------------------------------------------------------------ | 96 //------------------------------------------------------------------------------ |
| 141 // Implementations of NPClass functions. These are here to: | 97 // Implementations of NPClass functions. These are here to: |
| 142 // - Implement src attribute. | 98 // - Implement src attribute. |
| 143 //------------------------------------------------------------------------------ | 99 //------------------------------------------------------------------------------ |
| 144 NPObject* BrowserPluginBindingsAllocate(NPP npp, NPClass* the_class) { | 100 NPObject* BrowserPluginBindingsAllocate(NPP npp, NPClass* the_class) { |
| 145 return new BrowserPluginBindings::BrowserPluginNPObject; | 101 return new BrowserPluginBindings::BrowserPluginNPObject; |
| 146 } | 102 } |
| 147 | 103 |
| 148 void BrowserPluginBindingsDeallocate(NPObject* object) { | 104 void BrowserPluginBindingsDeallocate(NPObject* object) { |
| 149 BrowserPluginBindings::BrowserPluginNPObject* instance = | 105 BrowserPluginBindings::BrowserPluginNPObject* instance = |
| 150 static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object); | 106 static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object); |
| 151 delete instance; | 107 delete instance; |
| 152 } | 108 } |
| 153 | 109 |
| 154 bool BrowserPluginBindingsHasMethod(NPObject* np_obj, NPIdentifier name) { | 110 bool BrowserPluginBindingsHasMethod(NPObject* np_obj, NPIdentifier name) { |
| 155 if (!np_obj) | 111 if (!np_obj) |
| 156 return false; | 112 return false; |
| 157 | 113 |
| 158 if (IdentifierIsAddEventListener(name)) | 114 BrowserPluginBindings* bindings = GetBindings(np_obj); |
| 159 return true; | 115 if (!bindings) |
| 116 return false; | |
| 160 | 117 |
| 161 if (IdentifierIsBackMethod(name)) | 118 return bindings->HasMethod(name); |
| 162 return true; | |
| 163 | |
| 164 if (IdentifierIsCanGoBack(name)) | |
| 165 return true; | |
| 166 | |
| 167 if (IdentifierIsCanGoForward(name)) | |
| 168 return true; | |
| 169 | |
| 170 if (IdentifierIsForwardMethod(name)) | |
| 171 return true; | |
| 172 | |
| 173 if (IdentifierIsGetProcessID(name)) | |
| 174 return true; | |
| 175 | |
| 176 if (IdentifierIsGoMethod(name)) | |
| 177 return true; | |
| 178 | |
| 179 if (IdentifierIsReload(name)) | |
| 180 return true; | |
| 181 | |
| 182 if (IdentifierIsRemoveEventListener(name)) | |
| 183 return true; | |
| 184 | |
| 185 if (IdentifierIsStop(name)) | |
| 186 return true; | |
| 187 | |
| 188 if (IdentifierIsTerminate(name)) | |
| 189 return true; | |
| 190 | |
| 191 return false; | |
| 192 } | 119 } |
| 193 | 120 |
| 194 bool BrowserPluginBindingsInvoke(NPObject* np_obj, NPIdentifier name, | 121 bool BrowserPluginBindingsInvoke(NPObject* np_obj, NPIdentifier name, |
| 195 const NPVariant* args, uint32 arg_count, | 122 const NPVariant* args, uint32 arg_count, |
| 196 NPVariant* result) { | 123 NPVariant* result) { |
| 197 if (!np_obj) | 124 if (!np_obj) |
| 198 return false; | 125 return false; |
| 199 | 126 |
| 200 BrowserPluginBindings* bindings = GetBindings(np_obj); | 127 BrowserPluginBindings* bindings = GetBindings(np_obj); |
| 201 if (!bindings) | 128 if (!bindings) |
| 202 return false; | 129 return false; |
| 203 | 130 |
| 204 if (IdentifierIsAddEventListener(name) && (arg_count == 2)) { | 131 return bindings->InvokeMethod(name, args, arg_count, result); |
| 205 std::string event_name = StringFromNPVariant(args[0]); | |
| 206 if (event_name.empty()) | |
| 207 return false; | |
| 208 | |
| 209 v8::Local<v8::Value> value = | |
| 210 v8::Local<v8::Value>::New(WebBindings::toV8Value(&args[1])); | |
| 211 if (value.IsEmpty() || !value->IsFunction()) | |
| 212 return false; | |
| 213 | |
| 214 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); | |
| 215 result->type = NPVariantType_Bool; | |
| 216 result->value.boolValue = | |
| 217 bindings->instance()->AddEventListener(event_name, function); | |
| 218 return true; | |
| 219 } | |
| 220 | |
| 221 if (IdentifierIsBackMethod(name) && !arg_count) { | |
| 222 bindings->instance()->Back(); | |
| 223 return true; | |
| 224 } | |
| 225 | |
| 226 if (IdentifierIsCanGoBack(name) && !arg_count) { | |
| 227 result->type = NPVariantType_Bool; | |
| 228 result->value.boolValue = bindings->instance()->CanGoBack(); | |
| 229 return true; | |
| 230 } | |
| 231 | |
| 232 if (IdentifierIsCanGoForward(name) && !arg_count) { | |
| 233 result->type = NPVariantType_Bool; | |
| 234 result->value.boolValue = bindings->instance()->CanGoForward(); | |
| 235 return true; | |
| 236 } | |
| 237 | |
| 238 if (IdentifierIsForwardMethod(name) && !arg_count) { | |
| 239 bindings->instance()->Forward(); | |
| 240 return true; | |
| 241 } | |
| 242 | |
| 243 if (IdentifierIsGetProcessID(name) && !arg_count) { | |
| 244 int process_id = bindings->instance()->process_id(); | |
| 245 result->type = NPVariantType_Int32; | |
| 246 result->value.intValue = process_id; | |
| 247 return true; | |
| 248 } | |
| 249 | |
| 250 if (IdentifierIsGoMethod(name) && arg_count == 1) { | |
| 251 bindings->instance()->Go(Int32FromNPVariant(args[0])); | |
| 252 return true; | |
| 253 } | |
| 254 | |
| 255 if (IdentifierIsReload(name) && !arg_count) { | |
| 256 bindings->instance()->Reload(); | |
| 257 return true; | |
| 258 } | |
| 259 | |
| 260 if (IdentifierIsRemoveEventListener(name) && arg_count == 2) { | |
| 261 std::string event_name = StringFromNPVariant(args[0]); | |
| 262 if (event_name.empty()) | |
| 263 return false; | |
| 264 | |
| 265 v8::Local<v8::Value> value = | |
| 266 v8::Local<v8::Value>::New(WebBindings::toV8Value(&args[1])); | |
| 267 | |
| 268 if (value.IsEmpty() || !value->IsFunction()) | |
| 269 return false; | |
| 270 | |
| 271 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); | |
| 272 result->type = NPVariantType_Bool; | |
| 273 result->value.boolValue = | |
| 274 bindings->instance()->RemoveEventListener(event_name, function); | |
| 275 return true; | |
| 276 } | |
| 277 | |
| 278 if (IdentifierIsStop(name) && !arg_count) { | |
| 279 bindings->instance()->Stop(); | |
| 280 return true; | |
| 281 } | |
| 282 | |
| 283 if (IdentifierIsTerminate(name)) { | |
| 284 bindings->instance()->TerminateGuest(); | |
| 285 } | |
| 286 | |
| 287 return false; | |
| 288 } | 132 } |
| 289 | 133 |
| 290 bool BrowserPluginBindingsInvokeDefault(NPObject* np_obj, | 134 bool BrowserPluginBindingsInvokeDefault(NPObject* np_obj, |
| 291 const NPVariant* args, | 135 const NPVariant* args, |
| 292 uint32 arg_count, | 136 uint32 arg_count, |
| 293 NPVariant* result) { | 137 NPVariant* result) { |
| 294 NOTIMPLEMENTED(); | 138 NOTIMPLEMENTED(); |
| 295 return false; | 139 return false; |
| 296 } | 140 } |
| 297 | 141 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 377 &BrowserPluginBindingsInvokeDefault, | 221 &BrowserPluginBindingsInvokeDefault, |
| 378 &BrowserPluginBindingsHasProperty, | 222 &BrowserPluginBindingsHasProperty, |
| 379 &BrowserPluginBindingsGetProperty, | 223 &BrowserPluginBindingsGetProperty, |
| 380 &BrowserPluginBindingsSetProperty, | 224 &BrowserPluginBindingsSetProperty, |
| 381 NULL, | 225 NULL, |
| 382 &BrowserPluginBindingsEnumerate, | 226 &BrowserPluginBindingsEnumerate, |
| 383 }; | 227 }; |
| 384 | 228 |
| 385 } // namespace | 229 } // namespace |
| 386 | 230 |
| 231 namespace internal { | |
| 232 | |
| 233 class BrowserPluginBindingInterface { | |
|
Charlie Reis
2012/10/15 22:19:33
nit: Interface suggests a class with only pure vir
sadrul
2012/10/15 22:37:12
Good point. Done.
| |
| 234 public: | |
| 235 BrowserPluginBindingInterface(const char name[], | |
| 236 uint32 arg_count) | |
| 237 : name_(name), | |
| 238 arg_count_(arg_count) { | |
| 239 } | |
| 240 | |
| 241 virtual ~BrowserPluginBindingInterface() {} | |
| 242 | |
| 243 bool MatchesName(NPIdentifier name) const { | |
| 244 return WebBindings::getStringIdentifier(name_.c_str()) == name; | |
| 245 } | |
| 246 | |
| 247 uint32 arg_count() const { return arg_count_; } | |
| 248 | |
| 249 virtual bool Invoked(BrowserPluginBindings* bindings, | |
|
Charlie Reis
2012/10/15 22:19:33
Why past tense? Invoke seems clearer to me.
sadrul
2012/10/15 22:37:12
Done.
| |
| 250 const NPVariant* args, | |
| 251 NPVariant* result) = 0; | |
| 252 | |
| 253 private: | |
| 254 std::string name_; | |
| 255 uint32 arg_count_; | |
| 256 | |
| 257 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingInterface); | |
| 258 }; | |
| 259 | |
| 260 class BrowserPluginBindingAddListener : public BrowserPluginBindingInterface { | |
| 261 public: | |
| 262 BrowserPluginBindingAddListener() | |
| 263 : BrowserPluginBindingInterface(kAddEventListener, 2) { | |
| 264 } | |
| 265 | |
| 266 virtual bool Invoked(BrowserPluginBindings* bindings, | |
| 267 const NPVariant* args, | |
| 268 NPVariant* result) OVERRIDE { | |
| 269 std::string event_name = StringFromNPVariant(args[0]); | |
| 270 if (event_name.empty()) | |
| 271 return false; | |
| 272 | |
| 273 v8::Local<v8::Value> value = | |
| 274 v8::Local<v8::Value>::New(WebBindings::toV8Value(&args[1])); | |
| 275 if (value.IsEmpty() || !value->IsFunction()) | |
| 276 return false; | |
| 277 | |
| 278 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); | |
| 279 BOOLEAN_TO_NPVARIANT( | |
| 280 bindings->instance()->AddEventListener(event_name, function), *result); | |
| 281 return true; | |
| 282 } | |
| 283 | |
| 284 private: | |
| 285 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingAddListener); | |
| 286 }; | |
| 287 | |
| 288 class BrowserPluginBindingBack : public BrowserPluginBindingInterface { | |
| 289 public: | |
| 290 BrowserPluginBindingBack() | |
| 291 : BrowserPluginBindingInterface(kBackMethod, 0) { | |
| 292 } | |
| 293 | |
| 294 virtual bool Invoked(BrowserPluginBindings* bindings, | |
| 295 const NPVariant* args, | |
| 296 NPVariant* result) OVERRIDE { | |
| 297 bindings->instance()->Back(); | |
| 298 return true; | |
| 299 } | |
| 300 | |
| 301 private: | |
| 302 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingBack); | |
| 303 }; | |
| 304 | |
| 305 class BrowserPluginBindingCanGoBack : public BrowserPluginBindingInterface { | |
| 306 public: | |
| 307 BrowserPluginBindingCanGoBack() | |
| 308 : BrowserPluginBindingInterface(kCanGoBack, 0) { | |
| 309 } | |
| 310 | |
| 311 virtual bool Invoked(BrowserPluginBindings* bindings, | |
| 312 const NPVariant* args, | |
| 313 NPVariant* result) OVERRIDE { | |
| 314 BOOLEAN_TO_NPVARIANT(bindings->instance()->CanGoBack(), *result); | |
| 315 return true; | |
| 316 } | |
| 317 | |
| 318 private: | |
| 319 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingCanGoBack); | |
| 320 }; | |
| 321 | |
| 322 class BrowserPluginBindingCanGoForward : public BrowserPluginBindingInterface { | |
| 323 public: | |
| 324 BrowserPluginBindingCanGoForward() | |
| 325 : BrowserPluginBindingInterface(kCanGoForward, 0) { | |
| 326 } | |
| 327 | |
| 328 virtual bool Invoked(BrowserPluginBindings* bindings, | |
| 329 const NPVariant* args, | |
| 330 NPVariant* result) OVERRIDE { | |
| 331 BOOLEAN_TO_NPVARIANT(bindings->instance()->CanGoForward(), *result); | |
| 332 return true; | |
| 333 } | |
| 334 | |
| 335 private: | |
| 336 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingCanGoForward); | |
| 337 }; | |
| 338 | |
| 339 class BrowserPluginBindingForward : public BrowserPluginBindingInterface { | |
| 340 public: | |
| 341 BrowserPluginBindingForward() | |
| 342 : BrowserPluginBindingInterface(kForwardMethod, 0) { | |
| 343 } | |
| 344 | |
| 345 virtual bool Invoked(BrowserPluginBindings* bindings, | |
| 346 const NPVariant* args, | |
| 347 NPVariant* result) OVERRIDE { | |
| 348 bindings->instance()->Forward(); | |
| 349 return true; | |
| 350 } | |
| 351 | |
| 352 private: | |
| 353 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingForward); | |
| 354 }; | |
| 355 | |
| 356 class BrowserPluginBindingGetProcessID : public BrowserPluginBindingInterface { | |
| 357 public: | |
| 358 BrowserPluginBindingGetProcessID() | |
| 359 : BrowserPluginBindingInterface(kGetProcessId, 0) { | |
| 360 } | |
| 361 | |
| 362 virtual bool Invoked(BrowserPluginBindings* bindings, | |
| 363 const NPVariant* args, | |
| 364 NPVariant* result) OVERRIDE { | |
| 365 int process_id = bindings->instance()->process_id(); | |
| 366 INT32_TO_NPVARIANT(process_id, *result); | |
| 367 return true; | |
| 368 } | |
| 369 | |
| 370 private: | |
| 371 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingGetProcessID); | |
| 372 }; | |
| 373 | |
| 374 class BrowserPluginBindingGo : public BrowserPluginBindingInterface { | |
| 375 public: | |
| 376 BrowserPluginBindingGo() | |
| 377 : BrowserPluginBindingInterface(kGoMethod, 1) { | |
| 378 } | |
| 379 | |
| 380 virtual bool Invoked(BrowserPluginBindings* bindings, | |
| 381 const NPVariant* args, | |
| 382 NPVariant* result) OVERRIDE { | |
| 383 bindings->instance()->Go(Int32FromNPVariant(args[0])); | |
| 384 return true; | |
| 385 } | |
| 386 | |
| 387 private: | |
| 388 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingGo); | |
| 389 }; | |
| 390 | |
| 391 class BrowserPluginBindingReload : public BrowserPluginBindingInterface { | |
| 392 public: | |
| 393 BrowserPluginBindingReload() | |
| 394 : BrowserPluginBindingInterface(kReloadMethod, 0) { | |
| 395 } | |
| 396 | |
| 397 virtual bool Invoked(BrowserPluginBindings* bindings, | |
| 398 const NPVariant* args, | |
| 399 NPVariant* result) OVERRIDE { | |
| 400 bindings->instance()->Reload(); | |
| 401 return true; | |
| 402 } | |
| 403 | |
| 404 private: | |
| 405 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingReload); | |
| 406 }; | |
| 407 | |
| 408 class BrowserPluginBindingRemoveListener | |
| 409 : public BrowserPluginBindingInterface { | |
| 410 public: | |
| 411 BrowserPluginBindingRemoveListener() | |
| 412 : BrowserPluginBindingInterface(kRemoveEventListener, 2) { | |
| 413 } | |
| 414 | |
| 415 virtual bool Invoked(BrowserPluginBindings* bindings, | |
| 416 const NPVariant* args, | |
| 417 NPVariant* result) OVERRIDE { | |
| 418 std::string event_name = StringFromNPVariant(args[0]); | |
| 419 if (event_name.empty()) | |
| 420 return false; | |
| 421 | |
| 422 v8::Local<v8::Value> value = | |
| 423 v8::Local<v8::Value>::New(WebBindings::toV8Value(&args[1])); | |
| 424 | |
| 425 if (value.IsEmpty() || !value->IsFunction()) | |
| 426 return false; | |
| 427 | |
| 428 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); | |
| 429 BOOLEAN_TO_NPVARIANT( | |
| 430 bindings->instance()->RemoveEventListener(event_name, function), | |
| 431 *result); | |
| 432 return true; | |
| 433 } | |
| 434 | |
| 435 private: | |
| 436 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingRemoveListener); | |
| 437 }; | |
| 438 | |
| 439 class BrowserPluginBindingStop : public BrowserPluginBindingInterface { | |
| 440 public: | |
| 441 BrowserPluginBindingStop() | |
| 442 : BrowserPluginBindingInterface(kStopMethod, 0) { | |
| 443 } | |
| 444 | |
| 445 virtual bool Invoked(BrowserPluginBindings* bindings, | |
| 446 const NPVariant* args, | |
| 447 NPVariant* result) OVERRIDE { | |
| 448 bindings->instance()->Stop(); | |
| 449 return true; | |
| 450 } | |
| 451 | |
| 452 private: | |
| 453 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingStop); | |
| 454 }; | |
| 455 | |
| 456 class BrowserPluginBindingTerminate : public BrowserPluginBindingInterface { | |
| 457 public: | |
| 458 BrowserPluginBindingTerminate() | |
| 459 : BrowserPluginBindingInterface(kTerminateMethod, 0) { | |
| 460 } | |
| 461 | |
| 462 virtual bool Invoked(BrowserPluginBindings* bindings, | |
| 463 const NPVariant* args, | |
| 464 NPVariant* result) OVERRIDE { | |
| 465 bindings->instance()->TerminateGuest(); | |
| 466 return true; | |
| 467 } | |
| 468 | |
| 469 private: | |
| 470 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingTerminate); | |
| 471 }; | |
| 472 | |
| 473 } // namespace internal | |
| 474 | |
| 387 // BrowserPluginBindings ------------------------------------------------------ | 475 // BrowserPluginBindings ------------------------------------------------------ |
| 388 | 476 |
| 389 BrowserPluginBindings::BrowserPluginNPObject::BrowserPluginNPObject() { | 477 BrowserPluginBindings::BrowserPluginNPObject::BrowserPluginNPObject() { |
| 390 } | 478 } |
| 391 | 479 |
| 392 BrowserPluginBindings::BrowserPluginNPObject::~BrowserPluginNPObject() { | 480 BrowserPluginBindings::BrowserPluginNPObject::~BrowserPluginNPObject() { |
| 393 } | 481 } |
| 394 | 482 |
| 395 BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance) | 483 BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance) |
| 396 : instance_(instance), | 484 : instance_(instance), |
| 397 np_object_(NULL), | 485 np_object_(NULL), |
| 398 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 486 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 399 NPObject* obj = | 487 NPObject* obj = |
| 400 WebBindings::createObject(NULL, &browser_plugin_message_class); | 488 WebBindings::createObject(NULL, &browser_plugin_message_class); |
| 401 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); | 489 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); |
| 402 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); | 490 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); |
| 491 | |
| 492 method_bindings_.push_back(new internal::BrowserPluginBindingAddListener); | |
| 493 method_bindings_.push_back(new internal::BrowserPluginBindingBack); | |
| 494 method_bindings_.push_back(new internal::BrowserPluginBindingCanGoBack); | |
| 495 method_bindings_.push_back(new internal::BrowserPluginBindingCanGoForward); | |
| 496 method_bindings_.push_back(new internal::BrowserPluginBindingForward); | |
| 497 method_bindings_.push_back(new internal::BrowserPluginBindingGetProcessID); | |
| 498 method_bindings_.push_back(new internal::BrowserPluginBindingGo); | |
| 499 method_bindings_.push_back(new internal::BrowserPluginBindingReload); | |
| 500 method_bindings_.push_back(new internal::BrowserPluginBindingRemoveListener); | |
| 501 method_bindings_.push_back(new internal::BrowserPluginBindingStop); | |
| 502 method_bindings_.push_back(new internal::BrowserPluginBindingTerminate); | |
| 403 } | 503 } |
| 404 | 504 |
| 405 BrowserPluginBindings::~BrowserPluginBindings() { | 505 BrowserPluginBindings::~BrowserPluginBindings() { |
| 406 WebBindings::releaseObject(np_object_); | 506 WebBindings::releaseObject(np_object_); |
| 407 } | 507 } |
| 408 | 508 |
| 509 bool BrowserPluginBindings::HasMethod(NPIdentifier name) const { | |
| 510 for (BindingList::const_iterator iter = method_bindings_.begin(); | |
| 511 iter != method_bindings_.end(); | |
|
Charlie Reis
2012/10/15 22:19:33
nit: wrong indent. (Should have one more space, t
sadrul
2012/10/15 22:37:12
Done.
| |
| 512 ++iter) { | |
| 513 if ((*iter)->MatchesName(name)) | |
| 514 return true; | |
| 515 } | |
| 516 return false; | |
| 517 } | |
| 518 | |
| 519 bool BrowserPluginBindings::InvokeMethod(NPIdentifier name, | |
| 520 const NPVariant* args, | |
| 521 uint32 arg_count, | |
| 522 NPVariant* result) { | |
| 523 for (BindingList::iterator iter = method_bindings_.begin(); | |
| 524 iter != method_bindings_.end(); | |
| 525 ++iter) { | |
| 526 if ((*iter)->MatchesName(name) && (*iter)->arg_count() == arg_count) | |
| 527 return (*iter)->Invoked(this, args, result); | |
| 528 } | |
| 529 return false; | |
| 530 } | |
| 531 | |
| 409 } // namespace content | 532 } // namespace content |
| OLD | NEW |