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 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 } | 491 } |
504 virtual void RemoveProperty(BrowserPluginBindings* bindings, | 492 virtual void RemoveProperty(BrowserPluginBindings* bindings, |
505 NPObject* np_obj) OVERRIDE { | 493 NPObject* np_obj) OVERRIDE { |
506 bindings->instance()->RemoveDOMAttribute(name()); | 494 bindings->instance()->RemoveDOMAttribute(name()); |
507 bindings->instance()->ParseSizeContraintsChanged(); | 495 bindings->instance()->ParseSizeContraintsChanged(); |
508 } | 496 } |
509 private: | 497 private: |
510 DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingMinWidth); | 498 DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingMinWidth); |
511 }; | 499 }; |
512 | 500 |
513 class BrowserPluginPropertyBindingPartition | |
514 : public BrowserPluginPropertyBinding { | |
515 public: | |
516 BrowserPluginPropertyBindingPartition() | |
517 : BrowserPluginPropertyBinding(browser_plugin::kAttributePartition) { | |
518 } | |
519 virtual bool GetProperty(BrowserPluginBindings* bindings, | |
520 NPVariant* result) OVERRIDE { | |
521 std::string partition_id = bindings->instance()->GetPartitionAttribute(); | |
522 return StringToNPVariant(partition_id, result); | |
523 } | |
524 virtual bool SetProperty(BrowserPluginBindings* bindings, | |
525 NPObject* np_obj, | |
526 const NPVariant* variant) OVERRIDE { | |
527 std::string new_value = StringFromNPVariant(*variant); | |
528 std::string old_value = bindings->instance()->GetPartitionAttribute(); | |
529 if (old_value != new_value) { | |
530 UpdateDOMAttribute(bindings, new_value); | |
531 std::string error_message; | |
532 if (!bindings->instance()->ParsePartitionAttribute(&error_message)) { | |
533 // Reset to old value on error. | |
534 UpdateDOMAttribute(bindings, old_value); | |
535 // Exceptions must be set as the last operation before returning to | |
536 // script. | |
537 WebBindings::setException( | |
538 np_obj, static_cast<const NPUTF8 *>(error_message.c_str())); | |
539 return false; | |
540 } | |
541 } | |
542 return true; | |
543 } | |
544 virtual void RemoveProperty(BrowserPluginBindings* bindings, | |
545 NPObject* np_obj) OVERRIDE { | |
546 std::string error_message; | |
547 if (bindings->instance()->CanRemovePartitionAttribute(&error_message)) { | |
548 bindings->instance()->RemoveDOMAttribute(name()); | |
549 } else { | |
550 WebBindings::setException( | |
551 np_obj, static_cast<const NPUTF8 *>(error_message.c_str())); | |
552 } | |
553 } | |
554 private: | |
555 DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingPartition); | |
556 }; | |
557 | |
558 class BrowserPluginPropertyBindingSrc : public BrowserPluginPropertyBinding { | |
559 public: | |
560 BrowserPluginPropertyBindingSrc() | |
561 : BrowserPluginPropertyBinding(browser_plugin::kAttributeSrc) { | |
562 } | |
563 virtual bool GetProperty(BrowserPluginBindings* bindings, | |
564 NPVariant* result) OVERRIDE { | |
565 std::string src = bindings->instance()->GetSrcAttribute(); | |
566 return StringToNPVariant(src, result); | |
567 } | |
568 virtual bool SetProperty(BrowserPluginBindings* bindings, | |
569 NPObject* np_obj, | |
570 const NPVariant* variant) OVERRIDE { | |
571 std::string new_value = StringFromNPVariant(*variant); | |
572 // We should not be issuing navigation IPCs if we attempt to set the | |
573 // src property to the empty string. Instead, we want to simply restore | |
574 // the src attribute back to its old value. | |
575 if (new_value.empty()) { | |
576 return true; | |
577 } | |
578 std::string old_value = bindings->instance()->GetSrcAttribute(); | |
579 // If the new value was empty then we're effectively resetting the | |
580 // attribute to the old value here. This will be picked up by <webview>'s | |
581 // mutation observer and will restore the src attribute after it has been | |
582 // removed. | |
583 UpdateDOMAttribute(bindings, new_value); | |
584 std::string error_message; | |
585 if (!bindings->instance()->ParseSrcAttribute(&error_message)) { | |
586 // Reset to old value on error. | |
587 UpdateDOMAttribute(bindings, old_value); | |
588 // Exceptions must be set as the last operation before returning to | |
589 // script. | |
590 WebBindings::setException( | |
591 np_obj, static_cast<const NPUTF8 *>(error_message.c_str())); | |
592 return false; | |
593 } | |
594 return true; | |
595 } | |
596 virtual void RemoveProperty(BrowserPluginBindings* bindings, | |
597 NPObject* np_obj) OVERRIDE { | |
598 bindings->instance()->RemoveDOMAttribute(name()); | |
599 } | |
600 private: | |
601 DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingSrc); | |
602 }; | |
603 | |
604 | 501 |
605 // BrowserPluginBindings ------------------------------------------------------ | 502 // BrowserPluginBindings ------------------------------------------------------ |
606 | 503 |
607 BrowserPluginBindings::BrowserPluginNPObject::BrowserPluginNPObject() { | 504 BrowserPluginBindings::BrowserPluginNPObject::BrowserPluginNPObject() { |
608 } | 505 } |
609 | 506 |
610 BrowserPluginBindings::BrowserPluginNPObject::~BrowserPluginNPObject() { | 507 BrowserPluginBindings::BrowserPluginNPObject::~BrowserPluginNPObject() { |
611 } | 508 } |
612 | 509 |
613 BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance) | 510 BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance) |
614 : instance_(instance), | 511 : instance_(instance), |
615 np_object_(NULL), | 512 np_object_(NULL), |
616 weak_ptr_factory_(this) { | 513 weak_ptr_factory_(this) { |
617 NPObject* obj = | 514 NPObject* obj = |
618 WebBindings::createObject(instance->pluginNPP(), | 515 WebBindings::createObject(instance->pluginNPP(), |
619 &browser_plugin_message_class); | 516 &browser_plugin_message_class); |
620 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); | 517 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); |
621 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); | 518 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); |
622 | 519 |
623 method_bindings_.push_back(new BrowserPluginBindingAttach); | 520 method_bindings_.push_back(new BrowserPluginBindingAttach); |
624 | 521 |
625 property_bindings_.push_back( | 522 property_bindings_.push_back( |
626 new BrowserPluginPropertyBindingAllowTransparency); | 523 new BrowserPluginPropertyBindingAllowTransparency); |
627 property_bindings_.push_back(new BrowserPluginPropertyBindingAutoSize); | 524 property_bindings_.push_back(new BrowserPluginPropertyBindingAutoSize); |
628 property_bindings_.push_back(new BrowserPluginPropertyBindingContentWindow); | 525 property_bindings_.push_back(new BrowserPluginPropertyBindingContentWindow); |
629 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxHeight); | 526 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxHeight); |
630 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxWidth); | 527 property_bindings_.push_back(new BrowserPluginPropertyBindingMaxWidth); |
631 property_bindings_.push_back(new BrowserPluginPropertyBindingMinHeight); | 528 property_bindings_.push_back(new BrowserPluginPropertyBindingMinHeight); |
632 property_bindings_.push_back(new BrowserPluginPropertyBindingMinWidth); | 529 property_bindings_.push_back(new BrowserPluginPropertyBindingMinWidth); |
633 property_bindings_.push_back(new BrowserPluginPropertyBindingPartition); | |
634 property_bindings_.push_back(new BrowserPluginPropertyBindingSrc); | |
635 } | 530 } |
636 | 531 |
637 BrowserPluginBindings::~BrowserPluginBindings() { | 532 BrowserPluginBindings::~BrowserPluginBindings() { |
638 WebBindings::releaseObject(np_object_); | 533 WebBindings::releaseObject(np_object_); |
639 } | 534 } |
640 | 535 |
641 bool BrowserPluginBindings::HasMethod(NPIdentifier name) const { | 536 bool BrowserPluginBindings::HasMethod(NPIdentifier name) const { |
642 for (BindingList::const_iterator iter = method_bindings_.begin(); | 537 for (BindingList::const_iterator iter = method_bindings_.begin(); |
643 iter != method_bindings_.end(); | 538 iter != method_bindings_.end(); |
644 ++iter) { | 539 ++iter) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 for (PropertyBindingList::iterator iter = property_bindings_.begin(); | 599 for (PropertyBindingList::iterator iter = property_bindings_.begin(); |
705 iter != property_bindings_.end(); | 600 iter != property_bindings_.end(); |
706 ++iter) { | 601 ++iter) { |
707 if ((*iter)->MatchesName(name)) | 602 if ((*iter)->MatchesName(name)) |
708 return (*iter)->GetProperty(this, result); | 603 return (*iter)->GetProperty(this, result); |
709 } | 604 } |
710 return false; | 605 return false; |
711 } | 606 } |
712 | 607 |
713 } // namespace content | 608 } // namespace content |
OLD | NEW |