Home | History | Annotate | Download | only in webkit
      1 /*
      2  * Copyright (C) 2010 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 android.webkit;
     18 
     19 import android.os.Bundle;
     20 import android.net.http.SslError;
     21 
     22 import java.net.MalformedURLException;
     23 import java.net.URL;
     24 
     25 /**
     26  * Stores the user's decision of whether to allow or deny an invalid certificate.
     27  *
     28  * This class is not threadsafe. It is used only on the WebCore thread. Also, it
     29  * is used only by the Chromium HTTP stack.
     30  */
     31 final class SslCertLookupTable {
     32     private static SslCertLookupTable sTable;
     33     // We store the most severe error we're willing to allow for each host.
     34     private final Bundle table;
     35 
     36     public static SslCertLookupTable getInstance() {
     37         if (sTable == null) {
     38             sTable = new SslCertLookupTable();
     39         }
     40         return sTable;
     41     }
     42 
     43     private SslCertLookupTable() {
     44         table = new Bundle();
     45     }
     46 
     47     public void setIsAllowed(SslError sslError) {
     48         String host;
     49         try {
     50             host = new URL(sslError.getUrl()).getHost();
     51         } catch(MalformedURLException e) {
     52             return;
     53         }
     54         table.putInt(host, sslError.getPrimaryError());
     55     }
     56 
     57     // We allow the decision to be re-used if it's for the same host and is for
     58     // an error of equal or greater severity than this error.
     59     public boolean isAllowed(SslError sslError) {
     60         String host;
     61         try {
     62             host = new URL(sslError.getUrl()).getHost();
     63         } catch(MalformedURLException e) {
     64             return false;
     65         }
     66         return table.containsKey(host) && sslError.getPrimaryError() <= table.getInt(host);
     67     }
     68 
     69     public void clear() {
     70         table.clear();
     71     }
     72 }
     73