| OLD | NEW |
| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 89 |
| 103 @Override | 90 @Override |
| 104 public void documentLoadedInFrame(long frameId, boolean isMainFrame)
{ | 91 public void documentLoadedInFrame(long frameId, boolean isMainFrame)
{ |
| 105 if (!isMainFrame || mChannel != null) return; | 92 if (!isMainFrame || mChannel != null) return; |
| 106 initializeWithWebContents(webContents); | 93 initializeWithWebContents(webContents); |
| 107 } | 94 } |
| 108 }; | 95 }; |
| 109 } | 96 } |
| 110 | 97 |
| 111 private void initializeWithWebContents(final WebContents webContents) { | 98 private void initializeWithWebContents(final WebContents webContents) { |
| 112 final AppWebMessagePortService service = getAppWebMessagePortService(); | 99 mChannel = (AppWebMessagePort[]) webContents.createMessageChannel(); |
| 113 mChannel = (AppWebMessagePort[]) webContents.createMessageChannel(servic
e); | |
| 114 mChannel[0].setMessageCallback(mMessageCallback, null); | 100 mChannel[0].setMessageCallback(mMessageCallback, null); |
| 115 mSenderDelegate = new PostMessageSenderDelegate() { | |
| 116 @Override | |
| 117 public void postMessageToWeb( | |
| 118 String frameName, String message, String targetOrigin, | |
| 119 int[] sentPortIds) { | |
| 120 if (webContents.isDestroyed()) { | |
| 121 disconnectChannel(); | |
| 122 unbindFromContext(ContextUtils.getApplicationContext()); | |
| 123 return; | |
| 124 } | |
| 125 webContents.postMessageToFrame( | |
| 126 frameName, message, mOrigin.toString(), targetOrigin, se
ntPortIds); | |
| 127 } | |
| 128 | 101 |
| 129 @Override | 102 webContents.postMessageToFrame( |
| 130 public void onPostMessageQueueEmpty() {} | 103 null, "", mOrigin.toString(), "", new AppWebMessagePort[] {mChan
nel[1]}); |
| 131 | 104 |
| 132 @Override | 105 mMessageChannelCreated = true; |
| 133 public boolean isPostMessageSenderReady() { | 106 if (mBoundToService) notifyMessageChannelReady(null); |
| 134 return true; | |
| 135 } | |
| 136 }; | |
| 137 mPostMessageSender = new PostMessageSender( | |
| 138 mSenderDelegate, getAppWebMessagePortService()); | |
| 139 service.addObserver(new MessageChannelObserver() { | |
| 140 @Override | |
| 141 public void onMessageChannelCreated() { | |
| 142 service.removeObserver(this); | |
| 143 if (mChannel == null) return; | |
| 144 mPostMessageSender.postMessage( | |
| 145 null, "", "", new AppWebMessagePort[] {mChannel[1]}); | |
| 146 mMessageChannelCreated = true; | |
| 147 if (mBoundToService) notifyMessageChannelReady(null); | |
| 148 } | |
| 149 }); | |
| 150 } | 107 } |
| 151 | 108 |
| 152 private void disconnectChannel() { | 109 private void disconnectChannel() { |
| 153 if (mChannel == null) return; | 110 if (mChannel == null) return; |
| 154 mChannel[0].close(); | 111 mChannel[0].close(); |
| 155 mChannel = null; | 112 mChannel = null; |
| 156 mSenderDelegate = null; | |
| 157 mPostMessageSender = null; | |
| 158 mWebContents = null; | 113 mWebContents = null; |
| 159 } | 114 } |
| 160 | 115 |
| 161 /** | 116 /** |
| 162 * Sets the postMessage origin for this session to the given {@link Uri}. | 117 * Sets the postMessage origin for this session to the given {@link Uri}. |
| 163 * @param origin The origin value to be set. | 118 * @param origin The origin value to be set. |
| 164 */ | 119 */ |
| 165 public void initializeWithOrigin(Uri origin) { | 120 public void initializeWithOrigin(Uri origin) { |
| 166 mOrigin = origin; | 121 mOrigin = origin; |
| 167 if (mWebContents != null && !mWebContents.isDestroyed()) { | 122 if (mWebContents != null && !mWebContents.isDestroyed()) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 public void onPostMessageServiceConnected() { | 158 public void onPostMessageServiceConnected() { |
| 204 mBoundToService = true; | 159 mBoundToService = true; |
| 205 if (mMessageChannelCreated) notifyMessageChannelReady(null); | 160 if (mMessageChannelCreated) notifyMessageChannelReady(null); |
| 206 } | 161 } |
| 207 | 162 |
| 208 @Override | 163 @Override |
| 209 public void onPostMessageServiceDisconnected() { | 164 public void onPostMessageServiceDisconnected() { |
| 210 mBoundToService = false; | 165 mBoundToService = false; |
| 211 } | 166 } |
| 212 } | 167 } |
| OLD | NEW |