mobile/android/base/SiteIdentity.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/SiteIdentity.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko;
    1.10 +
    1.11 +import org.json.JSONObject;
    1.12 +
    1.13 +import android.text.TextUtils;
    1.14 +
    1.15 +public class SiteIdentity {
    1.16 +    private SecurityMode mSecurityMode;
    1.17 +    private String mHost;
    1.18 +    private String mOwner;
    1.19 +    private String mSupplemental;
    1.20 +    private String mVerifier;
    1.21 +    private String mEncrypted;
    1.22 +
    1.23 +    // The order of the items here correspond to image
    1.24 +    // levels in site_security_level.xml
    1.25 +    public enum SecurityMode {
    1.26 +        UNKNOWN("unknown"),
    1.27 +        IDENTIFIED("identified"),
    1.28 +        VERIFIED("verified"),
    1.29 +        MIXED_CONTENT_BLOCKED("mixed_content_blocked"),
    1.30 +        MIXED_CONTENT_LOADED("mixed_content_loaded");
    1.31 +
    1.32 +        private final String mId;
    1.33 +
    1.34 +        private SecurityMode(String id) {
    1.35 +            mId = id;
    1.36 +        }
    1.37 +
    1.38 +        public static SecurityMode fromString(String id) {
    1.39 +            if (id == null) {
    1.40 +                throw new IllegalArgumentException("Can't convert null String to SiteIdentity");
    1.41 +            }
    1.42 +
    1.43 +            for (SecurityMode mode : SecurityMode.values()) {
    1.44 +                if (TextUtils.equals(mode.mId, id.toLowerCase())) {
    1.45 +                    return mode;
    1.46 +                }
    1.47 +            }
    1.48 +
    1.49 +            throw new IllegalArgumentException("Could not convert String id to SiteIdentity");
    1.50 +        }
    1.51 +
    1.52 +        @Override
    1.53 +        public String toString() {
    1.54 +            return mId;
    1.55 +        }
    1.56 +    }
    1.57 +
    1.58 +    public SiteIdentity() {
    1.59 +        reset(SecurityMode.UNKNOWN);
    1.60 +    }
    1.61 +
    1.62 +    private void reset(SecurityMode securityMode) {
    1.63 +        mSecurityMode = securityMode;
    1.64 +        mHost = null;
    1.65 +        mOwner = null;
    1.66 +        mSupplemental = null;
    1.67 +        mVerifier = null;
    1.68 +        mEncrypted = null;
    1.69 +    }
    1.70 +
    1.71 +    void update(JSONObject identityData) {
    1.72 +        try {
    1.73 +            mSecurityMode = SecurityMode.fromString(identityData.getString("mode"));
    1.74 +        } catch (Exception e) {
    1.75 +            reset(SecurityMode.UNKNOWN);
    1.76 +            return;
    1.77 +        }
    1.78 +
    1.79 +        try {
    1.80 +            mHost = identityData.getString("host");
    1.81 +            mOwner = identityData.getString("owner");
    1.82 +            mSupplemental = identityData.optString("supplemental", null);
    1.83 +            mVerifier = identityData.getString("verifier");
    1.84 +            mEncrypted = identityData.getString("encrypted");
    1.85 +        } catch (Exception e) {
    1.86 +            reset(mSecurityMode);
    1.87 +        }
    1.88 +    }
    1.89 +
    1.90 +    public SecurityMode getSecurityMode() {
    1.91 +        return mSecurityMode;
    1.92 +    }
    1.93 +
    1.94 +    public String getHost() {
    1.95 +        return mHost;
    1.96 +    }
    1.97 +
    1.98 +    public String getOwner() {
    1.99 +        return mOwner;
   1.100 +    }
   1.101 +
   1.102 +    public String getSupplemental() {
   1.103 +        return mSupplemental;
   1.104 +    }
   1.105 +
   1.106 +    public String getVerifier() {
   1.107 +        return mVerifier;
   1.108 +    }
   1.109 +
   1.110 +    public String getEncrypted() {
   1.111 +        return mEncrypted;
   1.112 +    }
   1.113 +}
   1.114 \ No newline at end of file

mercurial