diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-11 20:20:56 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-11 20:20:56 +0000 |
commit | d38c5740cdfada7281f07a201dc01417511220a5 (patch) | |
tree | cc80698c750862160a74dff6ce4d20131cf585ec /webkit | |
parent | c8d7021f04a3430705fc7ccd9672fc83d6533caf (diff) | |
download | chromium_src-d38c5740cdfada7281f07a201dc01417511220a5.zip chromium_src-d38c5740cdfada7281f07a201dc01417511220a5.tar.gz chromium_src-d38c5740cdfada7281f07a201dc01417511220a5.tar.bz2 |
Add a console interface for logging to the JS console from a PPAPI plugin.
TEST=manual
Review URL: http://codereview.chromium.org/6667010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77852 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
-rw-r--r-- | webkit/glue/webkit_glue.gypi | 2 | ||||
-rw-r--r-- | webkit/plugins/ppapi/plugin_module.cc | 4 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppb_console_impl.cc | 86 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppb_console_impl.h | 23 | ||||
-rw-r--r-- | webkit/plugins/ppapi/var.cc | 33 | ||||
-rw-r--r-- | webkit/plugins/ppapi/var.h | 3 |
6 files changed, 151 insertions, 0 deletions
diff --git a/webkit/glue/webkit_glue.gypi b/webkit/glue/webkit_glue.gypi index 83395d3..1dc4664 100644 --- a/webkit/glue/webkit_glue.gypi +++ b/webkit/glue/webkit_glue.gypi @@ -283,6 +283,8 @@ '../plugins/ppapi/ppb_buffer_impl.h', '../plugins/ppapi/ppb_char_set_impl.cc', '../plugins/ppapi/ppb_char_set_impl.h', + '../plugins/ppapi/ppb_console_impl.cc', + '../plugins/ppapi/ppb_console_impl.h', '../plugins/ppapi/ppb_context_3d_impl.cc', '../plugins/ppapi/ppb_context_3d_impl.h', '../plugins/ppapi/ppb_cursor_control_impl.cc', diff --git a/webkit/plugins/ppapi/plugin_module.cc b/webkit/plugins/ppapi/plugin_module.cc index 0ea2859..02d1276 100644 --- a/webkit/plugins/ppapi/plugin_module.cc +++ b/webkit/plugins/ppapi/plugin_module.cc @@ -16,6 +16,7 @@ #include "ppapi/c/dev/ppb_char_set_dev.h" #include "ppapi/c/dev/ppb_context_3d_dev.h" #include "ppapi/c/dev/ppb_context_3d_trusted_dev.h" +#include "ppapi/c/dev/ppb_console_dev.h" #include "ppapi/c/dev/ppb_cursor_control_dev.h" #include "ppapi/c/dev/ppb_directory_reader_dev.h" #include "ppapi/c/dev/ppb_file_io_dev.h" @@ -65,6 +66,7 @@ #include "webkit/plugins/ppapi/ppb_audio_impl.h" #include "webkit/plugins/ppapi/ppb_buffer_impl.h" #include "webkit/plugins/ppapi/ppb_char_set_impl.h" +#include "webkit/plugins/ppapi/ppb_console_impl.h" #include "webkit/plugins/ppapi/ppb_cursor_control_impl.h" #include "webkit/plugins/ppapi/ppb_directory_reader_impl.h" #include "webkit/plugins/ppapi/ppb_file_chooser_impl.h" @@ -230,6 +232,8 @@ const void* GetInterface(const char* name) { return PPB_CharSet_Impl::GetInterface(); if (strcmp(name, PPB_CLASS_INTERFACE) == 0) return VarObjectClass::GetInterface(); + if (strcmp(name, PPB_CONSOLE_DEV_INTERFACE) == 0) + return PPB_Console_Impl::GetInterface(); if (strcmp(name, PPB_CORE_INTERFACE) == 0) return &core_interface; if (strcmp(name, PPB_CURSOR_CONTROL_DEV_INTERFACE) == 0) diff --git a/webkit/plugins/ppapi/ppb_console_impl.cc b/webkit/plugins/ppapi/ppb_console_impl.cc new file mode 100644 index 0000000..3ca773c --- /dev/null +++ b/webkit/plugins/ppapi/ppb_console_impl.cc @@ -0,0 +1,86 @@ +// Copyright (c) 2011 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 "webkit/plugins/ppapi/ppb_console_impl.h" + +#include "base/string_util.h" +#include "base/utf_string_conversions.h" +#include "ppapi/c/dev/ppb_console_dev.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" +#include "webkit/plugins/ppapi/plugin_module.h" +#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" +#include "webkit/plugins/ppapi/resource_tracker.h" +#include "webkit/plugins/ppapi/var.h" + +using WebKit::WebConsoleMessage; +using WebKit::WebString; + +namespace webkit { +namespace ppapi { + +namespace { + +void LogWithSource(PP_Instance instance_id, + PP_LogLevel_Dev level, + PP_Var source, + PP_Var value) { + PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); + if (!instance) + return; + + // Convert the log level, defaulting to error. + WebConsoleMessage::Level web_level; + switch (level) { + case PP_LOGLEVEL_TIP: + web_level = WebConsoleMessage::LevelTip; + break; + case PP_LOGLEVEL_LOG: + web_level = WebConsoleMessage::LevelLog; + break; + case PP_LOGLEVEL_WARNING: + web_level = WebConsoleMessage::LevelWarning; + break; + case PP_LOGLEVEL_ERROR: + default: + web_level = WebConsoleMessage::LevelError; + break; + } + + // Format is the "<source>: <value>". The source defaults to the module name + // if the source isn't a string or is empty. + std::string message; + if (source.type == PP_VARTYPE_STRING) + message = Var::PPVarToLogString(source); + if (message.empty()) + message = instance->module()->name(); + message.append(": "); + message.append(Var::PPVarToLogString(value)); + + instance->container()->element().document().frame()->addMessageToConsole( + WebConsoleMessage(web_level, WebString(UTF8ToUTF16(message)))); +} + +void Log(PP_Instance instance, PP_LogLevel_Dev level, PP_Var value) { + LogWithSource(instance, level, PP_MakeUndefined(), value); +} + +const PPB_Console_Dev ppb_console = { + &Log, + &LogWithSource +}; + +} // namespace + +// static +const struct PPB_Console_Dev* PPB_Console_Impl::GetInterface() { + return &ppb_console; +} + +} // namespace ppapi +} // namespace webkit + diff --git a/webkit/plugins/ppapi/ppb_console_impl.h b/webkit/plugins/ppapi/ppb_console_impl.h new file mode 100644 index 0000000..2d47587 --- /dev/null +++ b/webkit/plugins/ppapi/ppb_console_impl.h @@ -0,0 +1,23 @@ +// Copyright (c) 2011 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 WEBKIT_PLUGINS_PPAPI_PPB_CONSOLE_IMPL_H_ +#define WEBKIT_PLUGINS_PPAPI_PPB_CONSOLE_IMPL_H_ + +struct PPB_Console_Dev; + +namespace webkit { +namespace ppapi { + +class PPB_Console_Impl { + public: + // Returns a pointer to the interface implementing PPB_Console_Dev that is + // exposed to the plugin. + static const PPB_Console_Dev* GetInterface(); +}; + +} // namespace ppapi +} // namespace webkit + +#endif // WEBKIT_PLUGINS_PPAPI_PPB_CONSOLE_IMPL_H_ diff --git a/webkit/plugins/ppapi/var.cc b/webkit/plugins/ppapi/var.cc index b8071ed..097c613 100644 --- a/webkit/plugins/ppapi/var.cc +++ b/webkit/plugins/ppapi/var.cc @@ -8,6 +8,7 @@ #include "base/logging.h" #include "base/scoped_ptr.h" +#include "base/string_number_conversions.h" #include "base/string_util.h" #include "ppapi/c/dev/ppb_var_deprecated.h" #include "ppapi/c/ppb_var.h" @@ -724,6 +725,38 @@ PP_Var Var::NPIdentifierToPPVar(PluginModule* module, NPIdentifier id) { } // static +std::string Var::PPVarToLogString(PP_Var var) { + switch (var.type) { + case PP_VARTYPE_UNDEFINED: + return "[Undefined]"; + case PP_VARTYPE_NULL: + return "[Null]"; + case PP_VARTYPE_BOOL: + return var.value.as_bool ? "[True]" : "[False]"; + case PP_VARTYPE_INT32: + return base::IntToString(var.value.as_int); + case PP_VARTYPE_DOUBLE: + return base::DoubleToString(var.value.as_double); + case PP_VARTYPE_STRING: { + scoped_refptr<StringVar> string(StringVar::FromPPVar(var)); + if (!string) + return "[Invalid string]"; + + // Since this is for logging, escape NULLs. + std::string result = string->value(); + std::string null; + null.push_back(0); + ReplaceSubstringsAfterOffset(&result, 0, null, "\\0"); + return result; + } + case PP_VARTYPE_OBJECT: + return "[Object]"; + default: + return "[Invalid var]"; + } +} + +// static void Var::PluginAddRefPPVar(PP_Var var) { if (var.type == PP_VARTYPE_STRING || var.type == PP_VARTYPE_OBJECT) { if (!ResourceTracker::Get()->AddRefVar(static_cast<int32>(var.value.as_id))) diff --git a/webkit/plugins/ppapi/var.h b/webkit/plugins/ppapi/var.h index 4571698..0a17585 100644 --- a/webkit/plugins/ppapi/var.h +++ b/webkit/plugins/ppapi/var.h @@ -54,6 +54,9 @@ class Var : public base::RefCounted<Var> { // given module. A returned string will have a reference count of 1. static PP_Var NPIdentifierToPPVar(PluginModule* module, NPIdentifier id); + // Returns a string representing the given var for logging purposes. + static std::string PPVarToLogString(PP_Var var); + // Provides access to the manual refcounting of a PP_Var from the plugin's // perspective. This is different than the AddRef/Release on this scoped // object. This uses the ResourceTracker, which keeps a separate "plugin |