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 }
40
DispatchProgressEventOnMainThread(PP_Instance instance,const ProgressEvent & event)41 void DispatchProgressEventOnMainThread(PP_Instance instance,
42 const ProgressEvent& event) {
43 content::PepperPluginInstance* plugin_instance =
44 content::PepperPluginInstance::Get(instance);
45 if (!plugin_instance)
46 return;
47
48 WebPluginContainer* container = plugin_instance->GetContainer();
49 // It's possible that container() is NULL if the plugin has been removed from
50 // the DOM (but the PluginInstance is not destroyed yet).
51 if (!container)
52 return;
53
54 container->DispatchProgressEvent(
55 WebString::FromUTF8(EventTypeToName(event.event_type)),
56 event.length_is_computable, event.loaded_bytes, event.total_bytes,
57 WebString::FromUTF8(event.resource_url));
58 }
59
60 } // namespace
61
DispatchProgressEvent(PP_Instance instance,const ProgressEvent & event)62 void DispatchProgressEvent(PP_Instance instance, const ProgressEvent& event) {
63 ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
64 FROM_HERE,
65 base::BindOnce(&DispatchProgressEventOnMainThread, instance, event));
66 }
67
68 } // namespace nacl
69