Commit e295b3f2 authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[Sharing] Create a proxy to communicate with the SharingService

All Android JNI calls to native will be routed via SharingServiceProxy
which calls into SharingServiceProxyAndroid. SharingServiceProxyAndroid
then either handles the request directly or forwards it to one of the
sharing handlers.

TBR=estevenson@chromium.org

Bug: 996320, 997200
Change-Id: I0d30048938bf51439c19aa52b9d9e775dfc41fe3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1768339
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarAlex Chau <alexchau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#691108}
parent d388712b
......@@ -2627,6 +2627,7 @@ generate_jni("chrome_jni_headers") {
"java/src/org/chromium/chrome/browser/send_tab_to_self/TargetDeviceInfo.java",
"java/src/org/chromium/chrome/browser/sessions/SessionTabHelper.java",
"java/src/org/chromium/chrome/browser/sharing/SharingJNIBridge.java",
"java/src/org/chromium/chrome/browser/sharing/SharingServiceProxy.java",
"java/src/org/chromium/chrome/browser/sharing/click_to_call/ClickToCallMessageHandler.java",
"java/src/org/chromium/chrome/browser/sharing/shared_clipboard/SharedClipboardMessageHandler.java",
"java/src/org/chromium/chrome/browser/signin/IdentityServicesProvider.java",
......
......@@ -1434,6 +1434,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/sharing/click_to_call/ClickToCallUma.java",
"java/src/org/chromium/chrome/browser/sharing/shared_clipboard/SharedClipboardMessageHandler.java",
"java/src/org/chromium/chrome/browser/sharing/SharingJNIBridge.java",
"java/src/org/chromium/chrome/browser/sharing/SharingServiceProxy.java",
"java/src/org/chromium/chrome/browser/signin/AccountPickerDialogFragment.java",
"java/src/org/chromium/chrome/browser/signin/IdentityServicesProvider.java",
"java/src/org/chromium/chrome/browser/signin/ConfirmImportSyncDataDialog.java",
......
// Copyright 2019 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.sharing;
import org.chromium.base.Callback;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.profiles.Profile;
/**
* JNI bridge for SharingServiceProxy.
*/
public class SharingServiceProxy {
private static SharingServiceProxy sInstance;
private static long sNativeSharingServiceProxyAndroid;
/**
* @return Singleton instance for this class.
*/
public static SharingServiceProxy getInstance() {
if (sInstance != null) {
return sInstance;
}
if (sNativeSharingServiceProxyAndroid == 0) {
// The service hasn't been created yet.
Natives jni = SharingServiceProxyJni.get();
jni.initSharingService(Profile.getLastUsedProfile());
}
sInstance = new SharingServiceProxy();
return sInstance;
}
SharingServiceProxy() {}
@CalledByNative
private static void onProxyCreated(long nativeSharingServiceProxyAndroid) {
// There's only one profile in Android, therefore there should only be one service/proxy.
assert sNativeSharingServiceProxyAndroid == 0;
sNativeSharingServiceProxyAndroid = nativeSharingServiceProxyAndroid;
}
@CalledByNative
private static void onProxyDestroyed() {
sNativeSharingServiceProxyAndroid = 0;
}
/**
* Sends a shared clipboard message to the device specified by GUID.
* @param guid The guid of the device on the receiving end.
* @param message The text to send.
* @param callback The result of the operation. Runs |callback| with a
* org.chromium.chrome.browser.sharing.SharingSendMessageResult enum value.
*/
public void sendSharedClipboardMessage(
String guid, String message, Callback<Integer> callback) {
if (sNativeSharingServiceProxyAndroid == 0) {
callback.onResult(SharingSendMessageResult.INTERNAL_ERROR);
return;
}
Natives jni = SharingServiceProxyJni.get();
jni.sendSharedClipboardMessage(sNativeSharingServiceProxyAndroid, guid, message, callback);
}
@NativeMethods
interface Natives {
void initSharingService(Profile profile);
void sendSharedClipboardMessage(long nativeSharingServiceProxyAndroid, String guid,
String text, Callback<Integer> callback);
}
}
......@@ -11,7 +11,6 @@ import android.content.Intent;
import android.content.res.Resources;
import android.text.TextUtils;
import org.chromium.base.Callback;
import org.chromium.base.ContextUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.chrome.R;
......@@ -55,14 +54,4 @@ public class SharedClipboardMessageHandler {
notificationTitle, resources.getString(R.string.shared_clipboard_notification_text),
R.drawable.ic_devices_16dp, R.drawable.shared_clipboard_40dp);
}
/**
* Sends a message to the device specified by GUID.
* @param guid The guid of the device on the receiving end.
* @param message The payload to send.
* @param callback The result of the operation. Runs |callback| with a
* org.chromium.chrome.browser.sharing.SharingSendMessageResult enum value.
*/
public static native void sendMessageToDevice(
String guid, String message, Callback<Integer> callback);
}
......@@ -1604,6 +1604,8 @@ jumbo_split_static_library("browser") {
"sharing/sharing_service.h",
"sharing/sharing_service_factory.cc",
"sharing/sharing_service_factory.h",
"sharing/sharing_service_proxy_android.cc",
"sharing/sharing_service_proxy_android.h",
"sharing/sharing_sync_preference.cc",
"sharing/sharing_sync_preference.h",
"sharing/vapid_key_manager.cc",
......
......@@ -7,15 +7,12 @@
#include <memory>
#include <string>
#include "base/android/callback_android.h"
#include "base/android/jni_string.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/android/chrome_jni_headers/SharedClipboardMessageHandler_jni.h"
#include "chrome/browser/sharing/proto/shared_clipboard_message.pb.h"
#include "chrome/browser/sharing/proto/sharing_message.pb.h"
#include "chrome/browser/sharing/sharing_constants.h"
#include "chrome/browser/sharing/sharing_send_message_result.h"
#include "chrome/browser/sharing/sharing_service.h"
#include "components/sync_device_info/device_info.h"
#include "ui/base/clipboard/clipboard_buffer.h"
......@@ -45,28 +42,3 @@ void SharedClipboardMessageHandler::OnMessage(
env, base::android::ConvertUTF8ToJavaString(env, device_name));
}
void SharedClipboardMessageHandler::SendMessageToDevice(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& j_guid,
const base::android::JavaParamRef<jstring>& j_message,
const base::android::JavaParamRef<jobject>& j_runnable) {
std::string guid = base::android::ConvertJavaStringToUTF8(env, j_guid);
DCHECK(!guid.empty());
std::string message = base::android::ConvertJavaStringToUTF8(env, j_message);
chrome_browser_sharing::SharingMessage sharing_message;
sharing_message.mutable_shared_clipboard_message()->set_text(
std::move(message));
auto callback =
base::BindOnce(base::android::RunIntCallbackAndroid,
base::android::ScopedJavaGlobalRef<jobject>(j_runnable));
sharing_service_->SendMessageToDevice(
guid, kSendMessageTimeout, std::move(sharing_message),
base::BindOnce(
[](base::OnceCallback<void(int)> callback,
SharingSendMessageResult result) {
std::move(callback).Run(static_cast<int>(result));
},
std::move(callback)));
}
......@@ -5,7 +5,6 @@
#ifndef CHROME_BROWSER_SHARING_SHARED_CLIPBOARD_SHARED_CLIPBOARD_MESSAGE_HANDLER_ANDROID_H_
#define CHROME_BROWSER_SHARING_SHARED_CLIPBOARD_SHARED_CLIPBOARD_MESSAGE_HANDLER_ANDROID_H_
#include "base/android/jni_android.h"
#include "base/macros.h"
#include "chrome/browser/sharing/sharing_message_handler.h"
......@@ -21,12 +20,6 @@ class SharedClipboardMessageHandler : public SharingMessageHandler {
void OnMessage(
const chrome_browser_sharing::SharingMessage& message) override;
void SendMessageToDevice(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& j_guid,
const base::android::JavaParamRef<jstring>& j_message,
const base::android::JavaParamRef<jobject>& j_runnable);
private:
SharingService* sharing_service_ = nullptr;
......
......@@ -79,13 +79,13 @@ SharingService::SharingService(
if (base::FeatureList::IsEnabled(kClickToCallReceiver)) {
fcm_handler_->AddSharingHandler(
chrome_browser_sharing::SharingMessage::kClickToCallMessage,
&click_to_call_message_handler_);
sharing_service_proxy_android_.click_to_call_message_handler());
}
if (base::FeatureList::IsEnabled(kSharedClipboardReceiver)) {
fcm_handler_->AddSharingHandler(
chrome_browser_sharing::SharingMessage::kSharedClipboardMessage,
&shared_clipboard_message_handler_);
sharing_service_proxy_android_.shared_clipboard_message_handler());
}
#endif // defined(OS_ANDROID)
......
......@@ -25,8 +25,7 @@
#include "net/base/backoff_entry.h"
#if defined(OS_ANDROID)
#include "chrome/browser/sharing/click_to_call/click_to_call_message_handler_android.h"
#include "chrome/browser/sharing/shared_clipboard/shared_clipboard_message_handler_android.h"
#include "chrome/browser/sharing/sharing_service_proxy_android.h"
#endif // defined(OS_ANDROID)
namespace gcm {
......@@ -165,8 +164,7 @@ class SharingService : public KeyedService,
std::map<std::string, std::string> message_guids_;
#if defined(OS_ANDROID)
ClickToCallMessageHandler click_to_call_message_handler_;
SharedClipboardMessageHandler shared_clipboard_message_handler_{this};
SharingServiceProxyAndroid sharing_service_proxy_android_{this};
#endif // defined(OS_ANDROID)
base::WeakPtrFactory<SharingService> weak_ptr_factory_{this};
......
// Copyright 2019 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.
#include "chrome/browser/sharing/sharing_service_proxy_android.h"
#include "base/android/callback_android.h"
#include "base/android/jni_string.h"
#include "chrome/android/chrome_jni_headers/SharingServiceProxy_jni.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/sharing/sharing_constants.h"
#include "chrome/browser/sharing/sharing_send_message_result.h"
#include "chrome/browser/sharing/sharing_service.h"
#include "chrome/browser/sharing/sharing_service_factory.h"
void JNI_SharingServiceProxy_InitSharingService(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_profile) {
SharingService* service = SharingServiceFactory::GetForBrowserContext(
ProfileAndroid::FromProfileAndroid(j_profile));
DCHECK(service);
}
SharingServiceProxyAndroid::SharingServiceProxyAndroid(
SharingService* sharing_service)
: sharing_service_(sharing_service),
shared_clipboard_message_handler_(sharing_service) {
DCHECK(sharing_service_);
JNIEnv* env = base::android::AttachCurrentThread();
Java_SharingServiceProxy_onProxyCreated(env,
reinterpret_cast<intptr_t>(this));
}
SharingServiceProxyAndroid::~SharingServiceProxyAndroid() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_SharingServiceProxy_onProxyDestroyed(env);
}
void SharingServiceProxyAndroid::SendSharedClipboardMessage(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& j_guid,
const base::android::JavaParamRef<jstring>& j_text,
const base::android::JavaParamRef<jobject>& j_runnable) {
std::string guid = base::android::ConvertJavaStringToUTF8(env, j_guid);
DCHECK(!guid.empty());
std::string text = base::android::ConvertJavaStringToUTF8(env, j_text);
chrome_browser_sharing::SharingMessage sharing_message;
sharing_message.mutable_shared_clipboard_message()->set_text(std::move(text));
auto callback =
base::BindOnce(base::android::RunIntCallbackAndroid,
base::android::ScopedJavaGlobalRef<jobject>(j_runnable));
sharing_service_->SendMessageToDevice(
guid, kSendMessageTimeout, std::move(sharing_message),
base::BindOnce(
[](base::OnceCallback<void(int)> callback,
SharingSendMessageResult result) {
std::move(callback).Run(static_cast<int>(result));
},
std::move(callback)));
}
// Copyright 2019 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.
#ifndef CHROME_BROWSER_SHARING_SHARING_SERVICE_PROXY_ANDROID_H_
#define CHROME_BROWSER_SHARING_SHARING_SERVICE_PROXY_ANDROID_H_
#include "base/android/jni_android.h"
#include "base/macros.h"
#include "chrome/browser/sharing/click_to_call/click_to_call_message_handler_android.h"
#include "chrome/browser/sharing/shared_clipboard/shared_clipboard_message_handler_android.h"
#include "chrome/browser/sharing/sharing_message_handler.h"
class SharingService;
// Allows Android to query the Sharing Service for information.
class SharingServiceProxyAndroid {
public:
explicit SharingServiceProxyAndroid(SharingService* sharing_service);
~SharingServiceProxyAndroid();
SharedClipboardMessageHandler* shared_clipboard_message_handler() {
return &shared_clipboard_message_handler_;
}
ClickToCallMessageHandler* click_to_call_message_handler() {
return &click_to_call_message_handler_;
}
void SendSharedClipboardMessage(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& j_guid,
const base::android::JavaParamRef<jstring>& j_text,
const base::android::JavaParamRef<jobject>& j_runnable);
private:
SharingService* sharing_service_ = nullptr;
SharedClipboardMessageHandler shared_clipboard_message_handler_;
ClickToCallMessageHandler click_to_call_message_handler_;
DISALLOW_COPY_AND_ASSIGN(SharingServiceProxyAndroid);
};
#endif // CHROME_BROWSER_SHARING_SHARING_SERVICE_PROXY_ANDROID_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment