1 // Copyright 2014 The Chromium Authors
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 "components/nacl/renderer/progress_event.h"
6
7 #include "base/functional/bind.h"
8 #include "base/location.h"
9 #include "components/nacl/renderer/ppb_nacl_private.h"
10 #include "content/public/renderer/pepper_plugin_instance.h"
11 #include "ppapi/shared_impl/ppapi_globals.h"
12 #include "third_party/blink/public/platform/web_string.h"
13 #include "third_party/blink/public/web/web_plugin_container.h"
14
15 using blink::WebString;
16 using blink::WebPluginContainer;
17
18 namespace nacl {
19
20 namespace {
EventTypeToName(PP_NaClEventType event_type)21 const char* EventTypeToName(PP_NaClEventType event_type) {
22 switch (event_type) {
23 case PP_NACL_EVENT_LOADSTART:
24 return "loadstart";
25 case PP_NACL_EVENT_PROGRESS:
26 return "progress";
27 case PP_NACL_EVENT_ERROR:
28 return "error";
29 case PP_NACL_EVENT_ABORT:
30 return "abort";
31 case PP_NACL_EVENT_LOAD:
32 return "load";
33 case PP_NACL_EVENT_LOADEND:
34 return "loadend";
35 case PP_NACL_EVENT_CRASH:
36 return "crash";
37 }
38 NOTREACHED();
39 return "";
40 }
41
DispatchProgressEventOnMainThread(PP_Instance instance,const ProgressEvent & event)42 void DispatchProgressEventOnMainThread(PP_Instance instance,
43 const ProgressEvent& event) {
44 content::PepperPluginInstance* plugin_instance =
45 content::PepperPluginInstance::Get(instance);
46 if (!plugin_instance)
47 return;
48
49 WebPluginContainer* container = plugin_instance->GetContainer();
50 // It's possible that container() is NULL if the plugin has been removed from
51 // the DOM (but the PluginInstance is not destroyed yet).
52 if (!container)
53 return;
54
55 container->DispatchProgressEvent(
56 WebString::FromUTF8(EventTypeToName(event.event_type)),
57 event.length_is_computable, event.loaded_bytes, event.total_bytes,
58 WebString::FromUTF8(event.resource_url));
59 }
60
61 } // namespace
62
DispatchProgressEvent(PP_Instance instance,const ProgressEvent & event)63 void DispatchProgressEvent(PP_Instance instance, const ProgressEvent& event) {
64 ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
65 FROM_HERE,
66 base::BindOnce(&DispatchProgressEventOnMainThread, instance, event));
67 }
68
69 } // namespace nacl
70