Chromium Code Reviews| Index: content/renderer/browser_plugin/browser_plugin.cc |
| diff --git a/content/renderer/browser_plugin/browser_plugin.cc b/content/renderer/browser_plugin/browser_plugin.cc |
| index f6234bf1c6459f047d64d428c29b9228cfc99165..75a7a30fbec4aaf564e1dbf120f09be164c9fc3e 100644 |
| --- a/content/renderer/browser_plugin/browser_plugin.cc |
| +++ b/content/renderer/browser_plugin/browser_plugin.cc |
| @@ -18,6 +18,7 @@ |
| #include "content/renderer/browser_plugin/browser_plugin_manager.h" |
| #include "content/renderer/render_process_impl.h" |
| #include "content/renderer/render_thread_impl.h" |
| +#include "content/renderer/v8_value_converter_impl.h" |
| #include "skia/ext/platform_canvas.h" |
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| @@ -27,6 +28,7 @@ |
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginParams.h" |
| #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" |
| #include "webkit/plugins/sad_plugin.h" |
| using WebKit::WebCanvas; |
| @@ -55,7 +57,7 @@ const char kPartitionAttribute[] = "partition"; |
| const char kPersistPrefix[] = "persist:"; |
| const char kProcessId[] = "processId"; |
| const char kSrcAttribute[] = "src"; |
| -const char kType[] = "type"; |
| +const char kReason[] = "reason"; |
| const char kURL[] = "url"; |
| static std::string TerminationStatusToString(base::TerminationStatus status) { |
| @@ -105,15 +107,12 @@ BrowserPlugin::BrowserPlugin( |
| BrowserPluginManager::Get()->AddBrowserPlugin(instance_id, this); |
| bindings_.reset(new BrowserPluginBindings(this)); |
| - InitializeEvents(); |
| - |
| ParseAttributes(params); |
| } |
| BrowserPlugin::~BrowserPlugin() { |
| if (damage_buffer_) |
| FreeDamageBuffer(); |
| - RemoveEventListeners(); |
| BrowserPluginManager::Get()->RemoveBrowserPlugin(instance_id_); |
| BrowserPluginManager::Get()->Send( |
| new BrowserPluginHostMsg_PluginDestroyed( |
| @@ -313,66 +312,43 @@ float BrowserPlugin::GetDeviceScaleFactor() const { |
| return render_view_->GetWebView()->deviceScaleFactor(); |
| } |
| -void BrowserPlugin::InitializeEvents() { |
| - event_listener_map_[kExitEventName] = EventListeners(); |
| - event_listener_map_[kLoadAbortEventName] = EventListeners(); |
| - event_listener_map_[kLoadCommitEventName] = EventListeners(); |
| - event_listener_map_[kLoadRedirectEventName] = EventListeners(); |
| - event_listener_map_[kLoadStartEventName] = EventListeners(); |
| - event_listener_map_[kLoadStopEventName] = EventListeners(); |
| -} |
| - |
| -void BrowserPlugin::RemoveEventListeners() { |
| - EventListenerMap::iterator event_listener_map_iter = |
| - event_listener_map_.begin(); |
| - for (; event_listener_map_iter != event_listener_map_.end(); |
| - ++event_listener_map_iter) { |
| - EventListeners& listeners = |
| - event_listener_map_[event_listener_map_iter->first]; |
| - EventListeners::iterator it = listeners.begin(); |
| - for (; it != listeners.end(); ++it) { |
| - it->Dispose(); |
| +void BrowserPlugin::TriggerEvent(const std::string& event_name, |
| + std::map<std::string, base::Value*>* props) { |
| + WebKit::WebElement plugin = container()->element(); |
| + v8::HandleScope handle_scope; |
| + v8::Local<v8::Context> context = |
| + plugin.document().frame()->mainWorldScriptContext(); |
|
abarth-chromium
2012/11/05 19:07:15
Why is the main world the correct world?
sadrul
2012/11/06 03:23:34
The current code uses the main-world context, so I
|
| + v8::Context::Scope context_scope(context); |
| + std::string script = base::StringPrintf("new CustomEvent('%s', {" |
| + " 'bubbles': false, " |
| + " 'cancelable': false" |
| + "});", event_name.c_str()); |
|
abarth-chromium
2012/11/05 19:07:15
Where does event_name come from? Could this lead
sadrul
2012/11/06 03:23:34
I have changed this code to use WebDOMCustomEvent
|
| + v8::Local<v8::Value> value = v8::Local<v8::Value>::New( |
| + plugin.document().frame()->executeScriptAndReturnValue( |
| + WebKit::WebScriptSource(WebKit::WebString::fromUTF8(script.c_str())))); |
| + v8::Local<v8::Object> event = value->ToObject(); |
|
abarth-chromium
2012/11/05 19:07:15
Why not use the WebKit API to create a CustomEvent
sadrul
2012/11/06 03:23:34
Done: https://bugs.webkit.org/show_bug.cgi?id=1012
|
| + |
| + if (props) { |
| + V8ValueConverterImpl converter; |
| + for (std::map<std::string, base::Value*>::iterator iter = props->begin(), |
| + end = props->end(); iter != end; ++iter) { |
| + event->Set(v8::String::New(iter->first.data(), iter->first.size()), |
| + converter.ToV8Value(iter->second, context), |
| + v8::ReadOnly); |
|
abarth-chromium
2012/11/05 19:07:15
This operation can trigger JavaScript setters, whi
|
| } |
| + STLDeleteValues(props); |
| } |
| - event_listener_map_.clear(); |
| -} |
| - |
| -bool BrowserPlugin::IsValidEvent(const std::string& event_name) { |
| - return event_listener_map_.find(event_name) != event_listener_map_.end(); |
| -} |
| -void BrowserPlugin::TriggerEvent(const std::string& event_name, |
| - v8::Local<v8::Object>* event) { |
| - WebKit::WebElement plugin = container()->element(); |
| + NPVariant npevent, npresult; |
| + WebKit::WebBindings::toNPVariant(event, NULL, &npevent); |
| - const EventListeners& listeners = event_listener_map_[event_name.c_str()]; |
| - // A v8::Local copy of the listeners is created from the v8::Persistent |
| - // listeners before firing them. This is to ensure that if one of these |
| - // listeners mutate the list of listeners (by calling |
| - // addEventListener/removeEventListener), this local copy is not affected. |
| - // This means if you mutate the list of listeners for an event X while event X |
| - // is firing, the mutation is deferred until all current listeners for X have |
| - // fired. |
| - EventListenersLocal listeners_local; |
| - listeners_local.reserve(listeners.size()); |
| - for (EventListeners::const_iterator it = listeners.begin(); |
| - it != listeners.end(); |
| - ++it) { |
| - listeners_local.push_back(v8::Local<v8::Function>::New(*it)); |
| - } |
| + NPIdentifier method = |
| + WebKit::WebBindings::getStringIdentifier("dispatchEvent"); |
| + WebKit::WebBindings::invoke(NULL, container()->scriptableObjectForElement(), |
| + method, &npevent, 1, &npresult); |
| - (*event)->Set(v8::String::New("name"), |
| - v8::String::New(event_name.c_str(), event_name.size()), |
| - v8::ReadOnly); |
| - v8::Local<v8::Value> argv[] = { *event }; |
| - for (EventListenersLocal::const_iterator it = listeners_local.begin(); |
| - it != listeners_local.end(); |
| - ++it) { |
| - WebKit::WebFrame* frame = plugin.document().frame(); |
| - if (!frame) |
| - break; |
| - frame->callFunctionEvenIfScriptDisabled(*it, v8::Object::New(), 1, argv); |
| - } |
| + WebKit::WebBindings::releaseVariantValue(&npevent); |
| + WebKit::WebBindings::releaseVariantValue(&npresult); |
| } |
| void BrowserPlugin::Back() { |
| @@ -475,28 +451,17 @@ void BrowserPlugin::UpdateRect( |
| void BrowserPlugin::GuestGone(int process_id, base::TerminationStatus status) { |
| // We fire the event listeners before painting the sad graphic to give the |
| // developer an opportunity to display an alternative overlay image on crash. |
| - if (HasListeners(kExitEventName)) { |
| - WebKit::WebElement plugin = container()->element(); |
| - v8::HandleScope handle_scope; |
| - v8::Context::Scope context_scope( |
| - plugin.document().frame()->mainWorldScriptContext()); |
| - |
| - // Construct the exit event object. |
| - v8::Local<v8::Object> event = v8::Object::New(); |
| - event->Set(v8::String::New(kProcessId, sizeof(kProcessId) - 1), |
| - v8::Integer::New(process_id), |
| - v8::ReadOnly); |
| - std::string termination_status = TerminationStatusToString(status); |
| - event->Set(v8::String::New(kType, sizeof(kType) - 1), |
| - v8::String::New(termination_status.data(), |
| - termination_status.size()), |
| - v8::ReadOnly); |
| - // Event listeners may remove the BrowserPlugin from the document. If that |
| - // happens, the BrowserPlugin will be scheduled for later deletion (see |
| - // BrowserPlugin::destroy()). That will clear the container_ reference, |
| - // but leave other member variables valid below. |
| - TriggerEvent(kExitEventName, &event); |
| - } |
| + std::string termination_status = TerminationStatusToString(status); |
| + std::map<std::string, base::Value*> props; |
| + props[kProcessId] = base::Value::CreateIntegerValue(process_id); |
| + props[kReason] = base::Value::CreateStringValue(termination_status); |
| + |
| + // Event listeners may remove the BrowserPlugin from the document. If that |
| + // happens, the BrowserPlugin will be scheduled for later deletion (see |
| + // BrowserPlugin::destroy()). That will clear the container_ reference, |
| + // but leave other member variables valid below. |
| + TriggerEvent(kExitEventName, &props); |
|
abarth-chromium
2012/11/05 19:07:15
It looks like you're using constants. Can we pass
|
| + |
| guest_crashed_ = true; |
| // We won't paint the contents of the current backing store again so we might |
| // as well toss it out and save memory. |
| @@ -508,23 +473,11 @@ void BrowserPlugin::GuestGone(int process_id, base::TerminationStatus status) { |
| } |
| void BrowserPlugin::LoadStart(const GURL& url, bool is_top_level) { |
| - if (!HasListeners(kLoadStartEventName)) |
| - return; |
| - |
| - WebKit::WebElement plugin = container()->element(); |
| - v8::HandleScope handle_scope; |
| - v8::Context::Scope context_scope( |
| - plugin.document().frame()->mainWorldScriptContext()); |
| + std::map<std::string, base::Value*> props; |
| + props[kURL] = base::Value::CreateStringValue(url.spec()); |
| + props[kIsTopLevel] = base::Value::CreateBooleanValue(is_top_level); |
| - // Construct the loadStart event object. |
| - v8::Local<v8::Object> event = v8::Object::New(); |
| - event->Set(v8::String::New(kURL, sizeof(kURL) - 1), |
| - v8::String::New(url.spec().data(), url.spec().size()), |
| - v8::ReadOnly); |
| - event->Set(v8::String::New(kIsTopLevel, sizeof(kIsTopLevel) - 1), |
| - v8::Boolean::New(is_top_level), |
| - v8::ReadOnly); |
| - TriggerEvent(kLoadStartEventName, &event); |
| + TriggerEvent(kLoadStartEventName, &props); |
| } |
| void BrowserPlugin::LoadCommit( |
| @@ -537,90 +490,35 @@ void BrowserPlugin::LoadCommit( |
| current_nav_entry_index_ = params.current_entry_index; |
| nav_entry_count_ = params.entry_count; |
| - if (!HasListeners(kLoadCommitEventName)) |
| - return; |
| - |
| - WebKit::WebElement plugin = container()->element(); |
| - v8::HandleScope handle_scope; |
| - v8::Context::Scope context_scope( |
| - plugin.document().frame()->mainWorldScriptContext()); |
| - |
| - // Construct the loadCommit event object. |
| - v8::Local<v8::Object> event = v8::Object::New(); |
| - event->Set(v8::String::New(kURL, sizeof(kURL) - 1), |
| - v8::String::New(src_.data(), src_.size()), |
| - v8::ReadOnly); |
| - event->Set(v8::String::New(kIsTopLevel, sizeof(kIsTopLevel) - 1), |
| - v8::Boolean::New(params.is_top_level), |
| - v8::ReadOnly); |
| - |
| - TriggerEvent(kLoadCommitEventName, &event); |
| + std::map<std::string, base::Value*> props; |
| + props[kURL] = base::Value::CreateStringValue(src_); |
| + props[kIsTopLevel] = base::Value::CreateBooleanValue(params.is_top_level); |
| + TriggerEvent(kLoadCommitEventName, &props); |
| } |
| void BrowserPlugin::LoadStop() { |
| - if (!HasListeners(kLoadStopEventName)) |
| - return; |
| - |
| - WebKit::WebElement plugin = container()->element(); |
| - v8::HandleScope handle_scope; |
| - v8::Context::Scope context_scope( |
| - plugin.document().frame()->mainWorldScriptContext()); |
| - |
| // Construct the loadStop event object. |
| - v8::Local<v8::Object> event = v8::Object::New(); |
| - TriggerEvent(kLoadStopEventName, &event); |
| + TriggerEvent(kLoadStopEventName, NULL); |
| } |
| void BrowserPlugin::LoadAbort(const GURL& url, |
| bool is_top_level, |
| const std::string& type) { |
| - if (!HasListeners(kLoadAbortEventName)) |
| - return; |
| - |
| - WebKit::WebElement plugin = container()->element(); |
| - v8::HandleScope handle_scope; |
| - v8::Context::Scope context_scope( |
| - plugin.document().frame()->mainWorldScriptContext()); |
| - |
| - // Construct the loadAbort event object. |
| - v8::Local<v8::Object> event = v8::Object::New(); |
| - event->Set(v8::String::New(kURL, sizeof(kURL) - 1), |
| - v8::String::New(url.spec().data(), url.spec().size()), |
| - v8::ReadOnly); |
| - event->Set(v8::String::New(kIsTopLevel, sizeof(kIsTopLevel) - 1), |
| - v8::Boolean::New(is_top_level), |
| - v8::ReadOnly); |
| - event->Set(v8::String::New(kType, sizeof(kType) - 1), |
| - v8::String::New(type.data(), type.size()), |
| - v8::ReadOnly); |
| - |
| - TriggerEvent(kLoadAbortEventName, &event); |
| + std::map<std::string, base::Value*> props; |
| + props[kURL] = base::Value::CreateStringValue(url.spec()); |
| + props[kIsTopLevel] = base::Value::CreateBooleanValue(is_top_level); |
| + props[kReason] = base::Value::CreateStringValue(type); |
| + TriggerEvent(kLoadAbortEventName, &props); |
| } |
| void BrowserPlugin::LoadRedirect(const GURL& old_url, |
| const GURL& new_url, |
| bool is_top_level) { |
| - if (!HasListeners(kLoadRedirectEventName)) |
| - return; |
| - |
| - WebKit::WebElement plugin = container()->element(); |
| - v8::HandleScope handle_scope; |
| - v8::Context::Scope context_scope( |
| - plugin.document().frame()->mainWorldScriptContext()); |
| - |
| - // Construct the loadRedirect event object. |
| - v8::Local<v8::Object> event = v8::Object::New(); |
| - event->Set(v8::String::New(kOldURL, sizeof(kOldURL) - 1), |
| - v8::String::New(old_url.spec().data(), old_url.spec().size()), |
| - v8::ReadOnly); |
| - event->Set(v8::String::New(kNewURL, sizeof(kNewURL) - 1), |
| - v8::String::New(new_url.spec().data(), new_url.spec().size()), |
| - v8::ReadOnly); |
| - event->Set(v8::String::New(kIsTopLevel, sizeof(kIsTopLevel) - 1), |
| - v8::Boolean::New(is_top_level), |
| - v8::ReadOnly); |
| - |
| - TriggerEvent(kLoadRedirectEventName, &event); |
| + std::map<std::string, base::Value*> props; |
| + props[kOldURL] = base::Value::CreateStringValue(old_url.spec()); |
| + props[kNewURL] = base::Value::CreateStringValue(new_url.spec()); |
| + props[kIsTopLevel] = base::Value::CreateBooleanValue(is_top_level); |
| + TriggerEvent(kLoadRedirectEventName, &props); |
| } |
| void BrowserPlugin::AdvanceFocus(bool reverse) { |
| @@ -639,43 +537,6 @@ void BrowserPlugin::SetAcceptTouchEvents(bool accept) { |
| container()->setIsAcceptingTouchEvents(accept); |
| } |
| -bool BrowserPlugin::HasListeners(const std::string& event_name) { |
| - return IsValidEvent(event_name) && |
| - !event_listener_map_[event_name].empty(); |
| -} |
| - |
| -bool BrowserPlugin::AddEventListener(const std::string& event_name, |
| - v8::Local<v8::Function> function) { |
| - if (!IsValidEvent(event_name)) |
| - return false; |
| - EventListeners& listeners = event_listener_map_[event_name]; |
| - for (unsigned int i = 0; i < listeners.size(); ++i) { |
| - if (listeners[i] == function) |
| - return false; |
| - } |
| - v8::Persistent<v8::Function> persistent_function = |
| - v8::Persistent<v8::Function>::New(function); |
| - listeners.push_back(persistent_function); |
| - return true; |
| -} |
| - |
| -bool BrowserPlugin::RemoveEventListener(const std::string& event_name, |
| - v8::Local<v8::Function> function) { |
| - if (!HasListeners(event_name)) |
| - return false; |
| - |
| - EventListeners& listeners = event_listener_map_[event_name]; |
| - EventListeners::iterator it = listeners.begin(); |
| - for (; it != listeners.end(); ++it) { |
| - if (*it == function) { |
| - it->Dispose(); |
| - listeners.erase(it); |
| - return true; |
| - } |
| - } |
| - return false; |
| -} |
| - |
| WebKit::WebPluginContainer* BrowserPlugin::container() const { |
| return container_; |
| } |