summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/ppb_console_impl.cc
blob: 1b08ebf61aa295c2ce98408eada09e7a8305c467 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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 "ppapi/shared_impl/var.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"

using ppapi::Var;
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