Bug 1126608 - Add Doorhanger:Logins message from Gecko. r=ally
authorChenxia Liu <liuche@mozilla.com>
Tue, 05 May 2015 13:44:32 -0700
changeset 243968 2a0c51e9039dfa61c747f086d3935417c5209bd6
parent 243967 2782e88a35625206ba8138f61d96e412b1692f36
child 243969 09b6e5f3e10d572c9c2b5950cfabfcc5bf20548b
push id28761
push usercbook@mozilla.com
push dateFri, 15 May 2015 14:50:10 +0000
treeherdermozilla-central@c0e709a5baca [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersally
bugs1126608
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1126608 - Add Doorhanger:Logins message from Gecko. r=ally
mobile/android/base/toolbar/BrowserToolbar.java
mobile/android/base/toolbar/SiteIdentityPopup.java
mobile/android/base/toolbar/ToolbarDisplayLayout.java
mobile/android/chrome/content/browser.js
--- a/mobile/android/base/toolbar/BrowserToolbar.java
+++ b/mobile/android/base/toolbar/BrowserToolbar.java
@@ -849,16 +849,17 @@ public abstract class BrowserToolbar ext
     }
 
     public View getDoorHangerAnchor() {
         return urlDisplayLayout;
     }
 
     public void onDestroy() {
         Tabs.unregisterOnTabsChangedListener(this);
+        urlDisplayLayout.destroy();
     }
 
     public boolean openOptionsMenu() {
         if (!hasSoftMenuButton) {
             return false;
         }
 
         // Initialize the popup.
--- a/mobile/android/base/toolbar/SiteIdentityPopup.java
+++ b/mobile/android/base/toolbar/SiteIdentityPopup.java
@@ -1,24 +1,28 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.toolbar;
 
+import org.json.JSONException;
+import org.json.JSONArray;
 import org.mozilla.gecko.AboutPages;
+import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoEvent;
 import org.mozilla.gecko.SiteIdentity;
 import org.mozilla.gecko.SiteIdentity.SecurityMode;
 import org.mozilla.gecko.SiteIdentity.MixedMode;
 import org.mozilla.gecko.SiteIdentity.TrackingMode;
 import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.Tabs;
+import org.mozilla.gecko.util.GeckoEventListener;
 import org.mozilla.gecko.widget.AnchoredPopup;
 import org.mozilla.gecko.widget.DoorHanger;
 import org.mozilla.gecko.widget.DoorHanger.OnButtonClickListener;
 import org.json.JSONObject;
 
 import android.content.Context;
 import android.text.TextUtils;
 import android.util.Log;
@@ -27,17 +31,18 @@ import android.view.View;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 import org.mozilla.gecko.widget.DoorhangerConfig;
 
 /**
  * SiteIdentityPopup is a singleton class that displays site identity data in
  * an arrow panel popup hanging from the lock icon in the browser toolbar.
  */
-public class SiteIdentityPopup extends AnchoredPopup {
+public class SiteIdentityPopup extends AnchoredPopup implements GeckoEventListener {
+
     public static enum ButtonType { DISABLE, ENABLE, KEEP_BLOCKING };
 
     private static final String LOGTAG = "GeckoSiteIdentityPopup";
 
     private static final String MIXED_CONTENT_SUPPORT_URL =
         "https://support.mozilla.org/kb/how-does-insecure-content-affect-safety-android";
 
     private static final String TRACKING_CONTENT_SUPPORT_URL =
@@ -61,16 +66,17 @@ public class SiteIdentityPopup extends A
     private DoorHanger mTrackingContentNotification;
 
     private final OnButtonClickListener mButtonClickListener;
 
     public SiteIdentityPopup(Context context) {
         super(context);
 
         mButtonClickListener = new PopupButtonListener();
+        EventDispatcher.getInstance().registerGeckoThreadListener(this, "Doorhanger:Logins");
     }
 
     @Override
     protected void init() {
         super.init();
 
         // Make the popup focusable so it doesn't inadvertently trigger click events elsewhere
         // which may reshow the popup (see bug 785156)
@@ -109,16 +115,32 @@ public class SiteIdentityPopup extends A
         final boolean isIdentityKnown = (siteIdentity.getSecurityMode() != SecurityMode.UNKNOWN);
         toggleIdentityKnownContainerVisibility(isIdentityKnown);
 
         if (isIdentityKnown) {
             updateIdentityInformation(siteIdentity);
         }
     }
 
+    @Override
+    public void handleMessage(String event, JSONObject geckoObject) {
+        if ("Doorhanger:Logins".equals(event)) {
+            try {
+                final JSONArray logins = geckoObject.getJSONArray("logins");
+                addSelectLoginDoorhanger(logins);
+            } catch (JSONException e) {
+                Log.e(LOGTAG, "Error accessing logins in Doorhanger:Logins message", e);
+            }
+        }
+    }
+
+    private void addSelectLoginDoorhanger(JSONArray logins) {
+        // TODO: add doorhanger + link (if there is more than one login).
+    }
+
     private void toggleIdentityKnownContainerVisibility(final boolean isIdentityKnown) {
         if (isIdentityKnown) {
             mIdentityKnownContainer.setVisibility(View.VISIBLE);
             mIdentityUnknownContainer.setVisibility(View.GONE);
         } else {
             mIdentityKnownContainer.setVisibility(View.GONE);
             mIdentityUnknownContainer.setVisibility(View.VISIBLE);
         }
@@ -283,16 +305,20 @@ public class SiteIdentityPopup extends A
             }
         }
 
         if (lastVisibleDoorHanger != null) {
             lastVisibleDoorHanger.hideDivider();
         }
     }
 
+    void destroy() {
+        EventDispatcher.getInstance().unregisterGeckoThreadListener(this, "Doorhanger:Logins");
+    }
+
     @Override
     public void dismiss() {
         super.dismiss();
         removeMixedContentNotification();
         removeTrackingContentNotification();
         mDivider.setVisibility(View.GONE);
     }
 
--- a/mobile/android/base/toolbar/ToolbarDisplayLayout.java
+++ b/mobile/android/base/toolbar/ToolbarDisplayLayout.java
@@ -611,9 +611,13 @@ public class ToolbarDisplayLayout extend
     boolean dismissSiteIdentityPopup() {
         if (mSiteIdentityPopup != null && mSiteIdentityPopup.isShowing()) {
             mSiteIdentityPopup.dismiss();
             return true;
         }
 
         return false;
     }
+
+    void destroy() {
+        mSiteIdentityPopup.destroy();
+    }
 }
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -4213,23 +4213,32 @@ Tab.prototype = {
 
           this.browser.addEventListener("pagehide", listener, true);
         }
 
         if (docURI.startsWith("about:reader")) {
           // Update the page action to show the "reader active" icon.
           Reader.updatePageAction(this);
         }
-
         break;
       }
 
       case "DOMFormHasPassword": {
         LoginManagerContent.onDOMFormHasPassword(aEvent,
                                                  this.browser.contentWindow);
+
+        // Send logins for this hostname to Java.
+        let hostname = aEvent.target.baseURIObject.prePath;
+        let foundLogins = Services.logins.findLogins({}, hostname, "", "");
+        if (foundLogins.length > 0) {
+          let displayHost = IdentityHandler.getEffectiveHost();
+          let title = { text: displayHost, resource: hostname };
+          let selectObj = { title: title, logins: foundLogins };
+          Messaging.sendRequest({ type: "Doorhanger:Logins", data: selectObj });
+        }
         break;
       }
 
       case "DOMMetaAdded":
         let target = aEvent.originalTarget;
         let browser = BrowserApp.getBrowserForDocument(target.ownerDocument);
 
         switch (target.name) {