Commit 00621ecd authored by Oksana Zhuravlova's avatar Oksana Zhuravlova Committed by Commit Bot

[remoteobjects] Add RemoteObjectGatewayProxy

This change adds an initial version of a class that establishes
the mojo connection with the renderer and calls
RemoteObjectGateway interface methods. Only the 'add interface'
functionality is implemented in this CL.

Bug: 794320
Test: local patch (https://crrev.com/c/2293476) where JavascriptInjectorImpl uses RemoteObjectGatewayProxy + JavaBridgeBasicsTest#testTypeOfInjectedObject test
Change-Id: I72c99bc1f9480197a9e90d216c20e0f422974adb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2285026
Commit-Queue: Oksana Zhuravlova <oksamyt@chromium.org>
Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Reviewed-by: default avatarShimi Zhang <ctzsm@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792264}
parent d2c9b053
...@@ -238,6 +238,7 @@ android_library("content_java") { ...@@ -238,6 +238,7 @@ android_library("content_java") {
"java/src/org/chromium/content/browser/remoteobjects/RemoteObjectAuditorImpl.java", "java/src/org/chromium/content/browser/remoteobjects/RemoteObjectAuditorImpl.java",
"java/src/org/chromium/content/browser/remoteobjects/RemoteObjectHostImpl.java", "java/src/org/chromium/content/browser/remoteobjects/RemoteObjectHostImpl.java",
"java/src/org/chromium/content/browser/remoteobjects/RemoteObjectImpl.java", "java/src/org/chromium/content/browser/remoteobjects/RemoteObjectImpl.java",
"java/src/org/chromium/content/browser/remoteobjects/RemoteObjectInjector.java",
"java/src/org/chromium/content/browser/remoteobjects/RemoteObjectRegistry.java", "java/src/org/chromium/content/browser/remoteobjects/RemoteObjectRegistry.java",
"java/src/org/chromium/content/browser/selection/AdditionalMenuItemProvider.java", "java/src/org/chromium/content/browser/selection/AdditionalMenuItemProvider.java",
"java/src/org/chromium/content/browser/selection/AdditionalMenuItemProviderImpl.java", "java/src/org/chromium/content/browser/selection/AdditionalMenuItemProviderImpl.java",
......
...@@ -54,6 +54,10 @@ class RemoteObjectHostImpl implements RemoteObjectHost { ...@@ -54,6 +54,10 @@ class RemoteObjectHostImpl implements RemoteObjectHost {
mRegistry = new WeakReference<>(registry); mRegistry = new WeakReference<>(registry);
} }
public RemoteObjectRegistry getRegistry() {
return mRegistry.get();
}
@Override @Override
public void getObject(int objectId, InterfaceRequest<RemoteObject> request) { public void getObject(int objectId, InterfaceRequest<RemoteObject> request) {
try (InterfaceRequest<RemoteObject> autoClose = request) { try (InterfaceRequest<RemoteObject> autoClose = request) {
......
// Copyright 2020 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.content.browser.remoteobjects;
import org.chromium.blink.mojom.RemoteObjectGateway;
import org.chromium.blink.mojom.RemoteObjectGatewayFactory;
import org.chromium.content_public.browser.RenderFrameHost;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.WebContentsObserver;
import org.chromium.mojo.bindings.InterfaceRequest;
import org.chromium.mojo.system.Pair;
import org.chromium.mojo.system.impl.CoreImpl;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Owned and constructed by JavascriptInjectorImpl.
* Could possibly be flattened into JavascriptInjectorImpl eventually, but may be nice to keep
* separate while there are separate codepaths.
*/
public final class RemoteObjectInjector extends WebContentsObserver {
/**
* Helper class for holding objects used to interact
* with the RemoteObjectGateway in the renderer.
*/
private static class RemoteObjectGatewayHelper {
public RemoteObjectGateway.Proxy gateway;
public RemoteObjectHostImpl host;
public RemoteObjectGatewayHelper(
RemoteObjectGateway.Proxy newGateway, RemoteObjectHostImpl newHost) {
gateway = newGateway;
host = newHost;
}
}
private final Set<Object> mRetainingSet = new HashSet<>();
private final Map<String, Pair<Object, Class<? extends Annotation>>> mInjectedObjects =
new HashMap<>();
private final Map<RenderFrameHost, RemoteObjectGatewayHelper> mRemoteObjectGatewayHelpers =
new HashMap<>();
public RemoteObjectInjector(WebContents webContents) {
super(webContents);
}
@Override
public void renderFrameCreated(int renderProcessId, int renderFrameId) {
// TODO(crbug.com/1105935): replicate the necessary logic from
// GinJavaBridgeDispatcherHost::RenderFrameCreated().
// TODO(crbug.com/1107555): get the RenderFrameHost
// from renderProcessId and renderFrameId.
}
public void addInterface(Object object, String name) {
WebContents webContents = mWebContents.get();
if (webContents == null) return;
// TODO(crbug.com/1105935): find a way to make requiredAnnotation available to
// mRemoteObjectHost when JavascriptInjectorImpl calls addInterface().
Class<? extends Annotation> requiredAnnotation = null;
mInjectedObjects.put(name, new Pair<>(object, requiredAnnotation));
// TODO(crbug.com/1105935): the objects need to be injected into all frames, not just the
// main one.
addInterfaceForFrame(webContents.getMainFrame(), object, name, requiredAnnotation);
}
private void addInterfaceForFrame(RenderFrameHost frameHost, Object object, String name,
Class<? extends Annotation> requiredAnnotation) {
RemoteObjectGatewayHelper helper =
getRemoteObjectGatewayHelperForFrame(frameHost, requiredAnnotation);
helper.gateway.addNamedObject(name, helper.host.getRegistry().getObjectId(object));
}
private RemoteObjectGatewayHelper getRemoteObjectGatewayHelperForFrame(
RenderFrameHost frameHost, Class<? extends Annotation> requiredAnnotation) {
// Only create one instance of RemoteObjectHostImpl per frame and store it in a map so it is
// reused in future calls.
if (!mRemoteObjectGatewayHelpers.containsKey(frameHost)) {
RemoteObjectRegistry registry = new RemoteObjectRegistry(mRetainingSet);
// Construct a RemoteObjectHost implementation.
RemoteObjectHostImpl host = new RemoteObjectHostImpl(
requiredAnnotation, new RemoteObjectAuditorImpl(), registry);
RemoteObjectGatewayFactory factory = frameHost.getRemoteInterfaces().getInterface(
RemoteObjectGatewayFactory.MANAGER);
Pair<RemoteObjectGateway.Proxy, InterfaceRequest<RemoteObjectGateway>> result =
RemoteObjectGateway.MANAGER.getInterfaceRequest(CoreImpl.getInstance());
factory.createRemoteObjectGateway(host, result.second);
mRemoteObjectGatewayHelpers.put(
frameHost, new RemoteObjectGatewayHelper(result.first, host));
}
return mRemoteObjectGatewayHelpers.get(frameHost);
}
}
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