OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "chrome/renderer/extensions/console.h" |
| 6 |
| 7 #include "base/compiler_specific.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/renderer/extensions/dispatcher.h" |
| 10 #include "chrome/renderer/extensions/extension_helper.h" |
| 11 #include "content/public/renderer/render_view.h" |
| 12 #include "content/public/renderer/render_view_visitor.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 16 |
| 17 namespace extensions { |
| 18 namespace console { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class ByContextFinder : public content::RenderViewVisitor { |
| 23 public: |
| 24 static content::RenderView* Find(v8::Handle<v8::Context> context) { |
| 25 ByContextFinder finder(context); |
| 26 content::RenderView::ForEach(&finder); |
| 27 return finder.found_; |
| 28 } |
| 29 |
| 30 private: |
| 31 explicit ByContextFinder(v8::Handle<v8::Context> context) |
| 32 : context_(context), found_(NULL) { |
| 33 } |
| 34 |
| 35 virtual bool Visit(content::RenderView* render_view) OVERRIDE { |
| 36 ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
| 37 if (helper && |
| 38 helper->dispatcher()->v8_context_set().GetByV8Context(context_)) { |
| 39 found_ = render_view; |
| 40 } |
| 41 return !found_; |
| 42 } |
| 43 |
| 44 v8::Handle<v8::Context> context_; |
| 45 content::RenderView* found_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(ByContextFinder); |
| 48 }; |
| 49 |
| 50 } // namespace |
| 51 |
| 52 void Debug(content::RenderView* render_view, const std::string& message) { |
| 53 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); |
| 54 } |
| 55 |
| 56 void Log(content::RenderView* render_view, const std::string& message) { |
| 57 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_LOG, message); |
| 58 } |
| 59 |
| 60 void Warn(content::RenderView* render_view, const std::string& message) { |
| 61 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); |
| 62 } |
| 63 |
| 64 void Error(content::RenderView* render_view, const std::string& message) { |
| 65 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); |
| 66 } |
| 67 |
| 68 void AddMessage(content::RenderView* render_view, |
| 69 content::ConsoleMessageLevel level, |
| 70 const std::string& message) { |
| 71 WebKit::WebView* web_view = render_view->GetWebView(); |
| 72 if (!web_view || !web_view->mainFrame()) |
| 73 return; |
| 74 WebKit::WebConsoleMessage::Level target_level = |
| 75 WebKit::WebConsoleMessage::LevelLog; |
| 76 switch (level) { |
| 77 case content::CONSOLE_MESSAGE_LEVEL_DEBUG: |
| 78 target_level = WebKit::WebConsoleMessage::LevelDebug; |
| 79 break; |
| 80 case content::CONSOLE_MESSAGE_LEVEL_LOG: |
| 81 target_level = WebKit::WebConsoleMessage::LevelLog; |
| 82 break; |
| 83 case content::CONSOLE_MESSAGE_LEVEL_WARNING: |
| 84 target_level = WebKit::WebConsoleMessage::LevelWarning; |
| 85 break; |
| 86 case content::CONSOLE_MESSAGE_LEVEL_ERROR: |
| 87 target_level = WebKit::WebConsoleMessage::LevelError; |
| 88 break; |
| 89 } |
| 90 web_view->mainFrame()->addMessageToConsole( |
| 91 WebKit::WebConsoleMessage(target_level, ASCIIToUTF16(message))); |
| 92 } |
| 93 |
| 94 void Debug(v8::Handle<v8::Context> context, const std::string& message) { |
| 95 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); |
| 96 } |
| 97 |
| 98 void Log(v8::Handle<v8::Context> context, const std::string& message) { |
| 99 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_LOG, message); |
| 100 } |
| 101 |
| 102 void Warn(v8::Handle<v8::Context> context, const std::string& message) { |
| 103 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); |
| 104 } |
| 105 |
| 106 void Error(v8::Handle<v8::Context> context, const std::string& message) { |
| 107 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); |
| 108 } |
| 109 |
| 110 void AddMessage(v8::Handle<v8::Context> context, |
| 111 content::ConsoleMessageLevel level, |
| 112 const std::string& message) { |
| 113 if (context.IsEmpty()) { |
| 114 LOG(WARNING) << "Could not log \"" << message << "\": no context given"; |
| 115 return; |
| 116 } |
| 117 content::RenderView* render_view = ByContextFinder::Find(context); |
| 118 if (!render_view) { |
| 119 LOG(WARNING) << "Could not log \"" << message << "\": no render view found"; |
| 120 return; |
| 121 } |
| 122 AddMessage(render_view, level, message); |
| 123 } |
| 124 |
| 125 } // namespace console |
| 126 } // namespace extensions |
OLD | NEW |