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 <cstdlib> |
| 8 #include <string> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "base/string16.h" |
| 14 #include "base/string_split.h" |
| 15 #include "base/utf_string_conversions.h" |
| 16 #include "content/renderer/browser_plugin/browser_plugin.h" |
| 17 #include "third_party/npapi/bindings/npapi.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMessageEvent.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSerialize
dScriptValue.h" |
| 26 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 27 #include "v8/include/v8.h" |
| 28 |
| 29 using WebKit::WebBindings; |
| 30 using WebKit::WebElement; |
| 31 using WebKit::WebDOMEvent; |
| 32 using WebKit::WebDOMMessageEvent; |
| 33 using WebKit::WebPluginContainer; |
| 34 using WebKit::WebSerializedScriptValue; |
| 35 using WebKit::WebString; |
| 36 |
| 37 namespace content { |
| 38 |
| 39 namespace browser_plugin { |
| 40 |
| 41 namespace { |
| 42 |
| 43 const char kAddEventListener[] = "addEventListener"; |
| 44 const char kRemoveEventListener[] = "removeEventListener"; |
| 45 const char kSrcAttribute[] = "src"; |
| 46 |
| 47 BrowserPluginBindings* GetBindings(NPObject* object) { |
| 48 return static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object)-> |
| 49 message_channel; |
| 50 } |
| 51 |
| 52 bool IdentifierIsAddEventListener(NPIdentifier identifier) { |
| 53 return WebBindings::getStringIdentifier(kAddEventListener) == identifier; |
| 54 } |
| 55 |
| 56 bool IdentifierIsRemoveEventListener(NPIdentifier identifier) { |
| 57 return WebBindings::getStringIdentifier(kRemoveEventListener) == identifier; |
| 58 } |
| 59 |
| 60 bool IdentifierIsSrcAttribute(NPIdentifier identifier) { |
| 61 return WebBindings::getStringIdentifier(kSrcAttribute) == identifier; |
| 62 } |
| 63 |
| 64 std::string StringFromNPVariant(const NPVariant& variant) { |
| 65 if (!NPVARIANT_IS_STRING(variant)) |
| 66 return std::string(); |
| 67 const NPString& np_string = NPVARIANT_TO_STRING(variant); |
| 68 return std::string(np_string.UTF8Characters, np_string.UTF8Length); |
| 69 } |
| 70 |
| 71 string16 String16FromNPVariant(const NPVariant& variant) { |
| 72 if (!NPVARIANT_IS_STRING(variant)) |
| 73 return string16(); |
| 74 const NPString& np_string = NPVARIANT_TO_STRING(variant); |
| 75 string16 wstr; |
| 76 if (!UTF8ToUTF16(np_string.UTF8Characters, np_string.UTF8Length, &wstr)) |
| 77 return string16(); |
| 78 return wstr; |
| 79 } |
| 80 |
| 81 bool StringToNPVariant(const std::string &in, NPVariant *variant) { |
| 82 size_t length = in.size(); |
| 83 NPUTF8 *chars = static_cast<NPUTF8 *>(malloc(length)); |
| 84 if (!chars) { |
| 85 VOID_TO_NPVARIANT(*variant); |
| 86 return false; |
| 87 } |
| 88 memcpy(chars, in.c_str(), length); |
| 89 STRINGN_TO_NPVARIANT(chars, length, *variant); |
| 90 return true; |
| 91 } |
| 92 |
| 93 //------------------------------------------------------------------------------ |
| 94 // Implementations of NPClass functions. These are here to: |
| 95 // - Implement src attribute. |
| 96 //------------------------------------------------------------------------------ |
| 97 NPObject* BrowserPluginBindingsAllocate(NPP npp, NPClass* the_class) { |
| 98 return new BrowserPluginBindings::BrowserPluginNPObject; |
| 99 } |
| 100 |
| 101 void BrowserPluginBindingsDeallocate(NPObject* object) { |
| 102 BrowserPluginBindings::BrowserPluginNPObject* instance = |
| 103 static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object); |
| 104 delete instance; |
| 105 } |
| 106 |
| 107 bool BrowserPluginBindingsHasMethod(NPObject* np_obj, NPIdentifier name) { |
| 108 if (!np_obj) |
| 109 return false; |
| 110 |
| 111 if (IdentifierIsAddEventListener(name)) |
| 112 return true; |
| 113 |
| 114 if (IdentifierIsRemoveEventListener(name)) |
| 115 return true; |
| 116 |
| 117 return false; |
| 118 } |
| 119 |
| 120 bool BrowserPluginBindingsInvoke(NPObject* np_obj, NPIdentifier name, |
| 121 const NPVariant* args, uint32 arg_count, |
| 122 NPVariant* result) { |
| 123 if (!np_obj) |
| 124 return false; |
| 125 |
| 126 BrowserPluginBindings* bindings = GetBindings(np_obj); |
| 127 if (!bindings) |
| 128 return false; |
| 129 |
| 130 if (IdentifierIsAddEventListener(name) && (arg_count == 2)) { |
| 131 std::string event_name = StringFromNPVariant(args[0]); |
| 132 if (event_name.empty()) |
| 133 return false; |
| 134 |
| 135 v8::Local<v8::Value> value = |
| 136 v8::Local<v8::Value>::New(WebBindings::toV8Value(&args[1])); |
| 137 if (value.IsEmpty() || !value->IsFunction()) |
| 138 return false; |
| 139 |
| 140 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); |
| 141 return bindings->instance()->AddEventListener(event_name, function); |
| 142 } |
| 143 |
| 144 if (IdentifierIsRemoveEventListener(name) && arg_count == 2) { |
| 145 std::string event_name = StringFromNPVariant(args[0]); |
| 146 if (event_name.empty()) |
| 147 return false; |
| 148 |
| 149 v8::Local<v8::Value> value = |
| 150 v8::Local<v8::Value>::New(WebBindings::toV8Value(&args[1])); |
| 151 |
| 152 if (value.IsEmpty() || !value->IsFunction()) |
| 153 return false; |
| 154 |
| 155 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); |
| 156 return bindings->instance()->RemoveEventListener(event_name, function); |
| 157 } |
| 158 |
| 159 return false; |
| 160 } |
| 161 |
| 162 bool BrowserPluginBindingsInvokeDefault(NPObject* np_obj, |
| 163 const NPVariant* args, |
| 164 uint32 arg_count, |
| 165 NPVariant* result) { |
| 166 NOTIMPLEMENTED(); |
| 167 return false; |
| 168 } |
| 169 |
| 170 bool BrowserPluginBindingsHasProperty(NPObject* np_obj, NPIdentifier name) { |
| 171 return IdentifierIsSrcAttribute(name); |
| 172 } |
| 173 |
| 174 bool BrowserPluginBindingsGetProperty(NPObject* np_obj, NPIdentifier name, |
| 175 NPVariant* result) { |
| 176 if (!np_obj) |
| 177 return false; |
| 178 |
| 179 if (IdentifierIsAddEventListener(name) || |
| 180 IdentifierIsRemoveEventListener(name)) |
| 181 return false; |
| 182 |
| 183 if (IdentifierIsSrcAttribute(name)) { |
| 184 BrowserPluginBindings* bindings = GetBindings(np_obj); |
| 185 if (!bindings) |
| 186 return false; |
| 187 std::string src = bindings->instance()->GetSrcAttribute(); |
| 188 return StringToNPVariant(src, result); |
| 189 } |
| 190 |
| 191 return false; |
| 192 } |
| 193 |
| 194 bool BrowserPluginBindingsSetProperty(NPObject* np_obj, NPIdentifier name, |
| 195 const NPVariant* variant) { |
| 196 if (!np_obj) |
| 197 return false; |
| 198 |
| 199 if (IdentifierIsSrcAttribute(name)) { |
| 200 std::string src = StringFromNPVariant(*variant); |
| 201 BrowserPluginBindings* bindings = GetBindings(np_obj); |
| 202 if (!bindings) |
| 203 return false; |
| 204 bindings->instance()->SetSrcAttribute(src); |
| 205 return true; |
| 206 } |
| 207 return false; |
| 208 } |
| 209 |
| 210 bool BrowserPluginBindingsEnumerate(NPObject *np_obj, NPIdentifier **value, |
| 211 uint32_t *count) { |
| 212 NOTIMPLEMENTED(); |
| 213 return true; |
| 214 } |
| 215 |
| 216 NPClass browser_plugin_message_class = { |
| 217 NP_CLASS_STRUCT_VERSION, |
| 218 &BrowserPluginBindingsAllocate, |
| 219 &BrowserPluginBindingsDeallocate, |
| 220 NULL, |
| 221 &BrowserPluginBindingsHasMethod, |
| 222 &BrowserPluginBindingsInvoke, |
| 223 &BrowserPluginBindingsInvokeDefault, |
| 224 &BrowserPluginBindingsHasProperty, |
| 225 &BrowserPluginBindingsGetProperty, |
| 226 &BrowserPluginBindingsSetProperty, |
| 227 NULL, |
| 228 &BrowserPluginBindingsEnumerate, |
| 229 }; |
| 230 |
| 231 } // namespace |
| 232 |
| 233 // BrowserPluginBindings ------------------------------------------------------ |
| 234 |
| 235 BrowserPluginBindings::BrowserPluginNPObject::BrowserPluginNPObject() { |
| 236 } |
| 237 |
| 238 BrowserPluginBindings::BrowserPluginNPObject::~BrowserPluginNPObject() { |
| 239 } |
| 240 |
| 241 BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance) |
| 242 : instance_(instance), |
| 243 np_object_(NULL), |
| 244 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 245 NPObject* obj = |
| 246 WebBindings::createObject(NULL, &browser_plugin_message_class); |
| 247 np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj); |
| 248 np_object_->message_channel = weak_ptr_factory_.GetWeakPtr(); |
| 249 } |
| 250 |
| 251 BrowserPluginBindings::~BrowserPluginBindings() { |
| 252 WebBindings::releaseObject(np_object_); |
| 253 } |
| 254 |
| 255 } // namespace browser_plugin |
| 256 } // namespace content |
OLD | NEW |