Chromium Code Reviews| 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->dispatcher()->v8_context_set().GetByV8Context(context_)) | |
|
Matt Perry
2013/03/15 22:10:27
are helper and dispatcher guaranteed to be non-NUL
not at google - send to devlin
2013/03/15 22:19:50
dispatcher yes, helper.. don't know, might as well
| |
| 38 found_ = render_view; | |
| 39 return !found_; | |
| 40 } | |
| 41 | |
| 42 v8::Handle<v8::Context> context_; | |
| 43 content::RenderView* found_; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(ByContextFinder); | |
| 46 }; | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 void Debug(content::RenderView* render_view, const std::string& message) { | |
| 51 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); | |
| 52 } | |
| 53 | |
| 54 void Log(content::RenderView* render_view, const std::string& message) { | |
| 55 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_LOG, message); | |
| 56 } | |
| 57 | |
| 58 void Warn(content::RenderView* render_view, const std::string& message) { | |
| 59 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); | |
| 60 } | |
| 61 | |
| 62 void Error(content::RenderView* render_view, const std::string& message) { | |
| 63 AddMessage(render_view, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); | |
| 64 } | |
| 65 | |
| 66 void AddMessage(content::RenderView* render_view, | |
| 67 content::ConsoleMessageLevel level, | |
| 68 const std::string& message) { | |
| 69 WebKit::WebView* web_view = render_view->GetWebView(); | |
| 70 if (!web_view || !web_view->mainFrame()) | |
| 71 return; | |
| 72 WebKit::WebConsoleMessage::Level target_level = | |
| 73 WebKit::WebConsoleMessage::LevelLog; | |
| 74 switch (level) { | |
| 75 case content::CONSOLE_MESSAGE_LEVEL_DEBUG: | |
| 76 target_level = WebKit::WebConsoleMessage::LevelDebug; | |
| 77 break; | |
| 78 case content::CONSOLE_MESSAGE_LEVEL_LOG: | |
| 79 target_level = WebKit::WebConsoleMessage::LevelLog; | |
| 80 break; | |
| 81 case content::CONSOLE_MESSAGE_LEVEL_WARNING: | |
| 82 target_level = WebKit::WebConsoleMessage::LevelWarning; | |
| 83 break; | |
| 84 case content::CONSOLE_MESSAGE_LEVEL_ERROR: | |
| 85 target_level = WebKit::WebConsoleMessage::LevelError; | |
| 86 break; | |
| 87 } | |
| 88 web_view->mainFrame()->addMessageToConsole( | |
| 89 WebKit::WebConsoleMessage(target_level, ASCIIToUTF16(message))); | |
| 90 } | |
| 91 | |
| 92 void Debug(v8::Handle<v8::Context> context, const std::string& message) { | |
| 93 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_DEBUG, message); | |
| 94 } | |
| 95 | |
| 96 void Log(v8::Handle<v8::Context> context, const std::string& message) { | |
| 97 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_LOG, message); | |
| 98 } | |
| 99 | |
| 100 void Warn(v8::Handle<v8::Context> context, const std::string& message) { | |
| 101 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_WARNING, message); | |
| 102 } | |
| 103 | |
| 104 void Error(v8::Handle<v8::Context> context, const std::string& message) { | |
| 105 AddMessage(context, content::CONSOLE_MESSAGE_LEVEL_ERROR, message); | |
| 106 } | |
| 107 | |
| 108 void AddMessage(v8::Handle<v8::Context> context, | |
| 109 content::ConsoleMessageLevel level, | |
| 110 const std::string& message) { | |
| 111 if (context.IsEmpty() || context->IsUndefined()) { | |
| 112 LOG(WARNING) << "Context was empty or undefined"; | |
| 113 return; | |
| 114 } | |
| 115 content::RenderView* render_view = ByContextFinder::Find(context); | |
| 116 if (!render_view) { | |
| 117 LOG(WARNING) << "No render view found for context"; | |
|
Matt Perry
2013/03/15 22:10:27
nit: log the message here as well?
not at google - send to devlin
2013/03/15 22:19:50
Done.
not at google - send to devlin
2013/03/15 22:19:50
Done.
| |
| 118 return; | |
| 119 } | |
| 120 AddMessage(render_view, level, message); | |
| 121 } | |
| 122 | |
| 123 } // namespace console | |
| 124 } // namespace extensions | |
| OLD | NEW |