Skip to content

Commit

Permalink
[AW] Add ComponentsProviderService
Browse files Browse the repository at this point in the history
Add ComponentsProviderService, with two methods:
- void getFilesForComponent(String, ResultReceiver)
- boolean onNewVersion(String, String, String)

See design doc at go/wv-component-updater

Bug: 1171771
Test: run_webview_instrumentation_test_apk -f "*ComponentsProviderServiceTest*"
Change-Id: I4c475a4e932ebfd3ab991209bd41d86760be30f4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2667349
Commit-Queue: Laís Minchillo <laisminchillo@chromium.org>
Auto-Submit: Laís Minchillo <laisminchillo@chromium.org>
Reviewed-by: Joshua Pawlicki <waffles@chromium.org>
Reviewed-by: Robert Sesek <rsesek@chromium.org>
Reviewed-by: Mike West <mkwst@chromium.org>
Reviewed-by: Hazem Ashmawy <hazems@chromium.org>
Reviewed-by: Nate Fischer <ntfschr@chromium.org>
Reviewed-by: Mugdha Lakhani <nator@chromium.org>
Cr-Commit-Position: refs/heads/master@{#855669}
  • Loading branch information
Laís Minchillo authored and Chromium LUCI CQ committed Feb 19, 2021
1 parent 3c9e61d commit 700074f
Show file tree
Hide file tree
Showing 14 changed files with 442 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":webview_service">
</service> # DIFF-ANCHOR: 65cddb26
<service # DIFF-ANCHOR: c756cf8d
android:exported="true"
android:name="org.chromium.android_webview.services.ComponentsProviderService"
android:process=":webview_service"
android:visibleToInstantApps="true"
tools:ignore="ExportedService">
</service> # DIFF-ANCHOR: c756cf8d
<service # DIFF-ANCHOR: 5cda9608
android:exported="true"
android:name="org.chromium.android_webview.services.CrashReceiverService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":webview_service">
</service> # DIFF-ANCHOR: 65cddb26
<service # DIFF-ANCHOR: c756cf8d
android:exported="true"
android:name="org.chromium.android_webview.services.ComponentsProviderService"
android:process=":webview_service"
android:visibleToInstantApps="true"
tools:ignore="ExportedService">
</service> # DIFF-ANCHOR: c756cf8d
<service # DIFF-ANCHOR: 5cda9608
android:exported="true"
android:name="org.chromium.android_webview.services.CrashReceiverService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class ServiceNames {
"org.chromium.android_webview.services.MetricsBridgeService";
public static final String VARIATIONS_SEED_SERVER =
"org.chromium.android_webview.services.VariationsSeedServer";
public static final String AW_COMPONENTS_PROVIDER_SERVICE =
"org.chromium.android_webview.services.ComponentsProviderService";

private ServiceNames() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2021 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.android_webview.test.services;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.os.ResultReceiver;

import androidx.test.filters.MediumTest;
import androidx.test.filters.SmallTest;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.chromium.android_webview.services.ComponentsProviderService;
import org.chromium.android_webview.test.AwActivityTestRule;
import org.chromium.android_webview.test.AwJUnit4ClassRunner;
import org.chromium.base.ContextUtils;
import org.chromium.base.FileUtils;
import org.chromium.components.component_updater.IComponentsProviderService;

import java.io.File;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* Tests for {@link ComponentsProviderService}. These are not batched per class so the service is
* unbound and killed, and the process is restarted between tests.
*/
@RunWith(AwJUnit4ClassRunner.class)
public class ComponentsProviderServiceTest {
private ServiceConnectionHelper mConnection;
private IComponentsProviderService mService;
private Context mContext;
private File mTempDirectory;

@Before
public void setUp() throws TimeoutException {
mContext = ContextUtils.getApplicationContext();
mTempDirectory = new File(mContext.getFilesDir(), "tmp/");
Assert.assertTrue(mTempDirectory.exists() || mTempDirectory.mkdirs());

mConnection = new ServiceConnectionHelper(
new Intent(mContext, ComponentsProviderService.class), Context.BIND_AUTO_CREATE);
mService = IComponentsProviderService.Stub.asInterface(mConnection.getBinder());
}

@After
public void tearDown() {
mConnection.close();
Assert.assertTrue("Failed to cleanup temporary test files",
FileUtils.recursivelyDeleteFile(mTempDirectory, null));
Assert.assertTrue("Failed to cleanup cps test files",
FileUtils.recursivelyDeleteFile(
new File(mContext.getFilesDir(), "components/cps/"), null));
}

@Test
@SmallTest
public void testInvalidComponentId() throws Exception {
Assert.assertNull("Result bundle for an invalid componentId should be null",
getFilesForComponentSync("anInvalidComponentId"));
}

@Test
@MediumTest
public void testValidComponentId() throws Exception {
final String randomDirectoryName = "jaAFih32";
final String componentId = "testComponentA";
final String version = "1.0.0";

File directory = new File(mTempDirectory, randomDirectoryName + "/");
Assert.assertTrue(directory.exists() || directory.mkdirs());
File file = new File(directory, "file.test");
Assert.assertTrue(file.exists() || file.createNewFile());

Assert.assertTrue(mService.onNewVersion(componentId, directory.getAbsolutePath(), version));

Bundle resultData = getFilesForComponentSync(componentId);

Assert.assertNotNull(
"Bundle resultData for componentId " + componentId + " should not be null",
resultData);
HashMap<String, ParcelFileDescriptor> map =
(HashMap<String, ParcelFileDescriptor>) resultData.getSerializable(
ComponentsProviderService.KEY_RESULT);
Assert.assertNotNull(map);
Assert.assertEquals(1, map.size());
Assert.assertTrue(map.containsKey(file.getName()));
ParcelFileDescriptor fileDescriptor = map.get(file.getName());
Assert.assertTrue(fileDescriptor.getFileDescriptor().valid());
fileDescriptor.close();
}

@Test
@SmallTest
public void testOnNewVersion() throws Exception {
final String randomDirectoryName = "lJna65aF";
final String componentId = "testComponentB";
final String version = "2.0.0";

File directory = new File(mTempDirectory, randomDirectoryName + "/");
Assert.assertTrue(directory.exists() || directory.mkdirs());
File file = new File(directory, "file.test");
Assert.assertTrue(file.exists() || file.createNewFile());

Assert.assertTrue(mService.onNewVersion(componentId, directory.getAbsolutePath(), version));
Assert.assertFalse(file.exists());
}

private Bundle getFilesForComponentSync(String componentId) throws Exception {
CompletableFuture<Bundle> future = new CompletableFuture<>();
mService.getFilesForComponent(componentId, new ResultReceiver(null) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
future.complete(resultData);
}
});
return future.get(AwActivityTestRule.WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.chromium.android_webview.common.services.ServiceNames;
import org.chromium.android_webview.services.AwMinidumpUploadJobService;
import org.chromium.android_webview.services.ComponentsProviderService;
import org.chromium.android_webview.services.CrashReceiverService;
import org.chromium.android_webview.services.DeveloperModeContentProvider;
import org.chromium.android_webview.services.DeveloperUiService;
Expand Down Expand Up @@ -41,5 +42,8 @@ public void testServiceNamesValid() {
ServiceNames.METRICS_BRIDGE_SERVICE);
Assert.assertEquals("Incorrect class name constant", VariationsSeedServer.class.getName(),
ServiceNames.VARIATIONS_SEED_SERVER);
Assert.assertEquals("Incorrect class name constant",
ComponentsProviderService.class.getName(),
ServiceNames.AW_COMPONENTS_PROVIDER_SERVICE);
}
}
2 changes: 2 additions & 0 deletions android_webview/nonembedded/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ android_library("services_java") {
"java/src/org/chromium/android_webview/services/AwMinidumpUploadJobService.java",
"java/src/org/chromium/android_webview/services/AwMinidumpUploaderDelegate.java",
"java/src/org/chromium/android_webview/services/AwVariationsSeedFetcher.java",
"java/src/org/chromium/android_webview/services/ComponentsProviderService.java",
"java/src/org/chromium/android_webview/services/CrashReceiverService.java",
"java/src/org/chromium/android_webview/services/DeveloperModeContentProvider.java",
"java/src/org/chromium/android_webview/services/DeveloperUiService.java",
Expand All @@ -104,6 +105,7 @@ android_library("services_java") {
"//android_webview/proto:metrics_bridge_records_proto_java",
"//base:base_java",
"//components/background_task_scheduler:background_task_scheduler_task_ids_java",
"//components/component_updater/android:component_provider_service_aidl_java",
"//components/minidump_uploader:minidump_uploader_java",
"//components/variations/android:variations_java",
"//components/version_info/android:version_constants_java",
Expand Down
5 changes: 5 additions & 0 deletions android_webview/nonembedded/java/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"
android:process=":webview_apk"/> {# Explicit process required for monochrome compatibility. #}
<service android:name="org.chromium.android_webview.services.ComponentsProviderService"
android:exported="true"
android:visibleToInstantApps="true"
android:process=":webview_service" {# Explicit process required for monochrome compatibility. #}
tools:ignore="ExportedService" />
{% endif %}
{% endmacro %}
{{ common(manifest_package|default('com.android.webview'), library|default('libwebviewchromium.so')) }}
Expand Down
Loading

0 comments on commit 700074f

Please sign in to comment.