1 // Copyright 2014 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.
5 #include "components/nacl/renderer/progress_event.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "components/nacl/renderer/ppb_nacl_private.h"
11 #include "content/public/renderer/pepper_plugin_instance.h"
12 #include "ppapi/shared_impl/ppapi_globals.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
14 #include "third_party/WebKit/public/web/WebDOMResourceProgressEvent.h"
15 #include "third_party/WebKit/public/web/WebDocument.h"
16 #include "third_party/WebKit/public/web/WebElement.h"
17 #include "third_party/WebKit/public/web/WebLocalFrame.h"
18 #include "third_party/WebKit/public/web/WebPluginContainer.h"
19 #include "v8/include/v8.h"
24 blink::WebString
EventTypeToString(PP_NaClEventType event_type
) {
26 case PP_NACL_EVENT_LOADSTART
:
27 return blink::WebString::fromUTF8("loadstart");
28 case PP_NACL_EVENT_PROGRESS
:
29 return blink::WebString::fromUTF8("progress");
30 case PP_NACL_EVENT_ERROR
:
31 return blink::WebString::fromUTF8("error");
32 case PP_NACL_EVENT_ABORT
:
33 return blink::WebString::fromUTF8("abort");
34 case PP_NACL_EVENT_LOAD
:
35 return blink::WebString::fromUTF8("load");
36 case PP_NACL_EVENT_LOADEND
:
37 return blink::WebString::fromUTF8("loadend");
38 case PP_NACL_EVENT_CRASH
:
39 return blink::WebString::fromUTF8("crash");
42 return blink::WebString();
45 void DispatchProgressEventOnMainThread(PP_Instance instance
,
46 const ProgressEvent
&event
) {
47 content::PepperPluginInstance
* plugin_instance
=
48 content::PepperPluginInstance::Get(instance
);
52 blink::WebPluginContainer
* container
= plugin_instance
->GetContainer();
53 // It's possible that container() is NULL if the plugin has been removed from
54 // the DOM (but the PluginInstance is not destroyed yet).
57 blink::WebLocalFrame
* frame
= container
->element().document().frame();
60 v8::HandleScope
handle_scope(plugin_instance
->GetIsolate());
61 v8::Local
<v8::Context
> context(
62 plugin_instance
->GetIsolate()->GetCurrentContext());
63 if (context
.IsEmpty()) {
64 // If there's no JavaScript on the stack, we have to make a new Context.
65 context
= v8::Context::New(plugin_instance
->GetIsolate());
67 v8::Context::Scope
context_scope(context
);
69 if (!event
.resource_url
.empty()) {
70 blink::WebString url_string
= blink::WebString::fromUTF8(
71 event
.resource_url
.data(), event
.resource_url
.size());
72 blink::WebDOMResourceProgressEvent
blink_event(
73 EventTypeToString(event
.event_type
),
74 event
.length_is_computable
,
78 container
->element().dispatchEvent(blink_event
);
80 blink::WebDOMProgressEvent
blink_event(EventTypeToString(event
.event_type
),
81 event
.length_is_computable
,
84 container
->element().dispatchEvent(blink_event
);
90 void DispatchProgressEvent(PP_Instance instance
, const ProgressEvent
&event
) {
91 ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
93 base::Bind(&DispatchProgressEventOnMainThread
, instance
, event
));