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 
17 package com.android.statementservice;
18 
19 import android.app.Service;
20 import android.content.Intent;
21 import android.net.http.HttpResponseCache;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.os.HandlerThread;
25 import android.os.IBinder;
26 import android.os.Looper;
27 import android.os.ResultReceiver;
28 import android.util.Log;
29 
30 import com.android.statementservice.retriever.AbstractAsset;
31 import com.android.statementservice.retriever.AbstractAssetMatcher;
32 import com.android.statementservice.retriever.AbstractStatementRetriever;
33 import com.android.statementservice.retriever.AbstractStatementRetriever.Result;
34 import com.android.statementservice.retriever.AssociationServiceException;
35 import com.android.statementservice.retriever.Relation;
36 import com.android.statementservice.retriever.Statement;
37 
38 import org.json.JSONException;
39 
40 import java.io.File;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.concurrent.Callable;
45 
46 /**
47  * Handles com.android.statementservice.service.CHECK_ALL_ACTION intents.
48  */
49 public 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
onCreate()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      */
onCreate(AbstractStatementRetriever statementRetriever, Looper looper, File cacheDir)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
onDestroy()156     public void onDestroy() {
157         super.onDestroy();
158         final HttpResponseCache responseCache = mHttpResponseCache;
159         mHandler.post(new Runnable() {
160             public void run() {
161                 try {
162                     if (responseCache != null) {
163                         responseCache.delete();
164                     }
165                 } catch (IOException e) {
166                     Log.i(TAG, "HTTP(S) response cache deletion failed:" + e);
167                 }
168                 Looper.myLooper().quit();
169             }
170         });
171         mHttpResponseCache = null;
172     }
173 
174     @Override
onBind(Intent intent)175     public IBinder onBind(Intent intent) {
176         return null;
177     }
178 
179     @Override
onStartCommand(Intent intent, int flags, int startId)180     public int onStartCommand(Intent intent, int flags, int startId) {
181         super.onStartCommand(intent, flags, startId);
182 
183         if (intent == null) {
184             Log.e(TAG, "onStartCommand called with null intent");
185             return START_STICKY;
186         }
187 
188         if (intent.getAction().equals(CHECK_ALL_ACTION)) {
189 
190             Bundle extras = intent.getExtras();
191             List<String> sources = extras.getStringArrayList(EXTRA_SOURCE_ASSET_DESCRIPTORS);
192             String target = extras.getString(EXTRA_TARGET_ASSET_DESCRIPTOR);
193             String relation = extras.getString(EXTRA_RELATION);
194             ResultReceiver resultReceiver = extras.getParcelable(EXTRA_RESULT_RECEIVER);
195 
196             if (resultReceiver == null) {
197                 Log.e(TAG, " Intent does not have extra " + EXTRA_RESULT_RECEIVER);
198                 return START_STICKY;
199             }
200             if (sources == null) {
201                 Log.e(TAG, " Intent does not have extra " + EXTRA_SOURCE_ASSET_DESCRIPTORS);
202                 resultReceiver.send(RESULT_FAIL, Bundle.EMPTY);
203                 return START_STICKY;
204             }
205             if (target == null) {
206                 Log.e(TAG, " Intent does not have extra " + EXTRA_TARGET_ASSET_DESCRIPTOR);
207                 resultReceiver.send(RESULT_FAIL, Bundle.EMPTY);
208                 return START_STICKY;
209             }
210             if (relation == null) {
211                 Log.e(TAG, " Intent does not have extra " + EXTRA_RELATION);
212                 resultReceiver.send(RESULT_FAIL, Bundle.EMPTY);
213                 return START_STICKY;
214             }
215 
216             mHandler.post(new ExceptionLoggingFutureTask<Void>(
217                     new IsAssociatedCallable(sources, target, relation, resultReceiver), TAG));
218         } else {
219             Log.e(TAG, "onStartCommand called with unsupported action: " + intent.getAction());
220         }
221         return START_STICKY;
222     }
223 
224     private class IsAssociatedCallable implements Callable<Void> {
225 
226         private List<String> mSources;
227         private String mTarget;
228         private String mRelation;
229         private ResultReceiver mResultReceiver;
230 
IsAssociatedCallable(List<String> sources, String target, String relation, ResultReceiver resultReceiver)231         public IsAssociatedCallable(List<String> sources, String target, String relation,
232                 ResultReceiver resultReceiver) {
233             mSources = sources;
234             mTarget = target;
235             mRelation = relation;
236             mResultReceiver = resultReceiver;
237         }
238 
verifyOneSource(AbstractAsset source, AbstractAssetMatcher target, Relation relation)239         private boolean verifyOneSource(AbstractAsset source, AbstractAssetMatcher target,
240                 Relation relation) throws AssociationServiceException {
241             Result statements = mStatementRetriever.retrieveStatements(source);
242             for (Statement statement : statements.getStatements()) {
243                 if (relation.matches(statement.getRelation())
244                         && target.matches(statement.getTarget())) {
245                     return true;
246                 }
247             }
248             return false;
249         }
250 
251         @Override
call()252         public Void call() {
253             Bundle result = new Bundle();
254             ArrayList<String> failedSources = new ArrayList<String>();
255             AbstractAssetMatcher target;
256             Relation relation;
257             try {
258                 target = AbstractAssetMatcher.createMatcher(mTarget);
259                 relation = Relation.create(mRelation);
260             } catch (AssociationServiceException | JSONException e) {
261                 Log.e(TAG, "isAssociatedCallable failed with exception", e);
262                 mResultReceiver.send(RESULT_FAIL, Bundle.EMPTY);
263                 return null;
264             }
265 
266             boolean allSourcesVerified = true;
267             for (String sourceString : mSources) {
268                 AbstractAsset source;
269                 try {
270                     source = AbstractAsset.create(sourceString);
271                 } catch (AssociationServiceException e) {
272                     mResultReceiver.send(RESULT_FAIL, Bundle.EMPTY);
273                     return null;
274                 }
275 
276                 try {
277                     if (!verifyOneSource(source, target, relation)) {
278                         failedSources.add(source.toJson());
279                         allSourcesVerified = false;
280                     }
281                 } catch (AssociationServiceException e) {
282                     failedSources.add(source.toJson());
283                     allSourcesVerified = false;
284                 }
285             }
286 
287             result.putBoolean(IS_ASSOCIATED, allSourcesVerified);
288             result.putStringArrayList(FAILED_SOURCES, failedSources);
289             mResultReceiver.send(RESULT_SUCCESS, result);
290             return null;
291         }
292     }
293 }
294