mobile/android/base/SiteIdentity.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 package org.mozilla.gecko;
     8 import org.json.JSONObject;
    10 import android.text.TextUtils;
    12 public class SiteIdentity {
    13     private SecurityMode mSecurityMode;
    14     private String mHost;
    15     private String mOwner;
    16     private String mSupplemental;
    17     private String mVerifier;
    18     private String mEncrypted;
    20     // The order of the items here correspond to image
    21     // levels in site_security_level.xml
    22     public enum SecurityMode {
    23         UNKNOWN("unknown"),
    24         IDENTIFIED("identified"),
    25         VERIFIED("verified"),
    26         MIXED_CONTENT_BLOCKED("mixed_content_blocked"),
    27         MIXED_CONTENT_LOADED("mixed_content_loaded");
    29         private final String mId;
    31         private SecurityMode(String id) {
    32             mId = id;
    33         }
    35         public static SecurityMode fromString(String id) {
    36             if (id == null) {
    37                 throw new IllegalArgumentException("Can't convert null String to SiteIdentity");
    38             }
    40             for (SecurityMode mode : SecurityMode.values()) {
    41                 if (TextUtils.equals(mode.mId, id.toLowerCase())) {
    42                     return mode;
    43                 }
    44             }
    46             throw new IllegalArgumentException("Could not convert String id to SiteIdentity");
    47         }
    49         @Override
    50         public String toString() {
    51             return mId;
    52         }
    53     }
    55     public SiteIdentity() {
    56         reset(SecurityMode.UNKNOWN);
    57     }
    59     private void reset(SecurityMode securityMode) {
    60         mSecurityMode = securityMode;
    61         mHost = null;
    62         mOwner = null;
    63         mSupplemental = null;
    64         mVerifier = null;
    65         mEncrypted = null;
    66     }
    68     void update(JSONObject identityData) {
    69         try {
    70             mSecurityMode = SecurityMode.fromString(identityData.getString("mode"));
    71         } catch (Exception e) {
    72             reset(SecurityMode.UNKNOWN);
    73             return;
    74         }
    76         try {
    77             mHost = identityData.getString("host");
    78             mOwner = identityData.getString("owner");
    79             mSupplemental = identityData.optString("supplemental", null);
    80             mVerifier = identityData.getString("verifier");
    81             mEncrypted = identityData.getString("encrypted");
    82         } catch (Exception e) {
    83             reset(mSecurityMode);
    84         }
    85     }
    87     public SecurityMode getSecurityMode() {
    88         return mSecurityMode;
    89     }
    91     public String getHost() {
    92         return mHost;
    93     }
    95     public String getOwner() {
    96         return mOwner;
    97     }
    99     public String getSupplemental() {
   100         return mSupplemental;
   101     }
   103     public String getVerifier() {
   104         return mVerifier;
   105     }
   107     public String getEncrypted() {
   108         return mEncrypted;
   109     }
   110 }

mercurial