Add tracing for WebviewChromium callbacks

Bug: 10488419

Depends on I5d0d2e253499741c5f9454582988c99e42dc0a81

Change-Id: I92a6477486528b49b7786564d746d69ec4bc8666
(cherry picked from commit 081c438a8201fe2d1248f487808b3bae6706aa7e)
diff --git a/chromium/java/com/android/webview/chromium/WebViewContentsClientAdapter.java b/chromium/java/com/android/webview/chromium/WebViewContentsClientAdapter.java
index cfcfa35..dc3d647 100644
--- a/chromium/java/com/android/webview/chromium/WebViewContentsClientAdapter.java
+++ b/chromium/java/com/android/webview/chromium/WebViewContentsClientAdapter.java
@@ -73,7 +73,10 @@
  * choose the latter, because it makes for a cleaner design.
  */
 public class WebViewContentsClientAdapter extends AwContentsClient {
-    private static final String TAG = "ContentViewClientAdapter";
+    // TAG is chosen for consistency with classic webview tracing.
+    private static final String TAG = "WebViewCallback";
+    // Enables API callback tracing
+    private static final boolean TRACE = android.webkit.DebugFlags.TRACE_CALLBACK;
     // The WebView instance that this adapter is serving.
     private final WebView mWebView;
     // The WebViewClient instance that was passed to WebView.setWebViewClient().
@@ -227,6 +230,7 @@
     public void getVisitedHistory(ValueCallback<String[]> callback) {
         TraceEvent.begin();
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "getVisitedHistory");
             mWebChromeClient.getVisitedHistory(callback);
         }
         TraceEvent.end();
@@ -238,6 +242,7 @@
     @Override
     public void doUpdateVisitedHistory(String url, boolean isReload) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "doUpdateVisitedHistory=" + url + " reload=" + isReload);
         mWebViewClient.doUpdateVisitedHistory(mWebView, url, isReload);
         TraceEvent.end();
     }
@@ -249,6 +254,7 @@
     public void onProgressChanged(int progress) {
         TraceEvent.begin();
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onProgressChanged=" + progress);
             mWebChromeClient.onProgressChanged(mWebView, progress);
         }
         TraceEvent.end();
@@ -260,6 +266,7 @@
     @Override
     public InterceptedRequestData shouldInterceptRequest(String url) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "shouldInterceptRequest=" + url);
         WebResourceResponse response = mWebViewClient.shouldInterceptRequest(mWebView, url);
         TraceEvent.end();
         if (response == null) return null;
@@ -275,6 +282,7 @@
     @Override
     public boolean shouldOverrideUrlLoading(String url) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "shouldOverrideUrlLoading=" + url);
         boolean result = mWebViewClient.shouldOverrideUrlLoading(mWebView, url);
         TraceEvent.end();
         return result;
@@ -286,6 +294,7 @@
     @Override
     public void onUnhandledKeyEvent(KeyEvent event) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "onUnhandledKeyEvent");
         mWebViewClient.onUnhandledKeyEvent(mWebView, event);
         TraceEvent.end();
     }
@@ -298,6 +307,7 @@
         TraceEvent.begin();
         boolean result;
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onConsoleMessage");
             result = mWebChromeClient.onConsoleMessage(consoleMessage);
         } else {
             result = false;
@@ -314,6 +324,7 @@
             boolean isDoneCounting) {
         if (mFindListener == null) return;
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "onFindResultReceived");
         mFindListener.onFindResultReceived(activeMatchOrdinal, numberOfMatches, isDoneCounting);
         TraceEvent.end();
     }
@@ -325,6 +336,7 @@
     public void onNewPicture(Picture picture) {
         if (mPictureListener == null) return;
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "onNewPicture");
         mPictureListener.onNewPicture(mWebView, picture);
         TraceEvent.end();
     }
@@ -332,6 +344,7 @@
     @Override
     public void onLoadResource(String url) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "onLoadResource=" + url);
         mWebViewClient.onLoadResource(mWebView, url);
         TraceEvent.end();
     }
@@ -343,6 +356,7 @@
         TraceEvent.begin();
         boolean result;
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onCreateWindow");
             result = mWebChromeClient.onCreateWindow(mWebView, isDialog, isUserGesture, m);
         } else {
             result = false;
@@ -358,6 +372,7 @@
     public void onCloseWindow() {
         TraceEvent.begin();
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onCloseWindow");
             mWebChromeClient.onCloseWindow(mWebView);
         }
         TraceEvent.end();
@@ -370,6 +385,7 @@
     public void onRequestFocus() {
         TraceEvent.begin();
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onRequestFocus");
             mWebChromeClient.onRequestFocus(mWebView);
         }
         TraceEvent.end();
@@ -382,6 +398,7 @@
     public void onReceivedTouchIconUrl(String url, boolean precomposed) {
         TraceEvent.begin();
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onReceivedTouchIconUrl=" + url);
             mWebChromeClient.onReceivedTouchIconUrl(mWebView, url, precomposed);
         }
         TraceEvent.end();
@@ -394,6 +411,7 @@
     public void onReceivedIcon(Bitmap bitmap) {
         TraceEvent.begin();
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onReceivedIcon");
             mWebChromeClient.onReceivedIcon(mWebView, bitmap);
         }
         TraceEvent.end();
@@ -405,6 +423,7 @@
     @Override
     public void onPageStarted(String url) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "onPageStarted=" + url);
         mWebViewClient.onPageStarted(mWebView, url, mWebView.getFavicon());
         TraceEvent.end();
     }
@@ -415,6 +434,7 @@
     @Override
     public void onPageFinished(String url) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "onPageFinished=" + url);
         mWebViewClient.onPageFinished(mWebView, url);
         TraceEvent.end();
 
@@ -437,6 +457,7 @@
                 public void run() {
                     UnimplementedWebViewApi.invoke();
                     if (mPictureListener != null) {
+                        if (TRACE) Log.d(TAG, "onPageFinished-fake");
                         mPictureListener.onNewPicture(mWebView, new Picture());
                     }
                 }
@@ -456,6 +477,7 @@
             description = ErrorStrings.getString(errorCode, mWebView.getContext());
         }
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "onReceivedError=" + failingUrl);
         mWebViewClient.onReceivedError(mWebView, errorCode, description, failingUrl);
         TraceEvent.end();
     }
@@ -467,6 +489,7 @@
     public void onReceivedTitle(String title) {
         TraceEvent.begin();
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onReceivedTitle");
             mWebChromeClient.onReceivedTitle(mWebView, title);
         }
         TraceEvent.end();
@@ -485,6 +508,7 @@
         //    dispatchKeyEvent().
         if (event.isSystem()) return true;
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "shouldOverrideKeyEvent");
         boolean result = mWebViewClient.shouldOverrideKeyEvent(mWebView, event);
         TraceEvent.end();
         return result;
@@ -498,6 +522,7 @@
     // TODO: Delete this method when removed from base class.
     public void onStartContentIntent(Context context, String contentUrl) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "shouldOverrideUrlLoading=" + contentUrl);
         mWebViewClient.shouldOverrideUrlLoading(mWebView, contentUrl);
         TraceEvent.end();
     }
@@ -507,6 +532,7 @@
             GeolocationPermissions.Callback callback) {
         TraceEvent.begin();
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onGeolocationPermissionsShowPrompt");
             mWebChromeClient.onGeolocationPermissionsShowPrompt(origin, callback);
         }
         TraceEvent.end();
@@ -516,6 +542,7 @@
     public void onGeolocationPermissionsHidePrompt() {
         TraceEvent.begin();
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onGeolocationPermissionsHidePrompt");
             mWebChromeClient.onGeolocationPermissionsHidePrompt();
         }
         TraceEvent.end();
@@ -564,6 +591,7 @@
         if (mWebChromeClient != null) {
             final JsPromptResult res =
                     new JsPromptResultReceiverAdapter(receiver).getPromptResult();
+            if (TRACE) Log.d(TAG, "onJsAlert");
             if (!mWebChromeClient.onJsAlert(mWebView, url, message, res)) {
                 new JsDialogHelper(res, JsDialogHelper.ALERT, null, message, url)
                         .showDialog(mWebView.getContext());
@@ -580,6 +608,7 @@
         if (mWebChromeClient != null) {
             final JsPromptResult res =
                     new JsPromptResultReceiverAdapter(receiver).getPromptResult();
+            if (TRACE) Log.d(TAG, "onJsBeforeUnload");
             if (!mWebChromeClient.onJsBeforeUnload(mWebView, url, message, res)) {
                 new JsDialogHelper(res, JsDialogHelper.UNLOAD, null, message, url)
                         .showDialog(mWebView.getContext());
@@ -596,6 +625,7 @@
         if (mWebChromeClient != null) {
             final JsPromptResult res =
                     new JsPromptResultReceiverAdapter(receiver).getPromptResult();
+            if (TRACE) Log.d(TAG, "onJsConfirm");
             if (!mWebChromeClient.onJsConfirm(mWebView, url, message, res)) {
                 new JsDialogHelper(res, JsDialogHelper.CONFIRM, null, message, url)
                         .showDialog(mWebView.getContext());
@@ -613,6 +643,7 @@
         if (mWebChromeClient != null) {
             final JsPromptResult res =
                     new JsPromptResultReceiverAdapter(receiver).getPromptResult();
+            if (TRACE) Log.d(TAG, "onJsPrompt");
             if (!mWebChromeClient.onJsPrompt(mWebView, url, message, defaultValue, res)) {
                 new JsDialogHelper(res, JsDialogHelper.PROMPT, defaultValue, message, url)
                         .showDialog(mWebView.getContext());
@@ -626,6 +657,7 @@
     @Override
     public void onReceivedHttpAuthRequest(AwHttpAuthHandler handler, String host, String realm) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "onReceivedHttpAuthRequest=" + host);
         mWebViewClient.onReceivedHttpAuthRequest(mWebView,
                 new AwHttpAuthHandlerAdapter(handler), host, realm);
         TraceEvent.end();
@@ -652,6 +684,7 @@
             }
         };
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "onReceivedSslError");
         mWebViewClient.onReceivedSslError(mWebView, handler, error);
         TraceEvent.end();
     }
@@ -659,6 +692,7 @@
     @Override
     public void onReceivedLoginRequest(String realm, String account, String args) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "onReceivedLoginRequest=" + realm);
         mWebViewClient.onReceivedLoginRequest(mWebView, realm, account, args);
         TraceEvent.end();
     }
@@ -666,6 +700,7 @@
     @Override
     public void onFormResubmission(Message dontResend, Message resend) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, "onFormResubmission");
         mWebViewClient.onFormResubmission(mWebView, dontResend, resend);
         TraceEvent.end();
     }
@@ -678,6 +713,7 @@
                                 long contentLength) {
         if (mDownloadListener != null) {
             TraceEvent.begin();
+            if (TRACE) Log.d(TAG, "onDownloadStart");
             mDownloadListener.onDownloadStart(url,
                                               userAgent,
                                               contentDisposition,
@@ -690,6 +726,7 @@
     @Override
     public void onScaleChangedScaled(float oldScale, float newScale) {
         TraceEvent.begin();
+        if (TRACE) Log.d(TAG, " onScaleChangedScaled");
         mWebViewClient.onScaleChanged(mWebView, oldScale, newScale);
         TraceEvent.end();
     }
@@ -698,6 +735,7 @@
     public void onShowCustomView(View view, CustomViewCallback cb) {
         TraceEvent.begin();
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onShowCustomView");
             mWebChromeClient.onShowCustomView(view, cb);
         }
         TraceEvent.end();
@@ -707,6 +745,7 @@
     public void onHideCustomView() {
         TraceEvent.begin();
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "onHideCustomView");
             mWebChromeClient.onHideCustomView();
         }
         TraceEvent.end();
@@ -717,6 +756,7 @@
         TraceEvent.begin();
         View result;
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "getVideoLoadingProgressView");
             result = mWebChromeClient.getVideoLoadingProgressView();
         } else {
             result = null;
@@ -730,6 +770,7 @@
         TraceEvent.begin();
         Bitmap result;
         if (mWebChromeClient != null) {
+            if (TRACE) Log.d(TAG, "getDefaultVideoPoster");
             result = mWebChromeClient.getDefaultVideoPoster();
         } else {
             result = null;