OLD | NEW |
| (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 "ppapi/shared_impl/var.h" | |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | |
16 #include "webkit/plugins/ppapi/plugin_module.h" | |
17 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
18 #include "webkit/plugins/ppapi/resource_tracker.h" | |
19 | |
20 using ppapi::Var; | |
21 using WebKit::WebConsoleMessage; | |
22 using WebKit::WebString; | |
23 | |
24 namespace webkit { | |
25 namespace ppapi { | |
26 | |
27 namespace { | |
28 | |
29 void LogWithSource(PP_Instance instance_id, | |
30 PP_LogLevel_Dev level, | |
31 PP_Var source, | |
32 PP_Var value) { | |
33 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | |
34 if (!instance) | |
35 return; | |
36 | |
37 // Convert the log level, defaulting to error. | |
38 WebConsoleMessage::Level web_level; | |
39 switch (level) { | |
40 case PP_LOGLEVEL_TIP: | |
41 web_level = WebConsoleMessage::LevelTip; | |
42 break; | |
43 case PP_LOGLEVEL_LOG: | |
44 web_level = WebConsoleMessage::LevelLog; | |
45 break; | |
46 case PP_LOGLEVEL_WARNING: | |
47 web_level = WebConsoleMessage::LevelWarning; | |
48 break; | |
49 case PP_LOGLEVEL_ERROR: | |
50 default: | |
51 web_level = WebConsoleMessage::LevelError; | |
52 break; | |
53 } | |
54 | |
55 // Format is the "<source>: <value>". The source defaults to the module name | |
56 // if the source isn't a string or is empty. | |
57 std::string message; | |
58 if (source.type == PP_VARTYPE_STRING) | |
59 message = Var::PPVarToLogString(source); | |
60 if (message.empty()) | |
61 message = instance->module()->name(); | |
62 message.append(": "); | |
63 message.append(Var::PPVarToLogString(value)); | |
64 | |
65 instance->container()->element().document().frame()->addMessageToConsole( | |
66 WebConsoleMessage(web_level, WebString(UTF8ToUTF16(message)))); | |
67 } | |
68 | |
69 void Log(PP_Instance instance, PP_LogLevel_Dev level, PP_Var value) { | |
70 LogWithSource(instance, level, PP_MakeUndefined(), value); | |
71 } | |
72 | |
73 const PPB_Console_Dev ppb_console = { | |
74 &Log, | |
75 &LogWithSource | |
76 }; | |
77 | |
78 } // namespace | |
79 | |
80 // static | |
81 const struct PPB_Console_Dev* PPB_Console_Impl::GetInterface() { | |
82 return &ppb_console; | |
83 } | |
84 | |
85 } // namespace ppapi | |
86 } // namespace webkit | |
87 | |
OLD | NEW |