Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/customtabs/PostMessageHandler.java

Issue 2422793002: HTML MessagePort as mojo::MessagePipeHandle (Closed)
Patch Set: Add missing ScopedAsyncTaskScheduler instance for the new unit tests; required by a recent change t… Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.customtabs; 5 package org.chromium.chrome.browser.customtabs;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.net.Uri; 8 import android.net.Uri;
9 import android.support.customtabs.CustomTabsService; 9 import android.support.customtabs.CustomTabsService;
10 import android.support.customtabs.CustomTabsSessionToken; 10 import android.support.customtabs.CustomTabsSessionToken;
11 import android.support.customtabs.PostMessageServiceConnection; 11 import android.support.customtabs.PostMessageServiceConnection;
12 12
13 import org.chromium.base.ContextUtils; 13 import org.chromium.base.ContextUtils;
14 import org.chromium.base.ThreadUtils; 14 import org.chromium.base.ThreadUtils;
15 import org.chromium.chrome.browser.tab.Tab; 15 import org.chromium.chrome.browser.tab.Tab;
16 import org.chromium.content.browser.AppWebMessagePort; 16 import org.chromium.content.browser.AppWebMessagePort;
17 import org.chromium.content.browser.AppWebMessagePortService;
18 import org.chromium.content.browser.AppWebMessagePortService.MessageChannelObser ver;
19 import org.chromium.content.browser.PostMessageSender;
20 import org.chromium.content.browser.PostMessageSender.PostMessageSenderDelegate;
21 import org.chromium.content_public.browser.MessagePort; 17 import org.chromium.content_public.browser.MessagePort;
22 import org.chromium.content_public.browser.MessagePort.MessageCallback; 18 import org.chromium.content_public.browser.MessagePort.MessageCallback;
23 import org.chromium.content_public.browser.WebContents; 19 import org.chromium.content_public.browser.WebContents;
24 import org.chromium.content_public.browser.WebContentsObserver; 20 import org.chromium.content_public.browser.WebContentsObserver;
25 21
26 /** 22 /**
27 * A class that handles postMessage communications with a designated {@link Cust omTabsSessionToken}. 23 * A class that handles postMessage communications with a designated {@link Cust omTabsSessionToken}.
28 */ 24 */
29 public class PostMessageHandler extends PostMessageServiceConnection { 25 public class PostMessageHandler extends PostMessageServiceConnection {
30 private static AppWebMessagePortService sService;
31
32 private final MessageCallback mMessageCallback; 26 private final MessageCallback mMessageCallback;
33 private WebContents mWebContents; 27 private WebContents mWebContents;
34 private boolean mMessageChannelCreated; 28 private boolean mMessageChannelCreated;
35 private boolean mBoundToService; 29 private boolean mBoundToService;
36 private AppWebMessagePort[] mChannel; 30 private AppWebMessagePort[] mChannel;
37 private PostMessageSender mPostMessageSender;
38 private PostMessageSenderDelegate mSenderDelegate;
39 private Uri mOrigin; 31 private Uri mOrigin;
40 32
41 private static AppWebMessagePortService getAppWebMessagePortService() {
42 if (sService == null) sService = new AppWebMessagePortService();
43 return sService;
44 }
45
46 /** 33 /**
47 * Basic constructor. Everytime the given {@link CustomTabsSessionToken} is associated with a 34 * Basic constructor. Everytime the given {@link CustomTabsSessionToken} is associated with a
48 * new {@link WebContents}, 35 * new {@link WebContents},
49 * {@link PostMessageHandler#reset(WebContents)} should be called to 36 * {@link PostMessageHandler#reset(WebContents)} should be called to
50 * reset all internal state. 37 * reset all internal state.
51 * @param session The {@link CustomTabsSessionToken} to establish the postMe ssage communication 38 * @param session The {@link CustomTabsSessionToken} to establish the postMe ssage communication
52 * with. 39 * with.
53 */ 40 */
54 public PostMessageHandler(CustomTabsSessionToken session) { 41 public PostMessageHandler(CustomTabsSessionToken session) {
55 super(session); 42 super(session);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 91
105 @Override 92 @Override
106 public void documentLoadedInFrame(long frameId, boolean isMainFrame) { 93 public void documentLoadedInFrame(long frameId, boolean isMainFrame) {
107 if (!isMainFrame || mChannel != null) return; 94 if (!isMainFrame || mChannel != null) return;
108 initializeWithWebContents(webContents); 95 initializeWithWebContents(webContents);
109 } 96 }
110 }; 97 };
111 } 98 }
112 99
113 private void initializeWithWebContents(final WebContents webContents) { 100 private void initializeWithWebContents(final WebContents webContents) {
114 final AppWebMessagePortService service = getAppWebMessagePortService(); 101 mChannel = (AppWebMessagePort[]) webContents.createMessageChannel();
115 mChannel = (AppWebMessagePort[]) webContents.createMessageChannel(servic e);
116 mChannel[0].setMessageCallback(mMessageCallback, null); 102 mChannel[0].setMessageCallback(mMessageCallback, null);
117 mSenderDelegate = new PostMessageSenderDelegate() {
118 @Override
119 public void postMessageToWeb(
120 String frameName, String message, String targetOrigin,
121 int[] sentPortIds) {
122 if (webContents.isDestroyed()) {
123 disconnectChannel();
124 unbindFromContext(ContextUtils.getApplicationContext());
125 return;
126 }
127 webContents.postMessageToFrame(
128 frameName, message, mOrigin.toString(), targetOrigin, se ntPortIds);
129 }
130 103
131 @Override 104 webContents.postMessageToFrame(
132 public void onPostMessageQueueEmpty() {} 105 null, "", mOrigin.toString(), "", new AppWebMessagePort[] {mChan nel[1]});
133 106
134 @Override 107 mMessageChannelCreated = true;
135 public boolean isPostMessageSenderReady() { 108 if (mBoundToService) notifyMessageChannelReady(null);
136 return true;
137 }
138 };
139 mPostMessageSender = new PostMessageSender(
140 mSenderDelegate, getAppWebMessagePortService());
141 service.addObserver(new MessageChannelObserver() {
142 @Override
143 public void onMessageChannelCreated() {
144 service.removeObserver(this);
145 if (mChannel == null) return;
146 mPostMessageSender.postMessage(
147 null, "", "", new AppWebMessagePort[] {mChannel[1]});
148 mMessageChannelCreated = true;
149 if (mBoundToService) notifyMessageChannelReady(null);
150 }
151 });
152 } 109 }
153 110
154 private void disconnectChannel() { 111 private void disconnectChannel() {
155 if (mChannel == null) return; 112 if (mChannel == null) return;
156 mChannel[0].close(); 113 mChannel[0].close();
157 mChannel = null; 114 mChannel = null;
158 mSenderDelegate = null;
159 mPostMessageSender = null;
160 mWebContents = null; 115 mWebContents = null;
161 } 116 }
162 117
163 /** 118 /**
164 * Sets the postMessage origin for this session to the given {@link Uri}. 119 * Sets the postMessage origin for this session to the given {@link Uri}.
165 * @param origin The origin value to be set. 120 * @param origin The origin value to be set.
166 */ 121 */
167 public void initializeWithOrigin(Uri origin) { 122 public void initializeWithOrigin(Uri origin) {
168 mOrigin = origin; 123 mOrigin = origin;
169 if (mWebContents != null && !mWebContents.isDestroyed()) { 124 if (mWebContents != null && !mWebContents.isDestroyed()) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 public void onPostMessageServiceConnected() { 160 public void onPostMessageServiceConnected() {
206 mBoundToService = true; 161 mBoundToService = true;
207 if (mMessageChannelCreated) notifyMessageChannelReady(null); 162 if (mMessageChannelCreated) notifyMessageChannelReady(null);
208 } 163 }
209 164
210 @Override 165 @Override
211 public void onPostMessageServiceDisconnected() { 166 public void onPostMessageServiceDisconnected() {
212 mBoundToService = false; 167 mBoundToService = false;
213 } 168 }
214 } 169 }
OLDNEW
« no previous file with comments | « android_webview/native/aw_contents.cc ('k') | chrome/browser/printing/print_dialog_cloud_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698