| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/browser_plugin/browser_plugin_bindings.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "content/common/browser_plugin/browser_plugin_constants.h" | |
| 10 #include "content/renderer/browser_plugin/browser_plugin.h" | |
| 11 #include "third_party/WebKit/public/web/WebBindings.h" | |
| 12 #include "third_party/npapi/bindings/npapi.h" | |
| 13 | |
| 14 using blink::WebBindings; | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 BrowserPluginBindings* GetBindings(NPObject* object) { | |
| 21 return static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object)-> | |
| 22 message_channel.get(); | |
| 23 } | |
| 24 | |
| 25 std::string StringFromNPVariant(const NPVariant& variant) { | |
| 26 if (!NPVARIANT_IS_STRING(variant)) | |
| 27 return std::string(); | |
| 28 const NPString& np_string = NPVARIANT_TO_STRING(variant); | |
| 29 return std::string(np_string.UTF8Characters, np_string.UTF8Length); | |
| 30 } | |
| 31 | |
| 32 //------------------------------------------------------------------------------ | |
| 33 // Implementations of NPClass functions. These are here to: | |
| 34 // - Implement src attribute. | |
| 35 //------------------------------------------------------------------------------ | |
| 36 NPObject* BrowserPluginBindingsAllocate(NPP npp, NPClass* the_class) { | |
| 37 return new BrowserPluginBindings::BrowserPluginNPObject; | |
| 38 } | |
| 39 | |
| 40 void BrowserPluginBindingsDeallocate(NPObject* object) { | |
| 41 BrowserPluginBindings::BrowserPluginNPObject* instance = | |
| 42 static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object); | |
| 43 delete instance; | |
| 44 } | |
| 45 | |
| 46 bool BrowserPluginBindingsHasMethod(NPObject* np_obj, NPIdentifier name) { | |
| 47 return false; | |
| 48 } | |
| 49 | |
| 50 bool BrowserPluginBindingsInvokeDefault(NPObject* np_obj, | |
| 51 const NPVariant* args, | |
| 52 uint32 arg_count, | |
| 53 NPVariant* result) { | |
| 54 NOTIMPLEMENTED(); | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 bool BrowserPluginBindingsHasProperty(NPObject* np_obj, NPIdentifier name) { | |
| 59 if (!np_obj) | |
| 60 return false; | |
| 61 | |
| 62 BrowserPluginBindings* bindings = GetBindings(np_obj); | |
| 63 if (!bindings) | |
| 64 return false; | |
| 65 | |
| 66 return bindings->HasProperty(name); | |
| 67 } | |
| 68 | |
| 69 bool BrowserPluginBindingsGetProperty(NPObject* np_obj, NPIdentifier name, | |
| 70 NPVariant* result) { | |
| 71 if (!np_obj) | |
| 72 return false; | |
| 73 | |
| 74 if (!result) | |
| 75 return false; | |
| 76 | |
| 77 // All attributes from here on rely on the bindings, so retrieve it once and | |
| 78 // return on failure. | |
| 79 BrowserPluginBindings* bindings = GetBindings(np_obj); | |
| 80 if (!bindings) | |
| 81 return false; | |
| 82 | |
| 83 return bindings->GetProperty(name, result); | |
| 84 } | |
| 85 | |
| 86 bool BrowserPluginBindingsSetProperty(NPObject* np_obj, NPIdentifier name, | |
| 87 const NPVariant* variant) { | |
| 88 if (!np_obj) | |
| 89 return false; | |
| 90 if (!variant) | |
| 91 return false; | |
| 92 | |
| 93 // All attributes from here on rely on the bindings, so retrieve it once and | |
| 94 // return on failure. | |
| 95 BrowserPluginBindings* bindings = GetBindings(np_obj); | |
| 96 if (!bindings) | |
| 97 return false; | |
| 98 | |
| 99 if (variant->type == NPVariantType_Null) | |
| 100 return bindings->RemoveProperty(np_obj, name); | |
| 101 | |
| 102 return bindings->SetProperty(np_obj, name, variant); | |
| 103 } | |
| 104 | |
| 105 bool BrowserPluginBindingsEnumerate(NPObject *np_obj, NPIdentifier **value, | |
| 106 uint32_t *count) { | |
| 107 NOTIMPLEMENTED(); | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 NPClass browser_plugin_message_class = { | |
| 112 NP_CLASS_STRUCT_VERSION, | |
| 113 &BrowserPluginBindingsAllocate, | |
| 114 &BrowserPluginBindingsDeallocate, | |
| 115 NULL, | |
| 116 &BrowserPluginBindingsHasMethod, | |
| 117 NULL, | |
| 118 &BrowserPluginBindingsInvokeDefault, | |
| 119 &BrowserPluginBindingsHasProperty, | |
| 120 &BrowserPluginBindingsGetProperty, | |
| 121 &BrowserPluginBindingsSetProperty, | |
| 122 NULL, | |
| 123 &BrowserPluginBindingsEnumerate, | |
| 124 }; | |
| 125 | |
| 126 } // namespace | |
| 127 | |
| 128 // BrowserPluginPropertyBinding ------------------------------------------------ | |
| 129 | |
| 130 class BrowserPluginPropertyBinding { | |
| 131 public: | |
| 132 explicit BrowserPluginPropertyBinding(const char name[]) : name_(name) {} | |
| 133 virtual ~BrowserPluginPropertyBinding() {} | |
| 134 const std::string& name() const { return name_; } | |
| 135 bool MatchesName(NPIdentifier name) const { | |
| 136 return WebBindings::getStringIdentifier(name_.c_str()) == name; | |
| 137 } | |
| 138 virtual bool GetProperty(BrowserPluginBindings* bindings, | |
| 139 NPVariant* result) = 0; | |
| 140 virtual bool SetProperty(BrowserPluginBindings* bindings, | |
| 141 NPObject* np_obj, | |
| 142 const NPVariant* variant) = 0; | |
| 143 virtual void RemoveProperty(BrowserPluginBindings* bindings, | |
| 144 NPObject* np_obj) = 0; | |
| 145 // Updates the DOM Attribute value with the current property value. | |
| 146 void UpdateDOMAttribute(BrowserPluginBindings* bindings, | |
| 147 std::string new_value) { | |
| 148 bindings->instance()->UpdateDOMAttribute(name(), new_value); | |
| 149 } | |
| 150 private: | |
| 151 std::string name_; | |
| 152 | |
| 153 DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBinding); | |
| 154 }; | |
| 155 | |
| 156 class BrowserPluginPropertyBindingAllowTransparency | |
| 157 : public BrowserPluginPropertyBinding { | |
| 158 public: | |
| 159 BrowserPluginPropertyBindingAllowTransparency() | |
| 160 : BrowserPluginPropertyBinding( | |
| 161 browser_plugin::kAttributeAllowTransparency) { | |
| 162 } | |
| 163 virtual bool GetProperty(BrowserPluginBindings* bindings, | |
| 164 NPVariant* result) OVERRIDE { | |
| 165 bool allow_transparency = | |
| 166 bindings->instance()->GetAllowTransparencyAttribute(); | |
| 167 BOOLEAN_TO_NPVARIANT(allow_transparency, *result); | |
| 168 return true; | |
| 169 } | |
| 170 virtual bool SetProperty(BrowserPluginBindings* bindings, | |
| 171 NPObject* np_obj, | |
| 172 const NPVariant* variant) OVERRIDE { | |
| 173 std::string value = StringFromNPVariant(*variant); | |
| 174 if (!bindings->instance()->HasDOMAttribute(name())) { | |
| 175 UpdateDOMAttribute(bindings, value); | |
| 176 bindings->instance()->ParseAllowTransparencyAttribute(); | |
| 177 } else { | |
| 178 UpdateDOMAttribute(bindings, value); | |
| 179 } | |
| 180 return true; | |
| 181 } | |
| 182 virtual void RemoveProperty(BrowserPluginBindings* bindings, | |
| 183 NPObject* np_obj) OVERRIDE { | |
| 184 bindings->instance()->RemoveDOMAttribute(name()); | |
| 185 bindings->instance()->ParseAllowTransparencyAttribute(); | |
| 186 } | |
| 187 private: | |
| 188 DISALLOW_COPY_AND_ASSIGN(BrowserPluginPropertyBindingAllowTransparency); | |
| 189 }; | |
| 190 | |
| 191 | |
| 192 // BrowserPluginBindings ------------------------------------------------------ | |
| 193 | |
| 194 BrowserPluginBindings::BrowserPluginNPObject::BrowserPluginNPObject() { | |
| 195 } | |
| 196 | |
| 197 BrowserPluginBindings::BrowserPluginNPObject::~BrowserPluginNPObject() { | |
| 198 } | |
| 199 | |
| 200 BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance) | |
| 201 : instance_(instance), | |
| 202 np_object_(NULL), | |
| 203 weak_ptr_factory_(this) { | |
| 204 NPObject* obj = | |
| 205 WebBindings::createObject(instance->pluginNPP(), | |
| 206 &browser_plugin_message_class); | |
| 207 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); | |
| 208 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); | |
| 209 | |
| 210 property_bindings_.push_back( | |
| 211 new BrowserPluginPropertyBindingAllowTransparency); | |
| 212 } | |
| 213 | |
| 214 BrowserPluginBindings::~BrowserPluginBindings() { | |
| 215 WebBindings::releaseObject(np_object_); | |
| 216 } | |
| 217 | |
| 218 bool BrowserPluginBindings::HasProperty(NPIdentifier name) const { | |
| 219 for (PropertyBindingList::const_iterator iter = property_bindings_.begin(); | |
| 220 iter != property_bindings_.end(); | |
| 221 ++iter) { | |
| 222 if ((*iter)->MatchesName(name)) | |
| 223 return true; | |
| 224 } | |
| 225 return false; | |
| 226 } | |
| 227 | |
| 228 bool BrowserPluginBindings::SetProperty(NPObject* np_obj, | |
| 229 NPIdentifier name, | |
| 230 const NPVariant* variant) { | |
| 231 for (PropertyBindingList::iterator iter = property_bindings_.begin(); | |
| 232 iter != property_bindings_.end(); | |
| 233 ++iter) { | |
| 234 if ((*iter)->MatchesName(name)) { | |
| 235 if ((*iter)->SetProperty(this, np_obj, variant)) { | |
| 236 return true; | |
| 237 } | |
| 238 break; | |
| 239 } | |
| 240 } | |
| 241 return false; | |
| 242 } | |
| 243 | |
| 244 bool BrowserPluginBindings::RemoveProperty(NPObject* np_obj, | |
| 245 NPIdentifier name) { | |
| 246 for (PropertyBindingList::iterator iter = property_bindings_.begin(); | |
| 247 iter != property_bindings_.end(); | |
| 248 ++iter) { | |
| 249 if ((*iter)->MatchesName(name)) { | |
| 250 (*iter)->RemoveProperty(this, np_obj); | |
| 251 return true; | |
| 252 } | |
| 253 } | |
| 254 return false; | |
| 255 } | |
| 256 | |
| 257 bool BrowserPluginBindings::GetProperty(NPIdentifier name, NPVariant* result) { | |
| 258 for (PropertyBindingList::iterator iter = property_bindings_.begin(); | |
| 259 iter != property_bindings_.end(); | |
| 260 ++iter) { | |
| 261 if ((*iter)->MatchesName(name)) | |
| 262 return (*iter)->GetProperty(this, result); | |
| 263 } | |
| 264 return false; | |
| 265 } | |
| 266 | |
| 267 } // namespace content | |
| OLD | NEW |