Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.webshare; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.content.Intent; | |
| 9 | |
| 10 import org.chromium.chrome.R; | |
| 11 import org.chromium.content.browser.ContentViewCore; | |
| 12 import org.chromium.content_public.browser.WebContents; | |
| 13 import org.chromium.mojo.system.MojoException; | |
| 14 import org.chromium.mojom.webshare.ShareService; | |
| 15 import org.chromium.ui.base.WindowAndroid; | |
| 16 | |
| 17 /** | |
| 18 * Android implementation of the ShareService service defined in | |
| 19 * third_party/WebKit/public/platform/modules/webshare/webshare.mojom. | |
| 20 */ | |
| 21 public class ShareServiceImpl implements ShareService { | |
| 22 private final WindowAndroid mWindow; | |
| 23 private final Context mContext; | |
| 24 | |
| 25 public ShareServiceImpl(WebContents webContents) { | |
| 26 mWindow = windowFromWebContents(webContents); | |
| 27 mContext = contextFromWindow(mWindow); | |
| 28 } | |
| 29 | |
| 30 @Override | |
| 31 public void close() {} | |
| 32 | |
| 33 @Override | |
| 34 public void onConnectionError(MojoException e) {} | |
| 35 | |
| 36 @Override | |
| 37 public void share(String title, String text, ShareResponse callback) { | |
| 38 if (mContext == null) { | |
| 39 callback.call("Share failed"); | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 String chooserTitle = mContext.getString(R.string.share_link_chooser_tit le); | |
| 44 Intent send = new Intent(Intent.ACTION_SEND); | |
| 45 send.setType("text/plain"); | |
| 46 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
| |
| 47 send.putExtra(Intent.EXTRA_TEXT, text); | |
| 48 | |
| 49 Intent chooser = Intent.createChooser(send, chooserTitle); | |
| 50 if (!mWindow.showIntent(chooser, null, null)) { | |
| 51 callback.call("Share failed"); | |
| 52 return; | |
| 53 } | |
| 54 | |
| 55 // Success. | |
| 56 // TODO(mgiuca): Wait until the user has made a choice, and report failu re if they cancel | |
| 57 // the picker or something else goes wrong. | |
| 58 callback.call(null); | |
| 59 } | |
| 60 | |
| 61 // |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.
| |
| 62 private static WindowAndroid windowFromWebContents(WebContents webContents) { | |
| 63 if (webContents == null) return null; | |
| 64 | |
| 65 ContentViewCore contentViewCore = ContentViewCore.fromWebContents(webCon tents); | |
| 66 if (contentViewCore == null) return null; | |
| 67 | |
| 68 return contentViewCore.getWindowAndroid(); | |
| 69 } | |
| 70 | |
| 71 // |window| can be null. Can return null. | |
| 72 private static Context contextFromWindow(WindowAndroid window) { | |
| 73 if (window == null) return null; | |
| 74 | |
| 75 return window.getActivity().get(); | |
| 76 } | |
| 77 } | |
| OLD | NEW |