Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2231)

Unified Diff: content/renderer/browser_plugin/browser_plugin_bindings.cc

Issue 10830072: Browser Plugin: New Implementation (Renderer Side) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/renderer/browser_plugin/browser_plugin_bindings.cc
diff --git a/content/renderer/browser_plugin/browser_plugin_bindings.cc b/content/renderer/browser_plugin/browser_plugin_bindings.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8ae520746649993a7aa75cea9c155845c25cbbdf
--- /dev/null
+++ b/content/renderer/browser_plugin/browser_plugin_bindings.cc
@@ -0,0 +1,297 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/renderer/browser_plugin/browser_plugin_bindings.h"
+
+#include <cstdlib>
+#include <string>
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "base/message_loop.h"
+#include "base/string16.h"
+#include "base/string_split.h"
+#include "base/utf_string_conversions.h"
+#include "content/renderer/browser_plugin/browser_plugin.h"
+#include "third_party/npapi/bindings/npapi.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMMessageEvent.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSerializedScriptValue.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
+#include "v8/include/v8.h"
+
+using WebKit::WebBindings;
+using WebKit::WebElement;
+using WebKit::WebDOMEvent;
+using WebKit::WebDOMMessageEvent;
+using WebKit::WebPluginContainer;
+using WebKit::WebSerializedScriptValue;
+using WebKit::WebString;
+
+namespace content {
+
+namespace browser_plugin {
+
+namespace {
+
+const char kAddEventListener[] = "addEventListener";
+const char kRemoveEventListener[] = "removeEventListener";
+const char kPostMessage[] = "postMessage";
+const char kSrcAttribute[] = "src";
+
+BrowserPluginBindings* GetBindings(NPObject* object) {
+ return static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object)->
+ message_channel;
+}
+
+bool IdentifierIsAddEventListener(NPIdentifier identifier) {
+ return WebBindings::getStringIdentifier(kAddEventListener) == identifier;
+}
+
+bool IdentifierIsRemoveEventListener(NPIdentifier identifier) {
+ return WebBindings::getStringIdentifier(kRemoveEventListener) == identifier;
+}
+
+bool IdentifierIsPostMessage(NPIdentifier identifier) {
+ return WebBindings::getStringIdentifier(kPostMessage) == identifier;
+}
+
+bool IdentifierIsSrcAttribute(NPIdentifier identifier) {
+ return WebBindings::getStringIdentifier(kSrcAttribute) == identifier;
+}
+
+std::string StringFromNPVariant(const NPVariant& variant) {
+ if (!NPVARIANT_IS_STRING(variant))
+ return std::string();
+ const NPString& np_string = NPVARIANT_TO_STRING(variant);
+ return std::string(np_string.UTF8Characters, np_string.UTF8Length);
+}
+
+string16 String16FromNPVariant(const NPVariant& variant) {
+ if (!NPVARIANT_IS_STRING(variant))
+ return string16();
+ const NPString& np_string = NPVARIANT_TO_STRING(variant);
+ string16 wstr;
+ if (!UTF8ToUTF16(np_string.UTF8Characters, np_string.UTF8Length, &wstr))
+ return string16();
+ return wstr;
+}
+
+bool StringToNPVariant(const std::string &in, NPVariant *variant) {
+ size_t length = in.size();
+ NPUTF8 *chars = static_cast<NPUTF8 *>(malloc(length));
+ if (!chars) {
+ VOID_TO_NPVARIANT(*variant);
+ return false;
+ }
+ memcpy(chars, in.c_str(), length);
+ STRINGN_TO_NPVARIANT(chars, length, *variant);
+ return true;
+}
+
+//------------------------------------------------------------------------------
+// Implementations of NPClass functions. These are here to:
+// - Implement postMessage behavior.
+// - Implement src attribute.
+//------------------------------------------------------------------------------
+NPObject* BrowserPluginBindingsAllocate(NPP npp, NPClass* the_class) {
+ return new BrowserPluginBindings::BrowserPluginNPObject;
+}
+
+void BrowserPluginBindingsDeallocate(NPObject* object) {
+ BrowserPluginBindings::BrowserPluginNPObject* instance =
+ static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(object);
+ delete instance;
+}
+
+bool BrowserPluginBindingsHasMethod(NPObject* np_obj, NPIdentifier name) {
+ if (!np_obj)
+ return false;
+
+ if (IdentifierIsPostMessage(name))
+ return true;
+
+ if (IdentifierIsAddEventListener(name))
+ return true;
+
+ if (IdentifierIsRemoveEventListener(name))
+ return true;
+
+ return false;
+}
+
+bool BrowserPluginBindingsInvoke(NPObject* np_obj, NPIdentifier name,
+ const NPVariant* args, uint32 arg_count,
+ NPVariant* result) {
+ if (!np_obj)
+ return false;
+
+ BrowserPluginBindings* bindings = GetBindings(np_obj);
+ if (!bindings)
+ return false;
+
+ if (IdentifierIsPostMessage(name) && (arg_count == 2)) {
Charlie Reis 2012/08/03 18:06:02 Is this a typical pattern for bindings classes, or
Fady Samuel 2012/08/03 19:34:43 I agree, this will grow long fairly quickly as the
+ v8::Handle<v8::Value> v8_val = WebBindings::toV8Value(&args[0]);
+ // If serialization fails, there may be a V8 exception pending.
+ // Don't touch the V8 state (eg, to raise our own 'error calling
+ // method on NPObject' exception).
+ if (v8_val.IsEmpty())
+ // This might happen if toV8Value fails an allocation.
+ // Does SafeAllocation::newInstance throw an exception in this case?
+ return true;
+ WebSerializedScriptValue serialized_v8_val =
+ WebSerializedScriptValue::serialize(v8_val);
+ WebString message_string = serialized_v8_val.toString();
+ // Serialization can sometimes fail (the user might try to fax the
+ // |window|, for example); ::serialize will result in a
+ // DATA_CLONE_ERR being raised, but we still need to abort
+ // the postMessage() operation. There's no clear way to test
+ // for failure in WebSerializedScriptValue, but an empty string
+ // should signal an invalid object. (The serializer at a nonzero
+ // version will always write a version tag.)
+ // v8::TryCatch, for some reason, was not catching DATA_CLONE_ERR;
+ // if this worked, it would be the right way to go.
+ // (This isn't the only way that serialization might fail! Other
+ // exceptions are possible.)
+ if (!message_string.length())
+ return true;
+ string16 message(message_string);
+ string16 target_origin = String16FromNPVariant(args[1]);
+ bindings->instance()->PostMessage(message, target_origin);
Charlie Reis 2012/08/03 18:06:02 Just to confirm my understanding, this allows JS c
Fady Samuel 2012/08/03 19:34:43 That is correct.
+ return true;
+ }
+
+ if (IdentifierIsAddEventListener(name) && (arg_count == 2)) {
+ std::string event_name = StringFromNPVariant(args[0]);
+ if (event_name.empty())
+ return false;
+
+ v8::Local<v8::Value> value =
+ v8::Local<v8::Value>::New(WebBindings::toV8Value(&args[1]));
+ if (value.IsEmpty() || !value->IsFunction())
+ return false;
+
+ v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value);
+ return bindings->instance()->AddEventListener(event_name, function);
+ }
+
+ if (IdentifierIsRemoveEventListener(name) && arg_count == 2) {
+ std::string event_name = StringFromNPVariant(args[0]);
+ if (event_name.empty())
+ return false;
+
+ v8::Local<v8::Value> value =
+ v8::Local<v8::Value>::New(WebBindings::toV8Value(&args[1]));
+
+ if (value.IsEmpty() || !value->IsFunction())
+ return false;
+
+ v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value);
+ return bindings->instance()->RemoveEventListener(event_name, function);
+ }
+
+ return false;
+}
+
+bool BrowserPluginBindingsInvokeDefault(NPObject* np_obj,
+ const NPVariant* args,
+ uint32 arg_count,
+ NPVariant* result) {
+ NOTIMPLEMENTED();
+ return false;
+}
+
+bool BrowserPluginBindingsHasProperty(NPObject* np_obj, NPIdentifier name) {
+ return IdentifierIsSrcAttribute(name);
+}
+
+bool BrowserPluginBindingsGetProperty(NPObject* np_obj, NPIdentifier name,
+ NPVariant* result) {
+ if (!np_obj)
+ return false;
+
+ if (IdentifierIsPostMessage(name) ||
+ IdentifierIsAddEventListener(name) ||
+ IdentifierIsRemoveEventListener(name))
+ return false;
+
+ if (IdentifierIsSrcAttribute(name)) {
+ BrowserPluginBindings* bindings = GetBindings(np_obj);
+ if (!bindings)
+ return false;
+ std::string src = bindings->instance()->GetSrcAttribute();
+ return StringToNPVariant(src, result);
+ }
+
+ return false;
+}
+
+bool BrowserPluginBindingsSetProperty(NPObject* np_obj, NPIdentifier name,
+ const NPVariant* variant) {
+ if (!np_obj)
+ return false;
+
+ if (IdentifierIsSrcAttribute(name)) {
+ std::string src = StringFromNPVariant(*variant);
+ BrowserPluginBindings* bindings = GetBindings(np_obj);
+ if (!bindings)
+ return false;
+ bindings->instance()->SetSrcAttribute(src);
+ return true;
+ }
+ return false;
+}
+
+bool BrowserPluginBindingsEnumerate(NPObject *np_obj, NPIdentifier **value,
+ uint32_t *count) {
+ NOTIMPLEMENTED();
+ return true;
+}
+
+NPClass browser_plugin_message_class = {
+ NP_CLASS_STRUCT_VERSION,
+ &BrowserPluginBindingsAllocate,
+ &BrowserPluginBindingsDeallocate,
+ NULL,
+ &BrowserPluginBindingsHasMethod,
+ &BrowserPluginBindingsInvoke,
+ &BrowserPluginBindingsInvokeDefault,
+ &BrowserPluginBindingsHasProperty,
+ &BrowserPluginBindingsGetProperty,
+ &BrowserPluginBindingsSetProperty,
+ NULL,
+ &BrowserPluginBindingsEnumerate,
+};
+
+} // namespace
+
+// BrowserPluginBindings ------------------------------------------------------
+
+BrowserPluginBindings::BrowserPluginNPObject::BrowserPluginNPObject() {
+}
+
+BrowserPluginBindings::BrowserPluginNPObject::~BrowserPluginNPObject() {
+}
+
+BrowserPluginBindings::BrowserPluginBindings(BrowserPlugin* instance)
+ : instance_(instance),
+ np_object_(NULL),
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
+ NPObject* obj =
+ WebBindings::createObject(NULL, &browser_plugin_message_class);
+ np_object_ = static_cast<BrowserPluginBindings::BrowserPluginNPObject*>(obj);
+ np_object_->message_channel = weak_ptr_factory_.GetWeakPtr();
+}
+
+BrowserPluginBindings::~BrowserPluginBindings() {
+ WebBindings::releaseObject(np_object_);
+}
+
+} // namespace browser_plugin
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698