Don't preload rarely seen large images
[chromium-blink-merge.git] / components / nacl / renderer / progress_event.cc
blobae307558492ad6ed3a25bed2a1e8919ee2f6e030
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"
7 #include "base/bind.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"
21 namespace nacl {
23 namespace {
24 blink::WebString EventTypeToString(PP_NaClEventType event_type) {
25 switch (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");
41 NOTIMPLEMENTED();
42 return blink::WebString();
45 void DispatchProgressEventOnMainThread(PP_Instance instance,
46 const ProgressEvent &event) {
47 content::PepperPluginInstance* plugin_instance =
48 content::PepperPluginInstance::Get(instance);
49 if (!plugin_instance)
50 return;
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).
55 if (!container)
56 return;
57 blink::WebLocalFrame* frame = container->element().document().frame();
58 if (!frame)
59 return;
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,
75 event.loaded_bytes,
76 event.total_bytes,
77 url_string);
78 container->element().dispatchEvent(blink_event);
79 } else {
80 blink::WebDOMProgressEvent blink_event(EventTypeToString(event.event_type),
81 event.length_is_computable,
82 event.loaded_bytes,
83 event.total_bytes);
84 container->element().dispatchEvent(blink_event);
88 } // namespace
90 void DispatchProgressEvent(PP_Instance instance, const ProgressEvent &event) {
91 ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask(
92 FROM_HERE,
93 base::Bind(&DispatchProgressEventOnMainThread, instance, event));
96 } // namespace nacl