|
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/. */ |
|
5 |
|
6 package org.mozilla.gecko; |
|
7 |
|
8 import org.json.JSONObject; |
|
9 |
|
10 import android.text.TextUtils; |
|
11 |
|
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; |
|
19 |
|
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"); |
|
28 |
|
29 private final String mId; |
|
30 |
|
31 private SecurityMode(String id) { |
|
32 mId = id; |
|
33 } |
|
34 |
|
35 public static SecurityMode fromString(String id) { |
|
36 if (id == null) { |
|
37 throw new IllegalArgumentException("Can't convert null String to SiteIdentity"); |
|
38 } |
|
39 |
|
40 for (SecurityMode mode : SecurityMode.values()) { |
|
41 if (TextUtils.equals(mode.mId, id.toLowerCase())) { |
|
42 return mode; |
|
43 } |
|
44 } |
|
45 |
|
46 throw new IllegalArgumentException("Could not convert String id to SiteIdentity"); |
|
47 } |
|
48 |
|
49 @Override |
|
50 public String toString() { |
|
51 return mId; |
|
52 } |
|
53 } |
|
54 |
|
55 public SiteIdentity() { |
|
56 reset(SecurityMode.UNKNOWN); |
|
57 } |
|
58 |
|
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 } |
|
67 |
|
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 } |
|
75 |
|
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 } |
|
86 |
|
87 public SecurityMode getSecurityMode() { |
|
88 return mSecurityMode; |
|
89 } |
|
90 |
|
91 public String getHost() { |
|
92 return mHost; |
|
93 } |
|
94 |
|
95 public String getOwner() { |
|
96 return mOwner; |
|
97 } |
|
98 |
|
99 public String getSupplemental() { |
|
100 return mSupplemental; |
|
101 } |
|
102 |
|
103 public String getVerifier() { |
|
104 return mVerifier; |
|
105 } |
|
106 |
|
107 public String getEncrypted() { |
|
108 return mEncrypted; |
|
109 } |
|
110 } |