aboutsummaryrefslogtreecommitdiffstats
path: root/patches/chromium-theme-color.patch
blob: 5be56e2020ff2006537a1f93262ef24ac8cb0a1e (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
From 74774aff2ed94754205b330fbc32dc075068fb5a Mon Sep 17 00:00:00 2001
From: Danny Baumann <dannybaumann@web.de>
Date: Thu, 18 May 2017 11:04:40 +0200
Subject: [PATCH 1/2] Add support for getting updates for theme color changes.

---
 .../webview/chromium/WebViewChromium.java     |  9 +++++
 .../WebViewContentsClientAdapter.java         | 34 +++++++++++++++++++
 .../android_webview/AwContentsClient.java     |  2 ++
 .../AwContentsClientCallbackHelper.java       | 11 ++++++
 .../AwWebContentsObserver.java                | 15 ++++++++
 .../test/NullContentsClient.java              |  4 +++
 6 files changed, 75 insertions(+)

diff --git a/android_webview/glue/java/src/com/android/webview/chromium/WebViewChromium.java b/android_webview/glue/java/src/com/android/webview/chromium/WebViewChromium.java
index 8fe2100dcee3..ffd94c3d305b 100644
--- a/android_webview/glue/java/src/com/android/webview/chromium/WebViewChromium.java
+++ b/android_webview/glue/java/src/com/android/webview/chromium/WebViewChromium.java
@@ -11,6 +11,7 @@ import android.content.Intent;
 import android.content.res.Configuration;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
+import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.Picture;
 import android.graphics.Rect;
@@ -77,6 +78,7 @@ import org.chromium.components.embedder_support.application.ClassLoaderContextWr
 import org.chromium.content_public.browser.NavigationHistory;
 import org.chromium.content_public.browser.SmartClipProvider;
 import org.chromium.content_public.browser.UiThreadTaskTraits;
+import org.chromium.content_public.browser.WebContents;
 
 import java.io.BufferedWriter;
 import java.io.File;
@@ -1161,6 +1163,13 @@ class WebViewChromium implements WebViewProvider, WebViewProvider.ScrollDelegate
         return mAwContents.getMostRecentProgress();
     }
 
+    public int getThemeColor() {
+        WebContents webContents = mAwContents != null ? mAwContents.getWebContents() : null;
+        if (webContents == null) return Color.TRANSPARENT;
+        // No checkThread() because the value is cached java side (workaround for b/10533304).
+        return webContents.getThemeColor();
+    }
+
     @Override
     public int getContentHeight() {
         sWebViewApiCallSample.record(ApiCall.GET_CONTENT_HEIGHT);
diff --git a/android_webview/glue/java/src/com/android/webview/chromium/WebViewContentsClientAdapter.java b/android_webview/glue/java/src/com/android/webview/chromium/WebViewContentsClientAdapter.java
index 61a155a3131a..e955ad639e01 100644
--- a/android_webview/glue/java/src/com/android/webview/chromium/WebViewContentsClientAdapter.java
+++ b/android_webview/glue/java/src/com/android/webview/chromium/WebViewContentsClientAdapter.java
@@ -57,6 +57,8 @@ import org.chromium.base.task.PostTask;
 import org.chromium.content_public.browser.UiThreadTaskTraits;
 
 import java.lang.ref.WeakReference;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.security.Principal;
 import java.security.PrivateKey;
 import java.security.cert.X509Certificate;
@@ -101,6 +103,9 @@ class WebViewContentsClientAdapter extends SharedWebViewContentsClientAdapter {
     private WeakHashMap<AwPermissionRequest, WeakReference<PermissionRequestAdapter>>
             mOngoingPermissionRequests;
 
+    private static Method sWebChromeClientThemeColorMethod;
+    private static boolean sWebChromeClientThemeColorMethodInitialized;
+
     /**
      * Adapter constructor.
      *
@@ -111,6 +116,17 @@ class WebViewContentsClientAdapter extends SharedWebViewContentsClientAdapter {
         super(webView, webViewDelegate, context);
         try (ScopedSysTraceEvent event =
                         ScopedSysTraceEvent.scoped("WebViewContentsClientAdapter.constructor")) {
+
+            if (!sWebChromeClientThemeColorMethodInitialized) {
+                try {
+                    sWebChromeClientThemeColorMethod = WebChromeClient.class.getMethod(
+                            "onThemeColorChanged", WebView.class, Integer.TYPE);
+                } catch (Exception e) {
+                    // ignored
+                }
+                sWebChromeClientThemeColorMethodInitialized = true;
+            }
+
             mUiThreadHandler = new Handler() {
                 @Override
                 public void handleMessage(Message msg) {
@@ -1043,6 +1059,24 @@ class WebViewContentsClientAdapter extends SharedWebViewContentsClientAdapter {
         }
     }
 
+    /**
+     * @see AwContentsClient#onThemeColorChanged(int)
+     */
+    @Override
+    public void onThemeColorChanged(int color) {
+        try {
+            TraceEvent.begin("WebViewContentsClientAdapter.onThemeColorChanged");
+            if (mWebChromeClient != null && sWebChromeClientThemeColorMethod != null) {
+                if (TRACE) Log.d(TAG, "onThemeColorChanged=" + color);
+                sWebChromeClientThemeColorMethod.invoke(mWebChromeClient, mWebView, color);
+            }
+        } catch (IllegalAccessException | InvocationTargetException e) {
+            // ignored
+        } finally {
+            TraceEvent.end("WebViewContentsClientAdapter.onThemeColorChanged");
+        }
+    }
+
     private static class AwHttpAuthHandlerAdapter extends android.webkit.HttpAuthHandler {
         private AwHttpAuthHandler mAwHandler;
 
diff --git a/android_webview/java/src/org/chromium/android_webview/AwContentsClient.java b/android_webview/java/src/org/chromium/android_webview/AwContentsClient.java
index 3b3fc7c2d692..6bd34a30127e 100644
--- a/android_webview/java/src/org/chromium/android_webview/AwContentsClient.java
+++ b/android_webview/java/src/org/chromium/android_webview/AwContentsClient.java
@@ -161,6 +161,8 @@ public abstract class AwContentsClient {
 
     public abstract void onProgressChanged(int progress);
 
+    public abstract void onThemeColorChanged(int color);
+
     public abstract AwWebResourceResponse shouldInterceptRequest(
             AwWebResourceRequest request);
 
diff --git a/android_webview/java/src/org/chromium/android_webview/AwContentsClientCallbackHelper.java b/android_webview/java/src/org/chromium/android_webview/AwContentsClientCallbackHelper.java
index 365442427175..9a2eb9577e76 100644
--- a/android_webview/java/src/org/chromium/android_webview/AwContentsClientCallbackHelper.java
+++ b/android_webview/java/src/org/chromium/android_webview/AwContentsClientCallbackHelper.java
@@ -136,6 +136,9 @@ public class AwContentsClientCallbackHelper {
     private static final int MSG_ON_FORM_RESUBMISSION = 14;
     private static final int MSG_ON_SAFE_BROWSING_HIT = 15;
 
+    // Custom
+    private static final int MSG_ON_THEME_COLOR_CHANGED = 99;
+
     // Minimum period allowed between consecutive onNewPicture calls, to rate-limit the callbacks.
     private static final long ON_NEW_PICTURE_MIN_PERIOD_MILLIS = 500;
     // Timestamp of the most recent onNewPicture callback.
@@ -231,6 +234,10 @@ public class AwContentsClientCallbackHelper {
                     mContentsClient.onProgressChanged(msg.arg1);
                     break;
                 }
+                case MSG_ON_THEME_COLOR_CHANGED: {
+                    mContentsClient.onThemeColorChanged(msg.arg1);
+                    break;
+                }
                 case MSG_SYNTHESIZE_PAGE_LOADING: {
                     final String url = (String) msg.obj;
                     mContentsClient.onPageStarted(url);
@@ -343,6 +350,10 @@ public class AwContentsClientCallbackHelper {
         mHandler.sendMessage(mHandler.obtainMessage(MSG_SYNTHESIZE_PAGE_LOADING, url));
     }
 
+    public void postOnThemeColorChanged(int color) {
+        mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_THEME_COLOR_CHANGED, color, 0));
+    }
+
     public void postDoUpdateVisitedHistory(String url, boolean isReload) {
         DoUpdateVisitedHistoryInfo info = new DoUpdateVisitedHistoryInfo(url, isReload);
         mHandler.sendMessage(mHandler.obtainMessage(MSG_DO_UPDATE_VISITED_HISTORY, info));
diff --git a/android_webview/java/src/org/chromium/android_webview/AwWebContentsObserver.java b/android_webview/java/src/org/chromium/android_webview/AwWebContentsObserver.java
index 7bc0afb0e75c..6708c3184377 100644
--- a/android_webview/java/src/org/chromium/android_webview/AwWebContentsObserver.java
+++ b/android_webview/java/src/org/chromium/android_webview/AwWebContentsObserver.java
@@ -144,12 +144,27 @@ public class AwWebContentsObserver extends WebContentsObserver {
             });
         }
 
+        if (client != null) {
+            AwContents awContents = mAwContents.get();
+            WebContents contents = awContents != null ? awContents.getWebContents() : null;
+            if (contents != null) {
+                client.getCallbackHelper().postOnThemeColorChanged(contents.getThemeColor());
+            }
+        }
+
         if (client != null && navigation.isFragmentNavigation()) {
             // Note fragment navigations do not have a matching onPageStarted.
             client.getCallbackHelper().postOnPageFinished(url);
         }
     }
 
+    public void didChangeThemeColor(int color) {
+        AwContentsClient client = mAwContentsClient.get();
+        if (client != null) {
+            client.getCallbackHelper().postOnThemeColorChanged(color);
+        }
+    }
+
     public boolean didEverCommitNavigation() {
         return mCommittedNavigation;
     }
diff --git a/android_webview/test/shell/src/org/chromium/android_webview/test/NullContentsClient.java b/android_webview/test/shell/src/org/chromium/android_webview/test/NullContentsClient.java
index 5990446fa1c4..0825720dc258 100644
--- a/android_webview/test/shell/src/org/chromium/android_webview/test/NullContentsClient.java
+++ b/android_webview/test/shell/src/org/chromium/android_webview/test/NullContentsClient.java
@@ -72,6 +72,10 @@ public class NullContentsClient extends AwContentsClient {
     public void onProgressChanged(int progress) {
     }
 
+    @Override
+    public void onThemeColorChanged(int color) {
+    }
+
     @Override
     public AwWebResourceResponse shouldInterceptRequest(
             AwContentsClient.AwWebResourceRequest request) {
-- 
2.17.1