From 590ddb36115ce5c5f298a6b15795cc500fd5904e Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Mon, 24 Oct 2011 13:35:25 +0100
Subject: [PATCH] Adding an offline error message

---
 .../dialog/controllers/dialog_controller.js   | 34 ++++++++++++++++---
 .../static/dialog/resources/error-messages.js |  6 ++++
 2 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/browserid/static/dialog/controllers/dialog_controller.js b/browserid/static/dialog/controllers/dialog_controller.js
index 9f0507b27..676de0249 100644
--- a/browserid/static/dialog/controllers/dialog_controller.js
+++ b/browserid/static/dialog/controllers/dialog_controller.js
@@ -44,7 +44,8 @@
 
   var bid = BrowserID,
       user = bid.User,
-      errors = bid.Errors;
+      errors = bid.Errors,
+      offline = false;
 
   PageController.extend("Dialog", {}, {
       init: function(el) {
@@ -59,17 +60,23 @@
       },
         
       getVerifiedEmail: function(origin_url, onsuccess, onerror) {
-        this.onsuccess = onsuccess;
-        this.onerror = onerror;
+        var self=this;
+
+        self.onsuccess = onsuccess;
+        self.onerror = onerror;
+
+        if('onLine' in navigator && !navigator.onLine) {
+          self.doOffline();
+          return;
+        }
 
         user.setOrigin(origin_url);
         
         // get the cleaned origin.
         $("#sitename").text(user.getHostname());
 
-        this.doCheckAuth();
+        self.doCheckAuth();
 
-        var self=this;
         $(window).bind("unload", function() {
           self.doCancel();
         });
@@ -82,6 +89,14 @@
             el = this.element;
        
 
+        hub.subscribe("offline", function(msg, info) {
+          self.doOffline();
+        });
+
+        hub.subscribe("xhrError", function(msg, info) {
+          self.doXHRError(info);
+        });
+
         hub.subscribe("user_staged", function(msg, info) {
           self.doConfirmUser(info.email);
         });
@@ -140,6 +155,15 @@
 
       },
 
+      doOffline: function() {
+        this.renderError(errors.offline);
+        offline = true;
+      },
+
+      doXHRError: function(info) {
+        if (!offline) this.renderError(errors.offline);  
+      },
+
       doConfirmUser: function(email) {
         this.confirmEmail = email;
 
diff --git a/browserid/static/dialog/resources/error-messages.js b/browserid/static/dialog/resources/error-messages.js
index 860825d58..7064d82d9 100644
--- a/browserid/static/dialog/resources/error-messages.js
+++ b/browserid/static/dialog/resources/error-messages.js
@@ -78,6 +78,12 @@ BrowserID.Errors = (function(){
       message: "An error was encountered while signing you out.  Yucky!"
     },
 
+    offline: {
+      type: "networkError",
+      title: "You are offline!",
+      message: "Unfortunately, BrowserID cannot communicate while offline!"
+    },
+
     registration: {
       type: "serverError",
       title: "Registration Failed",
-- 
GitLab