mobile/android/base/toolbar/SiteIdentityPopup.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.toolbar;
michael@0 6
michael@0 7 import org.mozilla.gecko.BrowserApp;
michael@0 8 import org.mozilla.gecko.R;
michael@0 9 import org.mozilla.gecko.GeckoAppShell;
michael@0 10 import org.mozilla.gecko.GeckoEvent;
michael@0 11 import org.mozilla.gecko.SiteIdentity;
michael@0 12 import org.mozilla.gecko.SiteIdentity.SecurityMode;
michael@0 13 import org.mozilla.gecko.widget.ArrowPopup;
michael@0 14 import org.mozilla.gecko.widget.DoorHanger;
michael@0 15 import org.mozilla.gecko.widget.DoorHanger.OnButtonClickListener;
michael@0 16
michael@0 17 import org.json.JSONException;
michael@0 18 import org.json.JSONObject;
michael@0 19
michael@0 20 import android.content.res.Resources;
michael@0 21 import android.text.TextUtils;
michael@0 22 import android.util.Log;
michael@0 23 import android.view.LayoutInflater;
michael@0 24 import android.view.View;
michael@0 25 import android.widget.ImageView;
michael@0 26 import android.widget.LinearLayout;
michael@0 27 import android.widget.TextView;
michael@0 28
michael@0 29 /**
michael@0 30 * SiteIdentityPopup is a singleton class that displays site identity data in
michael@0 31 * an arrow panel popup hanging from the lock icon in the browser toolbar.
michael@0 32 */
michael@0 33 public class SiteIdentityPopup extends ArrowPopup {
michael@0 34 private static final String LOGTAG = "GeckoSiteIdentityPopup";
michael@0 35
michael@0 36 // FIXME: Update this URL for mobile. See bug 885923.
michael@0 37 private static final String MIXED_CONTENT_SUPPORT_URL =
michael@0 38 "https://support.mozilla.org/kb/how-does-content-isnt-secure-affect-my-safety";
michael@0 39
michael@0 40 private SiteIdentity mSiteIdentity;
michael@0 41
michael@0 42 private Resources mResources;
michael@0 43
michael@0 44 private LinearLayout mIdentity;
michael@0 45 private TextView mHost;
michael@0 46 private TextView mOwner;
michael@0 47 private TextView mVerifier;
michael@0 48
michael@0 49 private DoorHanger mMixedContentNotification;
michael@0 50
michael@0 51 private final OnButtonClickListener mButtonClickListener;
michael@0 52
michael@0 53 SiteIdentityPopup(BrowserApp activity) {
michael@0 54 super(activity);
michael@0 55
michael@0 56 mResources = activity.getResources();
michael@0 57 mButtonClickListener = new PopupButtonListener();
michael@0 58 }
michael@0 59
michael@0 60 @Override
michael@0 61 protected void init() {
michael@0 62 super.init();
michael@0 63
michael@0 64 // Make the popup focusable so it doesn't inadvertently trigger click events elsewhere
michael@0 65 // which may reshow the popup (see bug 785156)
michael@0 66 setFocusable(true);
michael@0 67
michael@0 68 LayoutInflater inflater = LayoutInflater.from(mActivity);
michael@0 69 mIdentity = (LinearLayout) inflater.inflate(R.layout.site_identity, null);
michael@0 70 mContent.addView(mIdentity);
michael@0 71
michael@0 72 mHost = (TextView) mIdentity.findViewById(R.id.host);
michael@0 73 mOwner = (TextView) mIdentity.findViewById(R.id.owner);
michael@0 74 mVerifier = (TextView) mIdentity.findViewById(R.id.verifier);
michael@0 75 }
michael@0 76
michael@0 77 private void updateUi() {
michael@0 78 if (!mInflated) {
michael@0 79 init();
michael@0 80 }
michael@0 81
michael@0 82 if (mSiteIdentity.getSecurityMode() == SecurityMode.MIXED_CONTENT_LOADED ||
michael@0 83 mSiteIdentity.getSecurityMode() == SecurityMode.MIXED_CONTENT_BLOCKED) {
michael@0 84 // Hide the identity data if there isn't valid site identity data.
michael@0 85 // Set some top padding on the popup content to create a of light blue
michael@0 86 // between the popup arrow and the mixed content notification.
michael@0 87 mContent.setPadding(0, (int) mResources.getDimension(R.dimen.identity_padding_top), 0, 0);
michael@0 88 mIdentity.setVisibility(View.GONE);
michael@0 89 } else {
michael@0 90 mHost.setText(mSiteIdentity.getHost());
michael@0 91
michael@0 92 String owner = mSiteIdentity.getOwner();
michael@0 93
michael@0 94 // Supplemental data is optional.
michael@0 95 final String supplemental = mSiteIdentity.getSupplemental();
michael@0 96 if (!TextUtils.isEmpty(supplemental)) {
michael@0 97 owner += "\n" + supplemental;
michael@0 98 }
michael@0 99 mOwner.setText(owner);
michael@0 100
michael@0 101 final String verifier = mSiteIdentity.getVerifier();
michael@0 102 final String encrypted = mSiteIdentity.getEncrypted();
michael@0 103 mVerifier.setText(verifier + "\n" + encrypted);
michael@0 104
michael@0 105 mContent.setPadding(0, 0, 0, 0);
michael@0 106 mIdentity.setVisibility(View.VISIBLE);
michael@0 107 }
michael@0 108 }
michael@0 109
michael@0 110 private void addMixedContentNotification(boolean blocked) {
michael@0 111 // Remove any exixting mixed content notification.
michael@0 112 removeMixedContentNotification();
michael@0 113 mMixedContentNotification = new DoorHanger(mActivity, DoorHanger.Theme.DARK);
michael@0 114
michael@0 115 String message;
michael@0 116 if (blocked) {
michael@0 117 message = mActivity.getString(R.string.blocked_mixed_content_message_top) + "\n\n" +
michael@0 118 mActivity.getString(R.string.blocked_mixed_content_message_bottom);
michael@0 119 } else {
michael@0 120 message = mActivity.getString(R.string.loaded_mixed_content_message);
michael@0 121 }
michael@0 122 mMixedContentNotification.setMessage(message);
michael@0 123 mMixedContentNotification.addLink(mActivity.getString(R.string.learn_more), MIXED_CONTENT_SUPPORT_URL, "\n\n");
michael@0 124
michael@0 125 if (blocked) {
michael@0 126 mMixedContentNotification.setIcon(R.drawable.shield_doorhanger);
michael@0 127 mMixedContentNotification.addButton(mActivity.getString(R.string.disable_protection),
michael@0 128 "disable", mButtonClickListener);
michael@0 129 mMixedContentNotification.addButton(mActivity.getString(R.string.keep_blocking),
michael@0 130 "keepBlocking", mButtonClickListener);
michael@0 131 } else {
michael@0 132 mMixedContentNotification.setIcon(R.drawable.warning_doorhanger);
michael@0 133 mMixedContentNotification.addButton(mActivity.getString(R.string.enable_protection),
michael@0 134 "enable", mButtonClickListener);
michael@0 135 }
michael@0 136
michael@0 137 mContent.addView(mMixedContentNotification);
michael@0 138 }
michael@0 139
michael@0 140 private void removeMixedContentNotification() {
michael@0 141 if (mMixedContentNotification != null) {
michael@0 142 mContent.removeView(mMixedContentNotification);
michael@0 143 mMixedContentNotification = null;
michael@0 144 }
michael@0 145 }
michael@0 146
michael@0 147 /*
michael@0 148 * @param identityData A JSONObject that holds the current tab's identity data.
michael@0 149 */
michael@0 150 void setSiteIdentity(SiteIdentity siteIdentity) {
michael@0 151 mSiteIdentity = siteIdentity;
michael@0 152 }
michael@0 153
michael@0 154 @Override
michael@0 155 public void show() {
michael@0 156 if (mSiteIdentity == null) {
michael@0 157 Log.e(LOGTAG, "Can't show site identity popup for undefined state");
michael@0 158 return;
michael@0 159 }
michael@0 160
michael@0 161 final SecurityMode mode = mSiteIdentity.getSecurityMode();
michael@0 162 if (mode == SecurityMode.UNKNOWN) {
michael@0 163 Log.e(LOGTAG, "Can't show site identity popup in non-identified state");
michael@0 164 return;
michael@0 165 }
michael@0 166
michael@0 167 updateUi();
michael@0 168
michael@0 169 if (mode == SecurityMode.MIXED_CONTENT_LOADED ||
michael@0 170 mode == SecurityMode.MIXED_CONTENT_BLOCKED) {
michael@0 171 addMixedContentNotification(mode == SecurityMode.MIXED_CONTENT_BLOCKED);
michael@0 172 }
michael@0 173
michael@0 174 super.show();
michael@0 175 }
michael@0 176
michael@0 177 @Override
michael@0 178 public void dismiss() {
michael@0 179 super.dismiss();
michael@0 180 removeMixedContentNotification();
michael@0 181 }
michael@0 182
michael@0 183 private class PopupButtonListener implements OnButtonClickListener {
michael@0 184 @Override
michael@0 185 public void onButtonClick(DoorHanger dh, String tag) {
michael@0 186 try {
michael@0 187 JSONObject data = new JSONObject();
michael@0 188 data.put("allowMixedContent", tag.equals("disable"));
michael@0 189 GeckoEvent e = GeckoEvent.createBroadcastEvent("Session:Reload", data.toString());
michael@0 190 GeckoAppShell.sendEventToGecko(e);
michael@0 191 } catch (JSONException e) {
michael@0 192 Log.e(LOGTAG, "Exception creating message to enable/disable mixed content blocking", e);
michael@0 193 }
michael@0 194
michael@0 195 dismiss();
michael@0 196 }
michael@0 197 }
michael@0 198 }

mercurial