Skip to content

Commit 2b926e9

Browse files
committed
Introduce a NameResolver for Android's intent: URIs
1 parent 42e1829 commit 2b926e9

8 files changed

Lines changed: 946 additions & 9 deletions

File tree

binder/src/androidTest/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
<service android:name="io.grpc.binder.HostServices$HostService1" android:exported="false">
1212
<intent-filter>
1313
<action android:name="action1"/>
14+
<data android:scheme="scheme" android:host="authority" android:path="/path"/>
1415
</intent-filter>
1516
</service>
1617
<service android:name="io.grpc.binder.HostServices$HostService2" android:exported="false">
1718
<intent-filter>
1819
<action android:name="action2"/>
20+
<data android:scheme="scheme" android:host="authority" android:path="/path"/>
1921
</intent-filter>
2022
</service>
2123
</application>

binder/src/androidTest/java/io/grpc/binder/BinderChannelSmokeTest.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import android.content.Context;
2525
import android.content.Intent;
26+
import android.net.Uri;
2627
import android.os.Parcel;
2728
import android.os.Parcelable;
2829
import androidx.test.core.app.ApplicationProvider;
@@ -39,7 +40,6 @@
3940
import io.grpc.ManagedChannel;
4041
import io.grpc.Metadata;
4142
import io.grpc.MethodDescriptor;
42-
import io.grpc.NameResolverRegistry;
4343
import io.grpc.ServerCall;
4444
import io.grpc.ServerCall.Listener;
4545
import io.grpc.ServerCallHandler;
@@ -49,7 +49,6 @@
4949
import io.grpc.Status.Code;
5050
import io.grpc.StatusRuntimeException;
5151
import io.grpc.internal.GrpcUtil;
52-
import io.grpc.internal.testing.FakeNameResolverProvider;
5352
import io.grpc.stub.ClientCalls;
5453
import io.grpc.stub.MetadataUtils;
5554
import io.grpc.stub.ServerCalls;
@@ -59,6 +58,7 @@
5958
import java.io.IOException;
6059
import java.io.InputStream;
6160
import java.util.ArrayList;
61+
import java.util.concurrent.ExecutionException;
6262
import java.util.concurrent.Executors;
6363
import java.util.concurrent.atomic.AtomicReference;
6464
import org.junit.After;
@@ -77,7 +77,6 @@ public final class BinderChannelSmokeTest {
7777

7878
private static final int SLIGHTLY_MORE_THAN_ONE_BLOCK = 16 * 1024 + 100;
7979
private static final String MSG = "Some text which will be repeated many many times";
80-
private static final String SERVER_TARGET_URI = "fake://server";
8180
private static final Metadata.Key<PoisonParcelable> POISON_KEY =
8281
ParcelableUtils.metadataKey("poison-bin", PoisonParcelable.CREATOR);
8382

@@ -99,7 +98,6 @@ public final class BinderChannelSmokeTest {
9998
.setType(MethodDescriptor.MethodType.BIDI_STREAMING)
10099
.build();
101100

102-
FakeNameResolverProvider fakeNameResolverProvider;
103101
ManagedChannel channel;
104102
AtomicReference<Metadata> headersCapture = new AtomicReference<>();
105103
AtomicReference<PeerUid> clientUidCapture = new AtomicReference<>();
@@ -138,8 +136,6 @@ public void setUp() throws Exception {
138136
PeerUids.newPeerIdentifyingServerInterceptor());
139137

140138
AndroidComponentAddress serverAddress = HostServices.allocateService(appContext);
141-
fakeNameResolverProvider = new FakeNameResolverProvider(SERVER_TARGET_URI, serverAddress);
142-
NameResolverRegistry.getDefaultRegistry().register(fakeNameResolverProvider);
143139
HostServices.configureService(
144140
serverAddress,
145141
HostServices.serviceParamsBuilder()
@@ -163,10 +159,24 @@ public void setUp() throws Exception {
163159
.build();
164160
}
165161

162+
@Test
163+
public void testConnectViaIntentTargetUri() throws Exception {
164+
// TODO(jdcormie): Make this test good.
165+
channel = BinderChannelBuilder.forTarget("intent://foo/bar", appContext).build();
166+
// channel = BinderChannelBuilder.forTarget("android-app://com.foo.bar/authoritaaay",
167+
// appContext).build();
168+
ListenableFuture<String> resultFuture = doCall("Hello");
169+
try {
170+
resultFuture.get();
171+
} catch (ExecutionException ee) {
172+
StatusRuntimeException sre = (StatusRuntimeException) ee.getCause();
173+
assertThat(sre.getStatus().getCode()).isEqualTo(Code.UNIMPLEMENTED);
174+
}
175+
}
176+
166177
@After
167178
public void tearDown() throws Exception {
168179
channel.shutdownNow();
169-
NameResolverRegistry.getDefaultRegistry().deregister(fakeNameResolverProvider);
170180
HostServices.awaitServiceShutdown();
171181
}
172182

@@ -235,7 +245,11 @@ public void testStreamingCallOptionHeaders() throws Exception {
235245

236246
@Test
237247
public void testConnectViaTargetUri() throws Exception {
238-
channel = BinderChannelBuilder.forTarget(SERVER_TARGET_URI, appContext).build();
248+
// Compare with the <intent-filter> mapping in AndroidManifest.xml.
249+
channel =
250+
BinderChannelBuilder.forTarget(
251+
"intent://authority/path#Intent;action=action1;scheme=scheme;end;", appContext)
252+
.build();
239253
assertThat(doCall("Hello").get()).isEqualTo("Hello");
240254
}
241255

@@ -245,7 +259,10 @@ public void testConnectViaIntentFilter() throws Exception {
245259
channel =
246260
BinderChannelBuilder.forAddress(
247261
AndroidComponentAddress.forBindIntent(
248-
new Intent().setAction("action1").setPackage(appContext.getPackageName())),
262+
new Intent()
263+
.setAction("action1")
264+
.setData(Uri.parse("scheme://authority/path"))
265+
.setPackage(appContext.getPackageName())),
249266
appContext)
250267
.build();
251268
assertThat(doCall("Hello").get()).isEqualTo("Hello");

binder/src/main/java/io/grpc/binder/BinderChannelBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.grpc.ManagedChannel;
2929
import io.grpc.ManagedChannelBuilder;
3030
import io.grpc.binder.internal.BinderClientTransportFactory;
31+
import io.grpc.binder.internal.IntentNameResolverProvider;
3132
import io.grpc.internal.FixedObjectPool;
3233
import io.grpc.internal.ManagedChannelImplBuilder;
3334
import java.util.concurrent.Executor;
@@ -177,6 +178,9 @@ private BinderChannelBuilder(
177178
new ManagedChannelImplBuilder(
178179
directAddress, directAddress.getAuthority(), transportFactoryBuilder, null);
179180
} else {
181+
if (checkNotNull(target).startsWith("intent:")) {
182+
IntentNameResolverProvider.maybeCreateAndDefaultRegister(sourceContext);
183+
}
180184
managedChannelImplBuilder =
181185
new ManagedChannelImplBuilder(target, transportFactoryBuilder, null);
182186
}

0 commit comments

Comments
 (0)