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