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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 message_channel.get(); | 44 message_channel.get(); |
45 } | 45 } |
46 | 46 |
47 std::string StringFromNPVariant(const NPVariant& variant) { | 47 std::string StringFromNPVariant(const NPVariant& variant) { |
48 if (!NPVARIANT_IS_STRING(variant)) | 48 if (!NPVARIANT_IS_STRING(variant)) |
49 return std::string(); | 49 return std::string(); |
50 const NPString& np_string = NPVARIANT_TO_STRING(variant); | 50 const NPString& np_string = NPVARIANT_TO_STRING(variant); |
51 return std::string(np_string.UTF8Characters, np_string.UTF8Length); | 51 return std::string(np_string.UTF8Characters, np_string.UTF8Length); |
52 } | 52 } |
53 | 53 |
54 bool StringToNPVariant(const std::string &in, NPVariant *variant) { | |
55 size_t length = in.size(); | |
56 NPUTF8 *chars = static_cast<NPUTF8 *>(malloc(length)); | |
57 if (!chars) { | |
58 VOID_TO_NPVARIANT(*variant); | |
59 return false; | |
60 } | |
61 memcpy(chars, in.c_str(), length); | |
62 STRINGN_TO_NPVARIANT(chars, length, *variant); | |
63 return true; | |
64 } | |
65 | |
66 // Depending on where the attribute comes from it could be a string, int32, | 54 // Depending on where the attribute comes from it could be a string, int32, |
67 // or a double. Javascript tends to produce an int32 or a string, but setting | 55 // or a double. Javascript tends to produce an int32 or a string, but setting |
68 // the value from the developer tools console may also produce a double. | 56 // the value from the developer tools console may also produce a double. |
69 int IntFromNPVariant(const NPVariant& variant) { | 57 int IntFromNPVariant(const NPVariant& variant) { |
70 int value = 0; | 58 int value = 0; |
71 switch (variant.type) { | 59 switch (variant.type) { |
72 case NPVariantType_Double: | 60 case NPVariantType_Double: |
73 value = NPVARIANT_TO_DOUBLE(variant); | 61 value = NPVARIANT_TO_DOUBLE(variant); |
74 break; | 62 break; |
75 case NPVariantType_Int32: | 63 case NPVariantType_Int32: |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 } | 496 } |
509 virtual void RemoveProperty(BrowserPluginBindings* bindings, | 497 virtual void RemoveProperty(BrowserPluginBindings* bindings, |
510 NPObject* np_obj) OVERRIDE { | 498 NPObject* np_obj) OVERRIDE { |
511 bindings->instance()->RemoveDOMAttribute(name()); | 499 bindings->instance()->RemoveDOMAttribute(name()); |
512 bindings->instance()->ParseSizeContraintsChanged(); | 500 bindings->instance()->ParseSizeContraintsChanged(); |
513 } | 501 } |
514 private: | 502 private: |
515 DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingMinWidth); | 503 DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingMinWidth); |
516 }; | 504 }; |
517 | 505 |
518 class BrowserPluginPropertyBindingPartition | |
519 : public BrowserPluginPropertyBinding { | |
520 public: | |
521 BrowserPluginPropertyBindingPartition() | |
522 : BrowserPluginPropertyBinding(browser_plugin::kAttributePartition) { | |
523 } | |
524 virtual bool GetProperty(BrowserPluginBindings* bindings, | |
525 NPVariant* result) OVERRIDE { | |
526 std::string partition_id = bindings->instance()->GetPartitionAttribute(); | |
527 return StringToNPVariant(partition_id, result); | |
528 } | |
529 virtual bool SetProperty(BrowserPluginBindings* bindings, | |
530 NPObject* np_obj, | |
531 const NPVariant* variant) OVERRIDE { | |
532 std::string new_value = StringFromNPVariant(*variant); | |
533 std::string old_value = bindings->instance()->GetPartitionAttribute(); | |
534 if (old_value != new_value) { | |
535 UpdateDOMAttribute(bindings, new_value); | |
536 std::string error_message; | |
537 if (!bindings->instance()->ParsePartitionAttribute(&error_message)) { | |
538 // Reset to old value on error. | |
539 UpdateDOMAttribute(bindings, old_value); | |
540 // Exceptions must be set as the last operation before returning to | |
541 // script. | |
542 WebBindings::setException( | |
543 np_obj, static_cast<const NPUTF8 *>(error_message.c_str())); | |
544 return false; | |
545 } | |
546 } | |
547 return true; | |
548 } | |
549 virtual void RemoveProperty(BrowserPluginBindings* bindings, | |
550 NPObject* np_obj) OVERRIDE { | |
551 std::string error_message; | |
552 if (bindings->instance()->CanRemovePartitionAttribute(&error_message)) { | |
553 bindings->instance()->RemoveDOMAttribute(name()); | |
554 } else { | |
555 WebBindings::setException( | |
556 np_obj, static_cast<const NPUTF8 *>(error_message.c_str())); | |
557 } | |
558 } | |
559 private: | |
560 DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingPartition); | |
561 }; | |
562 | |
563 class BrowserPluginPropertyBindingSrc : public BrowserPluginPropertyBinding { | |
564 public: | |
565 BrowserPluginPropertyBindingSrc() | |
566 : BrowserPluginPropertyBinding(browser_plugin::kAttributeSrc) { | |
567 } | |
568 virtual bool GetProperty(BrowserPluginBindings* bindings, | |
569 NPVariant* result) OVERRIDE { | |
570 std::string src = bindings->instance()->GetSrcAttribute(); | |
571 return StringToNPVariant(src, result); | |
572 } | |
573 virtual bool SetProperty(BrowserPluginBindings* bindings, | |
574 NPObject* np_obj, | |
575 const NPVariant* variant) OVERRIDE { | |
576 std::string new_value = StringFromNPVariant(*variant); | |
577 // We should not be issuing navigation IPCs if we attempt to set the | |
578 // src property to the empty string. Instead, we want to simply restore | |
579 // the src attribute back to its old value. | |
580 if (new_value.empty()) { | |
581 return true; | |
582 } | |
583 std::string old_value = bindings->instance()->GetSrcAttribute(); | |
584 // If the new value was empty then we're effectively resetting the | |
585 // attribute to the old value here. This will be picked up by <webview>'s | |
586 // mutation observer and will restore the src attribute after it has been | |
587 // removed. | |
588 UpdateDOMAttribute(bindings, new_value); | |
589 std::string error_message; | |
590 if (!bindings->instance()->ParseSrcAttribute(&error_message)) { | |
591 // Reset to old value on error. | |
592 UpdateDOMAttribute(bindings, old_value); | |
593 // Exceptions must be set as the last operation before returning to | |
594 // script. | |
595 WebBindings::setException( | |
596 np_obj, static_cast<const NPUTF8 *>(error_message.c_str())); | |
597 return false; | |
598 } | |
599 return true; | |
600 } | |
601 virtual void RemoveProperty(BrowserPluginBindings* bindings, | |
602 NPObject* np_obj) OVERRIDE { | |
603 bindings->instance()->RemoveDOMAttribute(name()); | |
604 } | |
605 private: | |
606 DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingSrc); | |
607 }; | |
608 | |
609 | 506 |
610 // BrowserPluginBindings ------------------------------------------------------ | 507 // BrowserPluginBindings ------------------------------------------------------ |
611 | 508 |
612 BrowserPluginBindings::BrowserPluginNPObject::BrowserPluginNPObject() { | 509 BrowserPluginBindings::BrowserPluginNPObject::BrowserPluginNPObject() { |
613 } | 510 } |
614 | 511 |
615 BrowserPluginBindings::BrowserPluginNPObject::~BrowserPluginNPObject() { | 512 BrowserPluginBindings::BrowserPluginNPObject::~BrowserPluginNPObject() { |
616 } | 513 } |
617 | 514 |
618 BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance) | 515 BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance) |
619 : instance_(instance), | 516 : instance_(instance), |
620 np_object_(NULL), | 517 np_object_(NULL), |
621 weak_ptr_factory_(this) { | 518 weak_ptr_factory_(this) { |
622 NPObject* obj = | 519 NPObject* obj = |
623 WebBindings::createObject(instance->pluginNPP(), | 520 WebBindings::createObject(instance->pluginNPP(), |
624 &browser_plugin_message_class); | 521 &browser_plugin_message_class); |
625 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); | 522 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); |
626 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); | 523 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); |
627 | 524 |
628 method_bindings_.push_back(new BrowserPluginBindingAttach); | 525 method_bindings_.push_back(new BrowserPluginBindingAttach); |
629 | 526 |
630 property_bindings_.push_back( | 527 property_bindings_.push_back( |
631 new BrowserPluginPropertyBindingAllowTransparency); | 528 new BrowserPluginPropertyBindingAllowTransparency); |
632 property_bindings_.push_back(new BrowserPluginPropertyBindingAutoSize); | 529 property_bindings_.push_back(new BrowserPluginPropertyBindingAutoSize); |
633 property_bindings_.push_back(new BrowserPluginPropertyBindingContentWindow); | 530 property_bindings_.push_back(new BrowserPluginPropertyBindingContentWindow); |
634 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxHeight); | 531 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxHeight); |
635 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxWidth); | 532 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxWidth); |
636 property_bindings_.push_back(new BrowserPluginPropertyBindingMinHeight); | 533 property_bindings_.push_back(new BrowserPluginPropertyBindingMinHeight); |
637 property_bindings_.push_back(new BrowserPluginPropertyBindingMinWidth); | 534 property_bindings_.push_back(new BrowserPluginPropertyBindingMinWidth); |
638 property_bindings_.push_back(new BrowserPluginPropertyBindingPartition); | |
639 property_bindings_.push_back(new BrowserPluginPropertyBindingSrc); | |
640 } | 535 } |
641 | 536 |
642 BrowserPluginBindings::~BrowserPluginBindings() { | 537 BrowserPluginBindings::~BrowserPluginBindings() { |
643 WebBindings::releaseObject(np_object_); | 538 WebBindings::releaseObject(np_object_); |
644 } | 539 } |
645 | 540 |
646 bool BrowserPluginBindings::HasMethod(NPIdentifier name) const { | 541 bool BrowserPluginBindings::HasMethod(NPIdentifier name) const { |
647 for (BindingList::const_iterator iter = method_bindings_.begin(); | 542 for (BindingList::const_iterator iter = method_bindings_.begin(); |
648 iter != method_bindings_.end(); | 543 iter != method_bindings_.end(); |
649 ++iter) { | 544 ++iter) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 for (PropertyBindingList::iterator iter = property_bindings_.begin(); | 604 for (PropertyBindingList::iterator iter = property_bindings_.begin(); |
710 iter != property_bindings_.end(); | 605 iter != property_bindings_.end(); |
711 ++iter) { | 606 ++iter) { |
712 if ((*iter)->MatchesName(name)) | 607 if ((*iter)->MatchesName(name)) |
713 return (*iter)->GetProperty(this, result); | 608 return (*iter)->GetProperty(this, result); |
714 } | 609 } |
715 return false; | 610 return false; |
716 } | 611 } |
717 | 612 |
718 } // namespace content | 613 } // namespace content |
OLD | NEW |