Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: webkit/plugins/ppapi/ppb_console_impl.cc

Issue 6667010: Add a console interface for logging to the JS console from a PPAPI plugin.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include "webkit/plugins/ppapi/ppb_console_impl.h"
6
7 #include "base/string_util.h"
8 #include "base/utf_string_conversions.h"
9 #include "ppapi/c/dev/ppb_console_dev.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h"
15 #include "webkit/plugins/ppapi/plugin_module.h"
16 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
17 #include "webkit/plugins/ppapi/resource_tracker.h"
18 #include "webkit/plugins/ppapi/var.h"
19
20 using WebKit::WebConsoleMessage;
21 using WebKit::WebString;
22
23 namespace webkit {
24 namespace ppapi {
25
26 namespace {
27
28 void LogWithSource(PP_Instance instance_id,
29 PP_LogLevel_Dev level,
30 PP_Var source,
31 PP_Var value) {
32 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
33 if (!instance)
34 return;
35
36 // Convert the log level, defaulting to error.
37 WebConsoleMessage::Level web_level;
38 switch (level) {
39 case PP_LOGLEVEL_TIP:
40 web_level = WebConsoleMessage::LevelTip;
41 break;
42 case PP_LOGLEVEL_LOG:
43 web_level = WebConsoleMessage::LevelLog;
44 break;
45 case PP_LOGLEVEL_WARNING:
46 web_level = WebConsoleMessage::LevelWarning;
47 break;
48 case PP_LOGLEVEL_ERROR:
49 default:
50 web_level = WebConsoleMessage::LevelError;
51 break;
52 }
53
54 // Format is the "<source>: <value>". The source defaults to the module name.
55 scoped_refptr<StringVar> source_string(StringVar::FromPPVar(source));
56 std::string message = source_string.get() ? source_string->value() :
57 instance->module()->name();
58 message.append(": ");
59 message.append(Var::PPVarToLogString(value));
60
61 // PP_Vars can contain embedded NULLs, don't pass these through. Even if the
62 // WebKit console handles it properly, you wouldn't be able to see them which
63 // is probably not useful.
64 std::string null;
65 null.push_back(0);
66 ReplaceSubstringsAfterOffset(&message, 0, null, "\\0");
yzshen1 2011/03/11 00:40:35 We have duplicate code for escaping NULLs, and we
67
68 instance->container()->element().document().frame()->addMessageToConsole(
69 WebConsoleMessage(web_level, WebString(UTF8ToUTF16(message))));
70 }
71
72 void Log(PP_Instance instance, PP_LogLevel_Dev level, PP_Var value) {
73 LogWithSource(instance, level, PP_MakeUndefined(), value);
74 }
75
76 const PPB_Console_Dev ppb_console = {
77 &Log,
78 &LogWithSource
79 };
80
81 } // namespace
82
83 // static
84 const struct PPB_Console_Dev* PPB_Console_Impl::GetInterface() {
85 return &ppb_console;
86 }
87
88 } // namespace ppapi
89 } // namespace webkit
90
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698