1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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 */
16
17package com.android.statementservice;
18
19import android.app.Service;
20import android.content.Intent;
21import android.net.http.HttpResponseCache;
22import android.os.Bundle;
23import android.os.Handler;
24import android.os.HandlerThread;
25import android.os.IBinder;
26import android.os.Looper;
27import android.os.ResultReceiver;
28import android.util.Log;
29
30import com.android.statementservice.retriever.AbstractAsset;
31import com.android.statementservice.retriever.AbstractAssetMatcher;
32import com.android.statementservice.retriever.AbstractStatementRetriever;
33import com.android.statementservice.retriever.AbstractStatementRetriever.Result;
34import com.android.statementservice.retriever.AssociationServiceException;
35import com.android.statementservice.retriever.Relation;
36import com.android.statementservice.retriever.Statement;
37
38import org.json.JSONException;
39
40import java.io.File;
41import java.io.IOException;
42import java.util.ArrayList;
43import java.util.List;
44import java.util.concurrent.Callable;
45
46/**
47 * Handles com.android.statementservice.service.CHECK_ALL_ACTION intents.
48 */
49public final class DirectStatementService extends Service {
50    private static final String TAG = DirectStatementService.class.getSimpleName();
51
52    /**
53     * Returns true if every asset in {@code SOURCE_ASSET_DESCRIPTORS} is associated with {@code
54     * EXTRA_TARGET_ASSET_DESCRIPTOR} for {@code EXTRA_RELATION} relation.
55     *
56     * <p>Takes parameter {@code EXTRA_RELATION}, {@code SOURCE_ASSET_DESCRIPTORS}, {@code
57     * EXTRA_TARGET_ASSET_DESCRIPTOR}, and {@code EXTRA_RESULT_RECEIVER}.
58     */
59    public static final String CHECK_ALL_ACTION =
60            "com.android.statementservice.service.CHECK_ALL_ACTION";
61
62    /**
63     * Parameter for {@link #CHECK_ALL_ACTION}.
64     *
65     * <p>A relation string.
66     */
67    public static final String EXTRA_RELATION =
68            "com.android.statementservice.service.RELATION";
69
70    /**
71     * Parameter for {@link #CHECK_ALL_ACTION}.
72     *
73     * <p>An array of asset descriptors in JSON.
74     */
75    public static final String EXTRA_SOURCE_ASSET_DESCRIPTORS =
76            "com.android.statementservice.service.SOURCE_ASSET_DESCRIPTORS";
77
78    /**
79     * Parameter for {@link #CHECK_ALL_ACTION}.
80     *
81     * <p>An asset descriptor in JSON.
82     */
83    public static final String EXTRA_TARGET_ASSET_DESCRIPTOR =
84            "com.android.statementservice.service.TARGET_ASSET_DESCRIPTOR";
85
86    /**
87     * Parameter for {@link #CHECK_ALL_ACTION}.
88     *
89     * <p>A {@code ResultReceiver} instance that will be used to return the result. If the request
90     * failed, return {@link #RESULT_FAIL} and an empty {@link android.os.Bundle}. Otherwise, return
91     * {@link #RESULT_SUCCESS} and a {@link android.os.Bundle} with the result stored in {@link
92     * #IS_ASSOCIATED}.
93     */
94    public static final String EXTRA_RESULT_RECEIVER =
95            "com.android.statementservice.service.RESULT_RECEIVER";
96
97    /**
98     * A boolean bundle entry that stores the result of {@link #CHECK_ALL_ACTION}.
99     * This is set only if the service returns with {@code RESULT_SUCCESS}.
100     * {@code IS_ASSOCIATED} is true if and only if {@code FAILED_SOURCES} is empty.
101     */
102    public static final String IS_ASSOCIATED = "is_associated";
103
104    /**
105     * A String ArrayList bundle entry that stores sources that can't be verified.
106     */
107    public static final String FAILED_SOURCES = "failed_sources";
108
109    /**
110     * Returned by the service if the request is successfully processed. The caller should check
111     * the {@code IS_ASSOCIATED} field to determine if the association exists or not.
112     */
113    public static final int RESULT_SUCCESS = 0;
114
115    /**
116     * Returned by the service if the request failed. The request will fail if, for example, the
117     * input is not well formed, or the network is not available.
118     */
119    public static final int RESULT_FAIL = 1;
120
121    private static final long HTTP_CACHE_SIZE_IN_BYTES = 1 * 1024 * 1024;  // 1 MBytes
122    private static final String CACHE_FILENAME = "request_cache";
123
124    private AbstractStatementRetriever mStatementRetriever;
125    private Handler mHandler;
126    private HandlerThread mThread;
127    private HttpResponseCache mHttpResponseCache;
128
129    @Override
130    public void onCreate() {
131        mThread = new HandlerThread("DirectStatementService thread",
132                android.os.Process.THREAD_PRIORITY_BACKGROUND);
133        mThread.start();
134        onCreate(AbstractStatementRetriever.createDirectRetriever(this), mThread.getLooper(),
135                getCacheDir());
136    }
137
138    /**
139     * Creates a DirectStatementService with the dependencies passed in for easy testing.
140     */
141    public void onCreate(AbstractStatementRetriever statementRetriever, Looper looper,
142                         File cacheDir) {
143        super.onCreate();
144        mStatementRetriever = statementRetriever;
145        mHandler = new Handler(looper);
146
147        try {
148            File httpCacheDir = new File(cacheDir, CACHE_FILENAME);
149            mHttpResponseCache = HttpResponseCache.install(httpCacheDir, HTTP_CACHE_SIZE_IN_BYTES);
150        } catch (IOException e) {
151            Log.i(TAG, "HTTPS response cache installation failed:" + e);
152        }
153    }
154
155    @Override
156    public void onDestroy() {
157        super.onDestroy();
158        if (mThread != null) {
159            mThread.quit();
160        }
161
162        try {
163            if (mHttpResponseCache != null) {
164                mHttpResponseCache.delete();
165            }
166        } catch (IOException e) {
167            Log.i(TAG, "HTTP(S) response cache deletion failed:" + e);
168        }
169    }
170
171    @Override
172    public IBinder onBind(Intent intent) {
173        return null;
174    }
175
176    @Override
177    public int onStartCommand(Intent intent, int flags, int startId) {
178        super.onStartCommand(intent, flags, startId);
179
180        if (intent == null) {
181            Log.e(TAG, "onStartCommand called with null intent");
182            return START_STICKY;
183        }
184
185        if (intent.getAction().equals(CHECK_ALL_ACTION)) {
186
187            Bundle extras = intent.getExtras();
188            List<String> sources = extras.getStringArrayList(EXTRA_SOURCE_ASSET_DESCRIPTORS);
189            String target = extras.getString(EXTRA_TARGET_ASSET_DESCRIPTOR);
190            String relation = extras.getString(EXTRA_RELATION);
191            ResultReceiver resultReceiver = extras.getParcelable(EXTRA_RESULT_RECEIVER);
192
193            if (resultReceiver == null) {
194                Log.e(TAG, " Intent does not have extra " + EXTRA_RESULT_RECEIVER);
195                return START_STICKY;
196            }
197            if (sources == null) {
198                Log.e(TAG, " Intent does not have extra " + EXTRA_SOURCE_ASSET_DESCRIPTORS);
199                resultReceiver.send(RESULT_FAIL, Bundle.EMPTY);
200                return START_STICKY;
201            }
202            if (target == null) {
203                Log.e(TAG, " Intent does not have extra " + EXTRA_TARGET_ASSET_DESCRIPTOR);
204                resultReceiver.send(RESULT_FAIL, Bundle.EMPTY);
205                return START_STICKY;
206            }
207            if (relation == null) {
208                Log.e(TAG, " Intent does not have extra " + EXTRA_RELATION);
209                resultReceiver.send(RESULT_FAIL, Bundle.EMPTY);
210                return START_STICKY;
211            }
212
213            mHandler.post(new ExceptionLoggingFutureTask<Void>(
214                    new IsAssociatedCallable(sources, target, relation, resultReceiver), TAG));
215        } else {
216            Log.e(TAG, "onStartCommand called with unsupported action: " + intent.getAction());
217        }
218        return START_STICKY;
219    }
220
221    private class IsAssociatedCallable implements Callable<Void> {
222
223        private List<String> mSources;
224        private String mTarget;
225        private String mRelation;
226        private ResultReceiver mResultReceiver;
227
228        public IsAssociatedCallable(List<String> sources, String target, String relation,
229                ResultReceiver resultReceiver) {
230            mSources = sources;
231            mTarget = target;
232            mRelation = relation;
233            mResultReceiver = resultReceiver;
234        }
235
236        private boolean verifyOneSource(AbstractAsset source, AbstractAssetMatcher target,
237                Relation relation) throws AssociationServiceException {
238            Result statements = mStatementRetriever.retrieveStatements(source);
239            for (Statement statement : statements.getStatements()) {
240                if (relation.matches(statement.getRelation())
241                        && target.matches(statement.getTarget())) {
242                    return true;
243                }
244            }
245            return false;
246        }
247
248        @Override
249        public Void call() {
250            Bundle result = new Bundle();
251            ArrayList<String> failedSources = new ArrayList<String>();
252            AbstractAssetMatcher target;
253            Relation relation;
254            try {
255                target = AbstractAssetMatcher.createMatcher(mTarget);
256                relation = Relation.create(mRelation);
257            } catch (AssociationServiceException | JSONException e) {
258                Log.e(TAG, "isAssociatedCallable failed with exception", e);
259                mResultReceiver.send(RESULT_FAIL, Bundle.EMPTY);
260                return null;
261            }
262
263            boolean allSourcesVerified = true;
264            for (String sourceString : mSources) {
265                AbstractAsset source;
266                try {
267                    source = AbstractAsset.create(sourceString);
268                } catch (AssociationServiceException e) {
269                    mResultReceiver.send(RESULT_FAIL, Bundle.EMPTY);
270                    return null;
271                }
272
273                try {
274                    if (!verifyOneSource(source, target, relation)) {
275                        failedSources.add(source.toJson());
276                        allSourcesVerified = false;
277                    }
278                } catch (AssociationServiceException e) {
279                    failedSources.add(source.toJson());
280                    allSourcesVerified = false;
281                }
282            }
283
284            result.putBoolean(IS_ASSOCIATED, allSourcesVerified);
285            result.putStringArrayList(FAILED_SOURCES, failedSources);
286            mResultReceiver.send(RESULT_SUCCESS, result);
287            return null;
288        }
289    }
290}
291