From 3141e0a62abe57e93e5d716895a2a57cc052bb50 Mon Sep 17 00:00:00 2001 From: Ben Murdoch Date: Thu, 28 Jan 2010 15:06:32 +0000 Subject: [PATCH] Add new ConsoleMessage class that adds more information (such as the Message Log Level from WebCore) and a new overload of onConsoleMessage that takes this as a parameter. This change requires a corresponding change in external/webkit. BUG=2401755 Change-Id: Iea2c58e93172240f162d8ad57eb2c8f2352f8162 --- api/current.xml | 117 +++++++++++++++++- core/java/android/webkit/CallbackProxy.java | 42 ++++++- core/java/android/webkit/ConsoleMessage.java | 64 ++++++++++ core/java/android/webkit/WebChromeClient.java | 18 ++- core/java/android/webkit/WebViewCore.java | 7 +- 5 files changed, 242 insertions(+), 6 deletions(-) create mode 100644 core/java/android/webkit/ConsoleMessage.java diff --git a/api/current.xml b/api/current.xml index 65eda2ef056b8..0995cfddbfbcf 100644 --- a/api/current.xml +++ b/api/current.xml @@ -186353,6 +186353,108 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -187851,6 +187953,19 @@ + + + + = numberOfMessageLevels) { + msgLevel = 0; + } + + ConsoleMessage.MessageLevel messageLevel = + ConsoleMessage.MessageLevel.values()[msgLevel]; + + if (!mWebChromeClient.onConsoleMessage(new ConsoleMessage(message, sourceID, + lineNumber, messageLevel))) { + // If false was returned the user did not provide their own console function so + // we should output some default messages to the system log. + String logTag = "Web Console"; + String logMessage = message + " at " + sourceID + ":" + lineNumber; + + switch (messageLevel) { + case TIP: + Log.v(logTag, logMessage); + break; + case LOG: + Log.i(logTag, logMessage); + break; + case WARNING: + Log.w(logTag, logMessage); + break; + case ERROR: + Log.e(logTag, logMessage); + break; + case DEBUG: + Log.d(logTag, logMessage); + break; + } + } + break; case GET_VISITED_HISTORY: @@ -1286,8 +1321,10 @@ class CallbackProxy extends Handler { * occurred. * @param sourceID The filename of the source file in which the error * occurred. + * @param msgLevel The message level, corresponding to the MessageLevel enum in + * WebCore/page/Console.h */ - public void addMessageToConsole(String message, int lineNumber, String sourceID) { + public void addMessageToConsole(String message, int lineNumber, String sourceID, int msgLevel) { if (mWebChromeClient == null) { return; } @@ -1296,6 +1333,7 @@ class CallbackProxy extends Handler { msg.getData().putString("message", message); msg.getData().putString("sourceID", sourceID); msg.getData().putInt("lineNumber", lineNumber); + msg.getData().putInt("msgLevel", msgLevel); sendMessage(msg); } diff --git a/core/java/android/webkit/ConsoleMessage.java b/core/java/android/webkit/ConsoleMessage.java new file mode 100644 index 0000000000000..a9c351a7351dc --- /dev/null +++ b/core/java/android/webkit/ConsoleMessage.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.webkit; + +/** + * Public class representing a JavaScript console message from WebCore. This could be a issued + * by a call to one of the console logging functions (e.g. + * console.log('...')) or a JavaScript error on the page. To receive notifications + * of these messages, override the + * {@link WebChromeClient#onConsoleMessage(ConsoleMessage)} function. + */ +public class ConsoleMessage { + + // This must be kept in sync with the WebCore enum in WebCore/page/Console.h + public enum MessageLevel { + TIP, + LOG, + WARNING, + ERROR, + DEBUG + }; + + private MessageLevel mLevel; + private String mMessage; + private String mSourceId; + private int mLineNumber; + + public ConsoleMessage(String message, String sourceId, int lineNumber, MessageLevel msgLevel) { + mMessage = message; + mSourceId = sourceId; + mLineNumber = lineNumber; + mLevel = msgLevel; + } + + public MessageLevel messageLevel() { + return mLevel; + } + + public String message() { + return mMessage; + } + + public String sourceId() { + return mSourceId; + } + + public int lineNumber() { + return mLineNumber; + } +}; diff --git a/core/java/android/webkit/WebChromeClient.java b/core/java/android/webkit/WebChromeClient.java index f40b55c744076..1d5aac7fb3c03 100644 --- a/core/java/android/webkit/WebChromeClient.java +++ b/core/java/android/webkit/WebChromeClient.java @@ -262,8 +262,24 @@ public class WebChromeClient { * @param message The error message to report. * @param lineNumber The line number of the error. * @param sourceID The name of the source file that caused the error. + * @deprecated Use {@link #onConsoleMessage(ConsoleMessage) onConsoleMessage(ConsoleMessage)} + * instead. */ - public void onConsoleMessage(String message, int lineNumber, String sourceID) {} + @Deprecated + public void onConsoleMessage(String message, int lineNumber, String sourceID) { } + + /** + * Report a JavaScript console message to the host application. The ChromeClient + * should override this to process the log message as they see fit. + * @param consoleMessage Object containing details of the console message. + * @return true if the message is handled by the client. + */ + public boolean onConsoleMessage(ConsoleMessage consoleMessage) { + // Call the old version of this function for backwards compatability. + onConsoleMessage(consoleMessage.message(), consoleMessage.lineNumber(), + consoleMessage.sourceId()); + return false; + } /** * When not playing, video elements are represented by a 'poster' image. The diff --git a/core/java/android/webkit/WebViewCore.java b/core/java/android/webkit/WebViewCore.java index 6e45e39e96ff8..9c91919033535 100644 --- a/core/java/android/webkit/WebViewCore.java +++ b/core/java/android/webkit/WebViewCore.java @@ -260,9 +260,12 @@ final class WebViewCore { * @param message The message to add * @param lineNumber the line on which the error occurred * @param sourceID the filename of the source that caused the error. + * @param msgLevel the log level of this message. This is a value casted to int + * from WebCore::MessageLevel in WebCore/page/Console.h. */ - protected void addMessageToConsole(String message, int lineNumber, String sourceID) { - mCallbackProxy.addMessageToConsole(message, lineNumber, sourceID); + protected void addMessageToConsole(String message, int lineNumber, String sourceID, + int msgLevel) { + mCallbackProxy.addMessageToConsole(message, lineNumber, sourceID, msgLevel); } /**