Home | History | Annotate | Download | only in datasource
      1 /*
      2  * Copyright (C) 2017 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 package com.example.android.autofill.service.datasource;
     17 
     18 import android.content.Context;
     19 import android.util.Log;
     20 
     21 import com.example.android.autofill.service.SecurityHelper;
     22 
     23 import static com.example.android.autofill.service.CommonUtil.TAG;
     24 
     25 /**
     26  * Singleton repository that caches the result of Digital Asset Links checks.
     27  */
     28 public class SharedPrefsDigitalAssetLinksRepository implements DigitalAssetLinksDataSource {
     29 
     30     private static SharedPrefsDigitalAssetLinksRepository sInstance;
     31 
     32     private SharedPrefsDigitalAssetLinksRepository() {
     33     }
     34 
     35     public static SharedPrefsDigitalAssetLinksRepository getInstance() {
     36         if (sInstance == null) {
     37             sInstance = new SharedPrefsDigitalAssetLinksRepository();
     38         }
     39         return sInstance;
     40     }
     41 
     42     @Override
     43     public boolean isValid(Context context, String webDomain, String packageName) {
     44         // TODO: implement caching. It could cache the whole domain -> (packagename, fingerprint),
     45         // but then either invalidate when the package change or when the DAL association times out
     46         // (the maxAge is part of the API response), or document that a real-life service
     47         // should do that.
     48 
     49         String fingerprint = null;
     50         try {
     51             fingerprint = SecurityHelper.getFingerprint(context, packageName);
     52         } catch (Exception e) {
     53             Log.w(TAG, "error getting fingerprint for " + packageName, e);
     54             return false;
     55         }
     56         return SecurityHelper.isValid(webDomain, packageName, fingerprint);
     57     }
     58 
     59     @Override
     60     public void clear(Context context) {
     61         // TODO: implement once if caches results or remove from the interface
     62     }
     63 }
     64