Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/webshare/ShareServiceImpl.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webshare/ShareServiceImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/webshare/ShareServiceImpl.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4a69fea08b85a6f882501dce2acff8354efbe478 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/webshare/ShareServiceImpl.java |
| @@ -0,0 +1,77 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.webshare; |
| + |
| +import android.content.Context; |
| +import android.content.Intent; |
| + |
| +import org.chromium.chrome.R; |
| +import org.chromium.content.browser.ContentViewCore; |
| +import org.chromium.content_public.browser.WebContents; |
| +import org.chromium.mojo.system.MojoException; |
| +import org.chromium.mojom.webshare.ShareService; |
| +import org.chromium.ui.base.WindowAndroid; |
| + |
| +/** |
| + * Android implementation of the ShareService service defined in |
| + * third_party/WebKit/public/platform/modules/webshare/webshare.mojom. |
| + */ |
| +public class ShareServiceImpl implements ShareService { |
| + private final WindowAndroid mWindow; |
| + private final Context mContext; |
| + |
| + public ShareServiceImpl(WebContents webContents) { |
| + mWindow = windowFromWebContents(webContents); |
| + mContext = contextFromWindow(mWindow); |
| + } |
| + |
| + @Override |
| + public void close() {} |
| + |
| + @Override |
| + public void onConnectionError(MojoException e) {} |
| + |
| + @Override |
| + public void share(String title, String text, ShareResponse callback) { |
| + if (mContext == null) { |
| + callback.call("Share failed"); |
| + return; |
| + } |
| + |
| + String chooserTitle = mContext.getString(R.string.share_link_chooser_title); |
| + Intent send = new Intent(Intent.ACTION_SEND); |
| + send.setType("text/plain"); |
| + send.putExtra(Intent.EXTRA_SUBJECT, text); |
|
gone
2016/07/28 16:03:20
Shouldn't this be title?
Matt Giuca
2016/07/29 01:43:03
Done. #oops
|
| + send.putExtra(Intent.EXTRA_TEXT, text); |
| + |
| + Intent chooser = Intent.createChooser(send, chooserTitle); |
| + if (!mWindow.showIntent(chooser, null, null)) { |
| + callback.call("Share failed"); |
| + return; |
| + } |
| + |
| + // Success. |
| + // TODO(mgiuca): Wait until the user has made a choice, and report failure if they cancel |
| + // the picker or something else goes wrong. |
| + callback.call(null); |
| + } |
| + |
| + // |webContents| can be null. Can return null. |
|
gone
2016/07/28 16:03:20
Use real Javadoc-style comments. Optionally use t
Matt Giuca
2016/07/29 01:43:03
Done.
|
| + private static WindowAndroid windowFromWebContents(WebContents webContents) { |
| + if (webContents == null) return null; |
| + |
| + ContentViewCore contentViewCore = ContentViewCore.fromWebContents(webContents); |
| + if (contentViewCore == null) return null; |
| + |
| + return contentViewCore.getWindowAndroid(); |
| + } |
| + |
| + // |window| can be null. Can return null. |
| + private static Context contextFromWindow(WindowAndroid window) { |
| + if (window == null) return null; |
| + |
| + return window.getActivity().get(); |
| + } |
| +} |