diff options
author | teravest@chromium.org <teravest@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-12 15:30:40 +0000 |
---|---|---|
committer | teravest@chromium.org <teravest@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-12 15:30:40 +0000 |
commit | 609c9c9f4597a94e8eab49f675f088c05478f0fc (patch) | |
tree | e947334ad5d249e7763b378ba7e27d5d97a35c03 /components | |
parent | 6c38b34525413dd0f892444851522f681d4720a3 (diff) | |
download | chromium_src-609c9c9f4597a94e8eab49f675f088c05478f0fc.zip chromium_src-609c9c9f4597a94e8eab49f675f088c05478f0fc.tar.gz chromium_src-609c9c9f4597a94e8eab49f675f088c05478f0fc.tar.bz2 |
Pepper: Clean up ProgressEvent logic.
This simplifies NexeLoadManager, which is getting pretty big. This also removes
an unused field from ProgressEvent, and adds a convenience function for posting
a task to dispatch a progress event from the main thread, which is often
necessary to ensure that progress events are posted to the DOM after other
state changes.
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=269362
R=dmichael@chromium.org
Review URL: https://codereview.chromium.org/270453004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269784 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components')
-rw-r--r-- | components/nacl.gyp | 2 | ||||
-rw-r--r-- | components/nacl/renderer/nexe_load_manager.cc | 139 | ||||
-rw-r--r-- | components/nacl/renderer/nexe_load_manager.h | 25 | ||||
-rw-r--r-- | components/nacl/renderer/ppb_nacl_private_impl.cc | 43 | ||||
-rw-r--r-- | components/nacl/renderer/progress_event.cc | 97 | ||||
-rw-r--r-- | components/nacl/renderer/progress_event.h | 53 |
6 files changed, 178 insertions, 181 deletions
diff --git a/components/nacl.gyp b/components/nacl.gyp index 8596924..a7b2538 100644 --- a/components/nacl.gyp +++ b/components/nacl.gyp @@ -167,6 +167,8 @@ 'nacl/renderer/pnacl_translation_resource_host.h', 'nacl/renderer/ppb_nacl_private_impl.cc', 'nacl/renderer/ppb_nacl_private_impl.h', + 'nacl/renderer/progress_event.cc', + 'nacl/renderer/progress_event.h', 'nacl/renderer/sandbox_arch.cc', 'nacl/renderer/sandbox_arch.h', 'nacl/renderer/trusted_plugin_channel.cc', diff --git a/components/nacl/renderer/nexe_load_manager.cc b/components/nacl/renderer/nexe_load_manager.cc index 94b56b5..658f0cf 100644 --- a/components/nacl/renderer/nexe_load_manager.cc +++ b/components/nacl/renderer/nexe_load_manager.cc @@ -14,6 +14,7 @@ #include "components/nacl/renderer/histogram.h" #include "components/nacl/renderer/manifest_service_channel.h" #include "components/nacl/renderer/pnacl_translation_resource_host.h" +#include "components/nacl/renderer/progress_event.h" #include "components/nacl/renderer/sandbox_arch.h" #include "components/nacl/renderer/trusted_plugin_channel.h" #include "content/public/common/content_client.h" @@ -33,10 +34,8 @@ #include "ppapi/shared_impl/var.h" #include "ppapi/shared_impl/var_tracker.h" #include "ppapi/thunk/enter.h" -#include "third_party/WebKit/public/web/WebDOMResourceProgressEvent.h" #include "third_party/WebKit/public/web/WebDocument.h" #include "third_party/WebKit/public/web/WebElement.h" -#include "third_party/WebKit/public/web/WebLocalFrame.h" #include "third_party/WebKit/public/web/WebPluginContainer.h" #include "third_party/WebKit/public/web/WebView.h" #include "v8/include/v8.h" @@ -60,27 +59,6 @@ const char* const kDevAttribute = "@dev"; const char* const kNaClMIMEType = "application/x-nacl"; const char* const kPNaClMIMEType = "application/x-pnacl"; -blink::WebString EventTypeToString(PP_NaClEventType event_type) { - switch (event_type) { - case PP_NACL_EVENT_LOADSTART: - return blink::WebString::fromUTF8("loadstart"); - case PP_NACL_EVENT_PROGRESS: - return blink::WebString::fromUTF8("progress"); - case PP_NACL_EVENT_ERROR: - return blink::WebString::fromUTF8("error"); - case PP_NACL_EVENT_ABORT: - return blink::WebString::fromUTF8("abort"); - case PP_NACL_EVENT_LOAD: - return blink::WebString::fromUTF8("load"); - case PP_NACL_EVENT_LOADEND: - return blink::WebString::fromUTF8("loadend"); - case PP_NACL_EVENT_CRASH: - return blink::WebString::fromUTF8("crash"); - } - NOTIMPLEMENTED(); - return blink::WebString(); -} - static int GetRoutingID(PP_Instance instance) { // Check that we are on the main renderer thread. DCHECK(content::RenderThread::Get()); @@ -168,14 +146,9 @@ void NexeLoadManager::NexeFileDidOpen(int32_t pp_error, nexe_size_); // Inform JavaScript that we successfully downloaded the nacl module. - ProgressEvent progress_event(pp_instance_, PP_NACL_EVENT_PROGRESS, url, - true, nexe_size_, nexe_size_); - ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( - FROM_HERE, - base::Bind(&NexeLoadManager::DispatchEvent, - weak_factory_.GetWeakPtr(), - progress_event)); - + ProgressEvent progress_event(PP_NACL_EVENT_PROGRESS, url, true, nexe_size_, + nexe_size_); + DispatchProgressEvent(pp_instance_, progress_event); load_start_ = base::Time::Now(); } } @@ -197,21 +170,13 @@ void NexeLoadManager::ReportLoadSuccess(const std::string& url, set_nacl_ready_state(PP_NACL_READY_STATE_DONE); // Inform JavaScript that loading was successful and is complete. - ProgressEvent load_event(pp_instance_, PP_NACL_EVENT_LOAD, url, true, - loaded_bytes, total_bytes); - ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( - FROM_HERE, - base::Bind(&NexeLoadManager::DispatchEvent, - weak_factory_.GetWeakPtr(), - load_event)); - - ProgressEvent loadend_event(pp_instance_, PP_NACL_EVENT_LOADEND, url, true, - loaded_bytes, total_bytes); - ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( - FROM_HERE, - base::Bind(&NexeLoadManager::DispatchEvent, - weak_factory_.GetWeakPtr(), - loadend_event)); + ProgressEvent load_event(PP_NACL_EVENT_LOAD, url, true, loaded_bytes, + total_bytes); + DispatchProgressEvent(pp_instance_, load_event); + + ProgressEvent loadend_event(PP_NACL_EVENT_LOADEND, url, true, loaded_bytes, + total_bytes); + DispatchProgressEvent(pp_instance_, loadend_event); // UMA HistogramEnumerateLoadStatus(PP_NACL_ERROR_LOAD_SUCCESS, is_installed_); @@ -245,17 +210,8 @@ void NexeLoadManager::ReportLoadError(PP_NaClError error, SetLastError(error_string); // Inform JavaScript that loading encountered an error and is complete. - ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( - FROM_HERE, - base::Bind(&NexeLoadManager::DispatchEvent, - weak_factory_.GetWeakPtr(), - ProgressEvent(PP_NACL_EVENT_ERROR))); - - ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( - FROM_HERE, - base::Bind(&NexeLoadManager::DispatchEvent, - weak_factory_.GetWeakPtr(), - ProgressEvent(PP_NACL_EVENT_LOADEND))); + DispatchProgressEvent(pp_instance_, ProgressEvent(PP_NACL_EVENT_ERROR)); + DispatchProgressEvent(pp_instance_, ProgressEvent(PP_NACL_EVENT_LOADEND)); HistogramEnumerateLoadStatus(error, is_installed_); LogToConsole(console_message); @@ -274,17 +230,8 @@ void NexeLoadManager::ReportLoadAbort() { SetLastError(error_string); // Inform JavaScript that loading was aborted and is complete. - ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( - FROM_HERE, - base::Bind(&NexeLoadManager::DispatchEvent, - weak_factory_.GetWeakPtr(), - ProgressEvent(PP_NACL_EVENT_ABORT))); - - ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( - FROM_HERE, - base::Bind(&NexeLoadManager::DispatchEvent, - weak_factory_.GetWeakPtr(), - ProgressEvent(PP_NACL_EVENT_LOADEND))); + DispatchProgressEvent(pp_instance_, ProgressEvent(PP_NACL_EVENT_ABORT)); + DispatchProgressEvent(pp_instance_, ProgressEvent(PP_NACL_EVENT_LOADEND)); HistogramEnumerateLoadStatus(PP_NACL_ERROR_LOAD_ABORTED, is_installed_); LogToConsole(error_string); @@ -321,43 +268,6 @@ void NexeLoadManager::NexeDidCrash(const char* crash_log) { CopyCrashLogToJsConsole(crash_log); } -void NexeLoadManager::DispatchEvent(const ProgressEvent &event) { - blink::WebPluginContainer* container = plugin_instance_->GetContainer(); - // It's possible that container() is NULL if the plugin has been removed from - // the DOM (but the PluginInstance is not destroyed yet). - if (!container) - return; - blink::WebLocalFrame* frame = container->element().document().frame(); - if (!frame) - return; - v8::HandleScope handle_scope(plugin_instance_->GetIsolate()); - v8::Local<v8::Context> context( - plugin_instance_->GetIsolate()->GetCurrentContext()); - if (context.IsEmpty()) { - // If there's no JavaScript on the stack, we have to make a new Context. - context = v8::Context::New(plugin_instance_->GetIsolate()); - } - v8::Context::Scope context_scope(context); - - if (!event.resource_url.empty()) { - blink::WebString url_string = blink::WebString::fromUTF8( - event.resource_url.data(), event.resource_url.size()); - blink::WebDOMResourceProgressEvent blink_event( - EventTypeToString(event.event_type), - event.length_is_computable, - event.loaded_bytes, - event.total_bytes, - url_string); - container->element().dispatchEvent(blink_event); - } else { - blink::WebDOMProgressEvent blink_event(EventTypeToString(event.event_type), - event.length_is_computable, - event.loaded_bytes, - event.total_bytes); - container->element().dispatchEvent(blink_event); - } -} - void NexeLoadManager::set_trusted_plugin_channel( scoped_ptr<TrustedPluginChannel> channel) { trusted_plugin_channel_ = channel.Pass(); @@ -438,11 +348,8 @@ bool NexeLoadManager::RequestNaClManifest(const std::string& url, HistogramEnumerateManifestIsDataURI(*is_data_uri); set_nacl_ready_state(PP_NACL_READY_STATE_OPENED); - ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( - FROM_HERE, - base::Bind(&NexeLoadManager::DispatchEvent, - weak_factory_.GetWeakPtr(), - ProgressEvent(PP_NACL_EVENT_LOADSTART))); + DispatchProgressEvent(pp_instance_, + ProgressEvent(PP_NACL_EVENT_LOADSTART)); return true; } } @@ -459,11 +366,7 @@ void NexeLoadManager::ProcessNaClManifest(const std::string& program_url) { if (gurl.is_valid()) is_installed_ = gurl.SchemeIs("chrome-extension"); set_nacl_ready_state(PP_NACL_READY_STATE_LOADING); - ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( - FROM_HERE, - base::Bind(&NexeLoadManager::DispatchEvent, - weak_factory_.GetWeakPtr(), - ProgressEvent(PP_NACL_EVENT_PROGRESS))); + DispatchProgressEvent(pp_instance_, ProgressEvent(PP_NACL_EVENT_PROGRESS)); } std::string NexeLoadManager::GetManifestURLArgument() const { @@ -514,11 +417,7 @@ void NexeLoadManager::ReportDeadNexe() { SetLastError(message); LogToConsole(message); - ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( - FROM_HERE, - base::Bind(&NexeLoadManager::DispatchEvent, - weak_factory_.GetWeakPtr(), - ProgressEvent(PP_NACL_EVENT_CRASH))); + DispatchProgressEvent(pp_instance_, ProgressEvent(PP_NACL_EVENT_CRASH)); nexe_error_reported_ = true; } // else ReportLoadError() and ReportLoadAbort() will be used by loading code diff --git a/components/nacl/renderer/nexe_load_manager.h b/components/nacl/renderer/nexe_load_manager.h index ed9dca94..878e1ab 100644 --- a/components/nacl/renderer/nexe_load_manager.h +++ b/components/nacl/renderer/nexe_load_manager.h @@ -52,31 +52,6 @@ class NexeLoadManager { // The intent is for this class to only expose functions for reporting a // load state transition (e.g., ReportLoadError, ReportProgress, // ReportLoadAbort, etc.) - struct ProgressEvent { - explicit ProgressEvent(PP_NaClEventType event_type_param) - : event_type(event_type_param), - length_is_computable(false), - loaded_bytes(0), - total_bytes(0) { - } - ProgressEvent(PP_Instance instance, PP_NaClEventType event_type, - const std::string& resource_url, bool length_is_computable, - uint64_t loaded_bytes, uint64_t total_bytes) - : instance(instance), - event_type(event_type), - resource_url(resource_url), - length_is_computable(length_is_computable), - loaded_bytes(loaded_bytes), - total_bytes(total_bytes) { - } - PP_Instance instance; - PP_NaClEventType event_type; - std::string resource_url; - bool length_is_computable; - uint64_t loaded_bytes; - uint64_t total_bytes; - }; - void DispatchEvent(const ProgressEvent &event); void set_trusted_plugin_channel(scoped_ptr<TrustedPluginChannel> channel); void set_manifest_service_channel( scoped_ptr<ManifestServiceChannel> channel); diff --git a/components/nacl/renderer/ppb_nacl_private_impl.cc b/components/nacl/renderer/ppb_nacl_private_impl.cc index e9efc6b..2623e28 100644 --- a/components/nacl/renderer/ppb_nacl_private_impl.cc +++ b/components/nacl/renderer/ppb_nacl_private_impl.cc @@ -26,6 +26,7 @@ #include "components/nacl/renderer/manifest_service_channel.h" #include "components/nacl/renderer/nexe_load_manager.h" #include "components/nacl/renderer/pnacl_translation_resource_host.h" +#include "components/nacl/renderer/progress_event.h" #include "components/nacl/renderer/sandbox_arch.h" #include "components/nacl/renderer/trusted_plugin_channel.h" #include "content/public/common/content_client.h" @@ -654,48 +655,18 @@ PP_FileHandle OpenNaClExecutable(PP_Instance instance, return handle; } -void DispatchEventOnMainThread(PP_Instance instance, - PP_NaClEventType event_type, - const std::string& resource_url, - PP_Bool length_is_computable, - uint64_t loaded_bytes, - uint64_t total_bytes); - void DispatchEvent(PP_Instance instance, PP_NaClEventType event_type, const char *resource_url, PP_Bool length_is_computable, uint64_t loaded_bytes, uint64_t total_bytes) { - ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( - FROM_HERE, - base::Bind(&DispatchEventOnMainThread, - instance, - event_type, - std::string(resource_url), - length_is_computable, - loaded_bytes, - total_bytes)); -} - -void DispatchEventOnMainThread(PP_Instance instance, - PP_NaClEventType event_type, - const std::string& resource_url, - PP_Bool length_is_computable, - uint64_t loaded_bytes, - uint64_t total_bytes) { - NexeLoadManager* load_manager = - GetNexeLoadManager(instance); - // The instance may have been destroyed after we were scheduled, so do - // nothing if it's gone. - if (load_manager) { - NexeLoadManager::ProgressEvent event(event_type); - event.resource_url = resource_url; - event.length_is_computable = PP_ToBool(length_is_computable); - event.loaded_bytes = loaded_bytes; - event.total_bytes = total_bytes; - load_manager->DispatchEvent(event); - } + ProgressEvent event(event_type, + resource_url, + PP_ToBool(length_is_computable), + loaded_bytes, + total_bytes); + DispatchProgressEvent(instance, event); } void NexeFileDidOpen(PP_Instance instance, diff --git a/components/nacl/renderer/progress_event.cc b/components/nacl/renderer/progress_event.cc new file mode 100644 index 0000000..1e05366 --- /dev/null +++ b/components/nacl/renderer/progress_event.cc @@ -0,0 +1,97 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/nacl/renderer/progress_event.h" + +#include "base/bind.h" +#include "base/location.h" +#include "base/logging.h" +#include "base/message_loop/message_loop_proxy.h" +#include "content/public/renderer/pepper_plugin_instance.h" +#include "ppapi/c/private/ppb_nacl_private.h" +#include "ppapi/shared_impl/ppapi_globals.h" +#include "third_party/WebKit/public/platform/WebString.h" +#include "third_party/WebKit/public/web/WebDOMResourceProgressEvent.h" +#include "third_party/WebKit/public/web/WebDocument.h" +#include "third_party/WebKit/public/web/WebElement.h" +#include "third_party/WebKit/public/web/WebLocalFrame.h" +#include "third_party/WebKit/public/web/WebPluginContainer.h" +#include "v8/include/v8.h" + +namespace nacl { + +namespace { +blink::WebString EventTypeToString(PP_NaClEventType event_type) { + switch (event_type) { + case PP_NACL_EVENT_LOADSTART: + return blink::WebString::fromUTF8("loadstart"); + case PP_NACL_EVENT_PROGRESS: + return blink::WebString::fromUTF8("progress"); + case PP_NACL_EVENT_ERROR: + return blink::WebString::fromUTF8("error"); + case PP_NACL_EVENT_ABORT: + return blink::WebString::fromUTF8("abort"); + case PP_NACL_EVENT_LOAD: + return blink::WebString::fromUTF8("load"); + case PP_NACL_EVENT_LOADEND: + return blink::WebString::fromUTF8("loadend"); + case PP_NACL_EVENT_CRASH: + return blink::WebString::fromUTF8("crash"); + } + NOTIMPLEMENTED(); + return blink::WebString(); +} + +void DispatchProgressEventOnMainThread(PP_Instance instance, + const ProgressEvent &event) { + content::PepperPluginInstance* plugin_instance = + content::PepperPluginInstance::Get(instance); + if (!plugin_instance) + return; + + blink::WebPluginContainer* container = plugin_instance->GetContainer(); + // It's possible that container() is NULL if the plugin has been removed from + // the DOM (but the PluginInstance is not destroyed yet). + if (!container) + return; + blink::WebLocalFrame* frame = container->element().document().frame(); + if (!frame) + return; + v8::HandleScope handle_scope(plugin_instance->GetIsolate()); + v8::Local<v8::Context> context( + plugin_instance->GetIsolate()->GetCurrentContext()); + if (context.IsEmpty()) { + // If there's no JavaScript on the stack, we have to make a new Context. + context = v8::Context::New(plugin_instance->GetIsolate()); + } + v8::Context::Scope context_scope(context); + + if (!event.resource_url.empty()) { + blink::WebString url_string = blink::WebString::fromUTF8( + event.resource_url.data(), event.resource_url.size()); + blink::WebDOMResourceProgressEvent blink_event( + EventTypeToString(event.event_type), + event.length_is_computable, + event.loaded_bytes, + event.total_bytes, + url_string); + container->element().dispatchEvent(blink_event); + } else { + blink::WebDOMProgressEvent blink_event(EventTypeToString(event.event_type), + event.length_is_computable, + event.loaded_bytes, + event.total_bytes); + container->element().dispatchEvent(blink_event); + } +} + +} // namespace + +void DispatchProgressEvent(PP_Instance instance, const ProgressEvent &event) { + ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( + FROM_HERE, + base::Bind(&DispatchProgressEventOnMainThread, instance, event)); +} + +} // namespace nacl diff --git a/components/nacl/renderer/progress_event.h b/components/nacl/renderer/progress_event.h new file mode 100644 index 0000000..53be606 --- /dev/null +++ b/components/nacl/renderer/progress_event.h @@ -0,0 +1,53 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_NACL_RENDERER_NEXE_PROGRESS_EVENT_H_ +#define COMPONENTS_NACL_RENDERER_NEXE_PROGRESS_EVENT_H_ + +#include <string> + +#include "ppapi/c/pp_instance.h" +#include "ppapi/c/private/ppb_nacl_private.h" + +namespace nacl { + +// See http://www.w3.org/TR/progress-events/ for more details on progress +// events. +struct ProgressEvent { + explicit ProgressEvent(PP_NaClEventType event_type_param) + : event_type(event_type_param), + length_is_computable(false), + loaded_bytes(0), + total_bytes(0) { + } + + ProgressEvent(PP_NaClEventType event_type_param, + const std::string& resource_url_param, + bool length_is_computable_param, + uint64_t loaded_bytes_param, + uint64_t total_bytes_param) + : event_type(event_type_param), + resource_url(resource_url_param), + length_is_computable(length_is_computable_param), + loaded_bytes(loaded_bytes_param), + total_bytes(total_bytes_param) { + } + + PP_NaClEventType event_type; + std::string resource_url; + bool length_is_computable; + uint64_t loaded_bytes; + uint64_t total_bytes; +}; + +// Dispatches a progress event to the DOM frame corresponding to the specified +// plugin instance. +// This posts a task to the main thread to perform the actual dispatch, since +// it's usually intended for progress events to be dispatched after all other +// state changes are handled. +void DispatchProgressEvent(PP_Instance instance, const ProgressEvent& event); + +} // namespace nacl + +#endif // COMPONENTS_NACL_RENDERER_NEXE_PROGRESS_EVENT_H_ |