blob: 6929202445e9ed4691bb5a5ef1fbbdb71b9c2d7e [file] [log] [blame]
Terry Wangfebbead2019-10-17 17:05:18 -07001/*
sidchhabraa7c8f8a2020-01-16 18:38:17 -08002 * Copyright (C) 2020 The Android Open Source Project
Terry Wangfebbead2019-10-17 17:05:18 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.server.appsearch;
17
sidchhabraa7c8f8a2020-01-16 18:38:17 -080018import android.annotation.NonNull;
Terry Wangfebbead2019-10-17 17:05:18 -070019import android.app.appsearch.IAppSearchManager;
20import android.content.Context;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080021import android.os.Binder;
22import android.os.UserHandle;
Terry Wangfebbead2019-10-17 17:05:18 -070023
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080024import com.android.internal.infra.AndroidFuture;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080025import com.android.internal.util.Preconditions;
Terry Wangfebbead2019-10-17 17:05:18 -070026import com.android.server.SystemService;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080027import com.android.server.appsearch.impl.AppSearchImpl;
sidchhabraa7c8f8a2020-01-16 18:38:17 -080028import com.android.server.appsearch.impl.FakeIcing;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080029import com.android.server.appsearch.impl.ImplInstanceManager;
Terry Wangfebbead2019-10-17 17:05:18 -070030
Alexander Dorokhinecc223452020-01-19 03:00:28 -080031import com.google.android.icing.proto.DocumentProto;
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080032import com.google.android.icing.proto.SchemaProto;
sidchhabraa7c8f8a2020-01-16 18:38:17 -080033import com.google.android.icing.proto.SearchResultProto;
34import com.google.android.icing.proto.SearchSpecProto;
35import com.google.android.icing.protobuf.InvalidProtocolBufferException;
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080036
Terry Wangfebbead2019-10-17 17:05:18 -070037/**
38 * TODO(b/142567528): add comments when implement this class
39 */
40public class AppSearchManagerService extends SystemService {
41
42 public AppSearchManagerService(Context context) {
43 super(context);
sidchhabraa7c8f8a2020-01-16 18:38:17 -080044 mFakeIcing = new FakeIcing();
Terry Wangfebbead2019-10-17 17:05:18 -070045 }
46
sidchhabraa7c8f8a2020-01-16 18:38:17 -080047 private final FakeIcing mFakeIcing;
48
Terry Wangfebbead2019-10-17 17:05:18 -070049 @Override
50 public void onStart() {
51 publishBinderService(Context.APP_SEARCH_SERVICE, new Stub());
52 }
53
54 private class Stub extends IAppSearchManager.Stub {
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080055 @Override
Alexander Dorokhine321e4b12020-01-18 11:15:00 -080056 public void setSchema(byte[] schemaBytes, boolean forceOverride, AndroidFuture callback) {
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080057 Preconditions.checkNotNull(schemaBytes);
58 Preconditions.checkNotNull(callback);
59 int callingUid = Binder.getCallingUidOrThrow();
60 int callingUserId = UserHandle.getUserId(callingUid);
61 long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080062 try {
63 SchemaProto schema = SchemaProto.parseFrom(schemaBytes);
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080064 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
Alexander Dorokhine321e4b12020-01-18 11:15:00 -080065 impl.setSchema(callingUid, schema, forceOverride);
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080066 callback.complete(null);
Alexander Dorokhine179c8b82020-01-11 00:17:48 -080067 } catch (Throwable t) {
68 callback.completeExceptionally(t);
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080069 } finally {
70 Binder.restoreCallingIdentity(callingIdentity);
Alexander Dorokhine179c8b82020-01-11 00:17:48 -080071 }
72 }
73
74 @Override
Alexander Dorokhinecc223452020-01-19 03:00:28 -080075 public void putDocument(byte[] documentBytes, AndroidFuture callback) {
76 Preconditions.checkNotNull(documentBytes);
77 Preconditions.checkNotNull(callback);
78 int callingUid = Binder.getCallingUidOrThrow();
79 int callingUserId = UserHandle.getUserId(callingUid);
80 long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhine179c8b82020-01-11 00:17:48 -080081 try {
Alexander Dorokhinecc223452020-01-19 03:00:28 -080082 DocumentProto document = DocumentProto.parseFrom(documentBytes);
83 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
84 impl.putDocument(callingUid, document);
85 callback.complete(null);
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080086 } catch (Throwable t) {
87 callback.completeExceptionally(t);
Alexander Dorokhinecc223452020-01-19 03:00:28 -080088 } finally {
89 Binder.restoreCallingIdentity(callingIdentity);
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080090 }
91 }
sidchhabraa7c8f8a2020-01-16 18:38:17 -080092 // TODO(sidchhabra):Init FakeIcing properly.
93 // TODO(sidchhabra): Do this in a threadpool.
94 @Override
95 public void query(@NonNull String queryExpression, @NonNull byte[] searchSpec,
96 AndroidFuture callback) {
97 Preconditions.checkNotNull(queryExpression);
98 Preconditions.checkNotNull(searchSpec);
99 SearchSpecProto searchSpecProto = null;
100 try {
101 searchSpecProto = SearchSpecProto.parseFrom(searchSpec);
102 } catch (InvalidProtocolBufferException e) {
103 throw new RuntimeException(e);
104 }
105 SearchResultProto searchResults =
106 mFakeIcing.query(queryExpression);
107 callback.complete(searchResults.toByteArray());
108 }
Terry Wangfebbead2019-10-17 17:05:18 -0700109 }
110}