michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import org.json.JSONObject; michael@0: michael@0: import android.text.TextUtils; michael@0: michael@0: public class SiteIdentity { michael@0: private SecurityMode mSecurityMode; michael@0: private String mHost; michael@0: private String mOwner; michael@0: private String mSupplemental; michael@0: private String mVerifier; michael@0: private String mEncrypted; michael@0: michael@0: // The order of the items here correspond to image michael@0: // levels in site_security_level.xml michael@0: public enum SecurityMode { michael@0: UNKNOWN("unknown"), michael@0: IDENTIFIED("identified"), michael@0: VERIFIED("verified"), michael@0: MIXED_CONTENT_BLOCKED("mixed_content_blocked"), michael@0: MIXED_CONTENT_LOADED("mixed_content_loaded"); michael@0: michael@0: private final String mId; michael@0: michael@0: private SecurityMode(String id) { michael@0: mId = id; michael@0: } michael@0: michael@0: public static SecurityMode fromString(String id) { michael@0: if (id == null) { michael@0: throw new IllegalArgumentException("Can't convert null String to SiteIdentity"); michael@0: } michael@0: michael@0: for (SecurityMode mode : SecurityMode.values()) { michael@0: if (TextUtils.equals(mode.mId, id.toLowerCase())) { michael@0: return mode; michael@0: } michael@0: } michael@0: michael@0: throw new IllegalArgumentException("Could not convert String id to SiteIdentity"); michael@0: } michael@0: michael@0: @Override michael@0: public String toString() { michael@0: return mId; michael@0: } michael@0: } michael@0: michael@0: public SiteIdentity() { michael@0: reset(SecurityMode.UNKNOWN); michael@0: } michael@0: michael@0: private void reset(SecurityMode securityMode) { michael@0: mSecurityMode = securityMode; michael@0: mHost = null; michael@0: mOwner = null; michael@0: mSupplemental = null; michael@0: mVerifier = null; michael@0: mEncrypted = null; michael@0: } michael@0: michael@0: void update(JSONObject identityData) { michael@0: try { michael@0: mSecurityMode = SecurityMode.fromString(identityData.getString("mode")); michael@0: } catch (Exception e) { michael@0: reset(SecurityMode.UNKNOWN); michael@0: return; michael@0: } michael@0: michael@0: try { michael@0: mHost = identityData.getString("host"); michael@0: mOwner = identityData.getString("owner"); michael@0: mSupplemental = identityData.optString("supplemental", null); michael@0: mVerifier = identityData.getString("verifier"); michael@0: mEncrypted = identityData.getString("encrypted"); michael@0: } catch (Exception e) { michael@0: reset(mSecurityMode); michael@0: } michael@0: } michael@0: michael@0: public SecurityMode getSecurityMode() { michael@0: return mSecurityMode; michael@0: } michael@0: michael@0: public String getHost() { michael@0: return mHost; michael@0: } michael@0: michael@0: public String getOwner() { michael@0: return mOwner; michael@0: } michael@0: michael@0: public String getSupplemental() { michael@0: return mSupplemental; michael@0: } michael@0: michael@0: public String getVerifier() { michael@0: return mVerifier; michael@0: } michael@0: michael@0: public String getEncrypted() { michael@0: return mEncrypted; michael@0: } michael@0: }