| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 return new BrowserPluginBindings::BrowserPluginNPObject; | 80 return new BrowserPluginBindings::BrowserPluginNPObject; |
| 81 } | 81 } |
| 82 | 82 |
| 83 void BrowserPluginBindingsDeallocate(NPObject* object) { | 83 void BrowserPluginBindingsDeallocate(NPObject* object) { |
| 84 BrowserPluginBindings::BrowserPluginNPObject* instance = | 84 BrowserPluginBindings::BrowserPluginNPObject* instance = |
| 85 static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object); | 85 static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object); |
| 86 delete instance; | 86 delete instance; |
| 87 } | 87 } |
| 88 | 88 |
| 89 bool BrowserPluginBindingsHasMethod(NPObject* np_obj, NPIdentifier name) { | 89 bool BrowserPluginBindingsHasMethod(NPObject* np_obj, NPIdentifier name) { |
| 90 if (!np_obj) | 90 return false; |
| 91 return false; | |
| 92 | |
| 93 BrowserPluginBindings* bindings = GetBindings(np_obj); | |
| 94 if (!bindings) | |
| 95 return false; | |
| 96 | |
| 97 return bindings->HasMethod(name); | |
| 98 } | |
| 99 | |
| 100 bool BrowserPluginBindingsInvoke(NPObject* np_obj, NPIdentifier name, | |
| 101 const NPVariant* args, uint32 arg_count, | |
| 102 NPVariant* result) { | |
| 103 if (!np_obj) | |
| 104 return false; | |
| 105 | |
| 106 BrowserPluginBindings* bindings = GetBindings(np_obj); | |
| 107 if (!bindings) | |
| 108 return false; | |
| 109 | |
| 110 return bindings->InvokeMethod(name, args, arg_count, result); | |
| 111 } | 91 } |
| 112 | 92 |
| 113 bool BrowserPluginBindingsInvokeDefault(NPObject* np_obj, | 93 bool BrowserPluginBindingsInvokeDefault(NPObject* np_obj, |
| 114 const NPVariant* args, | 94 const NPVariant* args, |
| 115 uint32 arg_count, | 95 uint32 arg_count, |
| 116 NPVariant* result) { | 96 NPVariant* result) { |
| 117 NOTIMPLEMENTED(); | 97 NOTIMPLEMENTED(); |
| 118 return false; | 98 return false; |
| 119 } | 99 } |
| 120 | 100 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 return bindings->SetProperty(np_obj, name, variant); | 145 return bindings->SetProperty(np_obj, name, variant); |
| 166 } | 146 } |
| 167 | 147 |
| 168 bool BrowserPluginBindingsEnumerate(NPObject *np_obj, NPIdentifier **value, | 148 bool BrowserPluginBindingsEnumerate(NPObject *np_obj, NPIdentifier **value, |
| 169 uint32_t *count) { | 149 uint32_t *count) { |
| 170 NOTIMPLEMENTED(); | 150 NOTIMPLEMENTED(); |
| 171 return true; | 151 return true; |
| 172 } | 152 } |
| 173 | 153 |
| 174 NPClass browser_plugin_message_class = { | 154 NPClass browser_plugin_message_class = { |
| 175 NP_CLASS_STRUCT_VERSION, | 155 NP_CLASS_STRUCT_VERSION, &BrowserPluginBindingsAllocate, |
| 176 &BrowserPluginBindingsAllocate, | 156 &BrowserPluginBindingsDeallocate, NULL, |
| 177 &BrowserPluginBindingsDeallocate, | 157 &BrowserPluginBindingsHasMethod, NULL, |
| 178 NULL, | 158 &BrowserPluginBindingsInvokeDefault, &BrowserPluginBindingsHasProperty, |
| 179 &BrowserPluginBindingsHasMethod, | 159 &BrowserPluginBindingsGetProperty, &BrowserPluginBindingsSetProperty, |
| 180 &BrowserPluginBindingsInvoke, | 160 NULL, &BrowserPluginBindingsEnumerate, |
| 181 &BrowserPluginBindingsInvokeDefault, | |
| 182 &BrowserPluginBindingsHasProperty, | |
| 183 &BrowserPluginBindingsGetProperty, | |
| 184 &BrowserPluginBindingsSetProperty, | |
| 185 NULL, | |
| 186 &BrowserPluginBindingsEnumerate, | |
| 187 }; | 161 }; |
| 188 | 162 |
| 189 } // namespace | 163 } // namespace |
| 190 | 164 |
| 191 // BrowserPluginMethodBinding -------------------------------------------------- | |
| 192 | |
| 193 class BrowserPluginMethodBinding { | |
| 194 public: | |
| 195 BrowserPluginMethodBinding(const char name[], uint32 arg_count) | |
| 196 : name_(name), | |
| 197 arg_count_(arg_count) { | |
| 198 } | |
| 199 | |
| 200 virtual ~BrowserPluginMethodBinding() {} | |
| 201 | |
| 202 bool MatchesName(NPIdentifier name) const { | |
| 203 return WebBindings::getStringIdentifier(name_.c_str()) == name; | |
| 204 } | |
| 205 | |
| 206 uint32 arg_count() const { return arg_count_; } | |
| 207 | |
| 208 virtual bool Invoke(BrowserPluginBindings* bindings, | |
| 209 const NPVariant* args, | |
| 210 NPVariant* result) = 0; | |
| 211 | |
| 212 private: | |
| 213 std::string name_; | |
| 214 uint32 arg_count_; | |
| 215 | |
| 216 DISALLOW_COPY_AND_ASSIGN(BrowserPluginMethodBinding); | |
| 217 }; | |
| 218 | |
| 219 class BrowserPluginBindingAttach: public BrowserPluginMethodBinding { | |
| 220 public: | |
| 221 BrowserPluginBindingAttach() | |
| 222 : BrowserPluginMethodBinding(browser_plugin::kMethodInternalAttach, 2) {} | |
| 223 | |
| 224 virtual bool Invoke(BrowserPluginBindings* bindings, | |
| 225 const NPVariant* args, | |
| 226 NPVariant* result) OVERRIDE { | |
| 227 bool attached = InvokeHelper(bindings, args); | |
| 228 BOOLEAN_TO_NPVARIANT(attached, *result); | |
| 229 return true; | |
| 230 } | |
| 231 | |
| 232 private: | |
| 233 bool InvokeHelper(BrowserPluginBindings* bindings, const NPVariant* args) { | |
| 234 if (!bindings->instance()->render_view()) | |
| 235 return false; | |
| 236 | |
| 237 int instance_id = IntFromNPVariant(args[0]); | |
| 238 if (!instance_id) | |
| 239 return false; | |
| 240 | |
| 241 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
| 242 v8::Handle<v8::Value> obj(blink::WebBindings::toV8Value(&args[1])); | |
| 243 scoped_ptr<base::Value> value( | |
| 244 converter->FromV8Value(obj, bindings->instance()->render_view()-> | |
| 245 GetWebView()->mainFrame()->mainWorldScriptContext())); | |
| 246 if (!value) | |
| 247 return false; | |
| 248 | |
| 249 if (!value->IsType(base::Value::TYPE_DICTIONARY)) | |
| 250 return false; | |
| 251 | |
| 252 scoped_ptr<base::DictionaryValue> extra_params( | |
| 253 static_cast<base::DictionaryValue*>(value.release())); | |
| 254 bindings->instance()->Attach(instance_id, extra_params.Pass()); | |
| 255 return true; | |
| 256 } | |
| 257 DISALLOW_COPY_AND_ASSIGN(BrowserPluginBindingAttach); | |
| 258 }; | |
| 259 | |
| 260 // BrowserPluginPropertyBinding ------------------------------------------------ | 165 // BrowserPluginPropertyBinding ------------------------------------------------ |
| 261 | 166 |
| 262 class BrowserPluginPropertyBinding { | 167 class BrowserPluginPropertyBinding { |
| 263 public: | 168 public: |
| 264 explicit BrowserPluginPropertyBinding(const char name[]) : name_(name) {} | 169 explicit BrowserPluginPropertyBinding(const char name[]) : name_(name) {} |
| 265 virtual ~BrowserPluginPropertyBinding() {} | 170 virtual ~BrowserPluginPropertyBinding() {} |
| 266 const std::string& name() const { return name_; } | 171 const std::string& name() const { return name_; } |
| 267 bool MatchesName(NPIdentifier name) const { | 172 bool MatchesName(NPIdentifier name) const { |
| 268 return WebBindings::getStringIdentifier(name_.c_str()) == name; | 173 return WebBindings::getStringIdentifier(name_.c_str()) == name; |
| 269 } | 174 } |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance) | 420 BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance) |
| 516 : instance_(instance), | 421 : instance_(instance), |
| 517 np_object_(NULL), | 422 np_object_(NULL), |
| 518 weak_ptr_factory_(this) { | 423 weak_ptr_factory_(this) { |
| 519 NPObject* obj = | 424 NPObject* obj = |
| 520 WebBindings::createObject(instance->pluginNPP(), | 425 WebBindings::createObject(instance->pluginNPP(), |
| 521 &browser_plugin_message_class); | 426 &browser_plugin_message_class); |
| 522 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); | 427 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); |
| 523 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); | 428 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); |
| 524 | 429 |
| 525 method_bindings_.push_back(new BrowserPluginBindingAttach); | |
| 526 | |
| 527 property_bindings_.push_back( | 430 property_bindings_.push_back( |
| 528 new BrowserPluginPropertyBindingAllowTransparency); | 431 new BrowserPluginPropertyBindingAllowTransparency); |
| 529 property_bindings_.push_back(new BrowserPluginPropertyBindingAutoSize); | 432 property_bindings_.push_back(new BrowserPluginPropertyBindingAutoSize); |
| 530 property_bindings_.push_back(new BrowserPluginPropertyBindingContentWindow); | 433 property_bindings_.push_back(new BrowserPluginPropertyBindingContentWindow); |
| 531 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxHeight); | 434 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxHeight); |
| 532 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxWidth); | 435 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxWidth); |
| 533 property_bindings_.push_back(new BrowserPluginPropertyBindingMinHeight); | 436 property_bindings_.push_back(new BrowserPluginPropertyBindingMinHeight); |
| 534 property_bindings_.push_back(new BrowserPluginPropertyBindingMinWidth); | 437 property_bindings_.push_back(new BrowserPluginPropertyBindingMinWidth); |
| 535 } | 438 } |
| 536 | 439 |
| 537 BrowserPluginBindings::~BrowserPluginBindings() { | 440 BrowserPluginBindings::~BrowserPluginBindings() { |
| 538 WebBindings::releaseObject(np_object_); | 441 WebBindings::releaseObject(np_object_); |
| 539 } | 442 } |
| 540 | 443 |
| 541 bool BrowserPluginBindings::HasMethod(NPIdentifier name) const { | |
| 542 for (BindingList::const_iterator iter = method_bindings_.begin(); | |
| 543 iter != method_bindings_.end(); | |
| 544 ++iter) { | |
| 545 if ((*iter)->MatchesName(name)) | |
| 546 return true; | |
| 547 } | |
| 548 return false; | |
| 549 } | |
| 550 | |
| 551 bool BrowserPluginBindings::InvokeMethod(NPIdentifier name, | |
| 552 const NPVariant* args, | |
| 553 uint32 arg_count, | |
| 554 NPVariant* result) { | |
| 555 for (BindingList::iterator iter = method_bindings_.begin(); | |
| 556 iter != method_bindings_.end(); | |
| 557 ++iter) { | |
| 558 if ((*iter)->MatchesName(name) && (*iter)->arg_count() == arg_count) | |
| 559 return (*iter)->Invoke(this, args, result); | |
| 560 } | |
| 561 return false; | |
| 562 } | |
| 563 | |
| 564 bool BrowserPluginBindings::HasProperty(NPIdentifier name) const { | 444 bool BrowserPluginBindings::HasProperty(NPIdentifier name) const { |
| 565 for (PropertyBindingList::const_iterator iter = property_bindings_.begin(); | 445 for (PropertyBindingList::const_iterator iter = property_bindings_.begin(); |
| 566 iter != property_bindings_.end(); | 446 iter != property_bindings_.end(); |
| 567 ++iter) { | 447 ++iter) { |
| 568 if ((*iter)->MatchesName(name)) | 448 if ((*iter)->MatchesName(name)) |
| 569 return true; | 449 return true; |
| 570 } | 450 } |
| 571 return false; | 451 return false; |
| 572 } | 452 } |
| 573 | 453 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 604 for (PropertyBindingList::iterator iter = property_bindings_.begin(); | 484 for (PropertyBindingList::iterator iter = property_bindings_.begin(); |
| 605 iter != property_bindings_.end(); | 485 iter != property_bindings_.end(); |
| 606 ++iter) { | 486 ++iter) { |
| 607 if ((*iter)->MatchesName(name)) | 487 if ((*iter)->MatchesName(name)) |
| 608 return (*iter)->GetProperty(this, result); | 488 return (*iter)->GetProperty(this, result); |
| 609 } | 489 } |
| 610 return false; | 490 return false; |
| 611 } | 491 } |
| 612 | 492 |
| 613 } // namespace content | 493 } // namespace content |
| OLD | NEW |