[chrome-gnome-shell/feature/network-errors] Show information about network errors via optional webRequest permission.



commit c66308fa9ddb595804c943235eb1f16213c43db5
Author: Yuri Konotopov <ykonotopov gmail com>
Date:   Tue Apr 19 20:17:44 2016 +0300

    Show information about network errors via optional webRequest permission.
    
    Issue: https://github.com/nE0sIghT/chrome-gnome-shell-mirror/issues/2

 extension/_locales/en/messages.json |    6 ++
 extension/_locales/ru/messages.json |    6 ++
 extension/css/options.css           |    4 +-
 extension/include/constants.js      |    2 +
 extension/include/update.js         |   93 +++++++++++++++++++++++------------
 extension/manifest.json             |    1 +
 extension/options.html              |   10 ++++
 extension/options.js                |   37 ++++++++++++++
 8 files changed, 126 insertions(+), 33 deletions(-)
---
diff --git a/extension/_locales/en/messages.json b/extension/_locales/en/messages.json
index d173bc1..1d521a9 100644
--- a/extension/_locales/en/messages.json
+++ b/extension/_locales/en/messages.json
@@ -62,6 +62,12 @@
        "options_check_period": {
                "message": "Update check period"
        },
+       "options_show_network_errors": {
+               "message": "Show information about network errors"
+       },
+       "options_show_network_errors_notice": {
+               "message": "Chrome permission «webRequest» required."
+       },
        "options_show_release_notes": {
                "message": "Show release notes when extension updated"
        },
diff --git a/extension/_locales/ru/messages.json b/extension/_locales/ru/messages.json
index fda27cb..496d5d9 100644
--- a/extension/_locales/ru/messages.json
+++ b/extension/_locales/ru/messages.json
@@ -62,6 +62,12 @@
        "options_check_period": {
                "message": "Период проверки обновлений"
        },
+       "options_show_network_errors": {
+               "message": "Показывать информацию о сетевых ошибках"
+       },
+       "options_show_network_errors_notice": {
+               "message": "Требуется разрешение Chrome «webRequest»."
+       },
        "options_show_release_notes": {
                "message": "Показывать примечания к выпуску при обновлении дополнения"
        },
diff --git a/extension/css/options.css b/extension/css/options.css
index e2250e2..6663fb2 100644
--- a/extension/css/options.css
+++ b/extension/css/options.css
@@ -15,7 +15,8 @@ body {
 }
 
 .buttons { text-align: center; margin-top: 1em; }
-.notice { font-size: 90%; margin-top: 0.5em; white-space: nowrap; }
+.notice { font-size: 90%; font-weight: normal; margin-top: 0.5em; white-space: nowrap; }
+.wrapped { white-space: normal; }
 
 fieldset {
        border: 0;
@@ -26,6 +27,7 @@ fieldset {
 
 dl {
        padding: 0.2em 0;
+       clear: both;
 }
 
 dt {
diff --git a/extension/include/constants.js b/extension/include/constants.js
index 6fdf557..c61e328 100644
--- a/extension/include/constants.js
+++ b/extension/include/constants.js
@@ -19,6 +19,8 @@ MESSAGE_NEXT_UPDATE_CHANGED           = 'gs-next-update-changed';
 
 NATIVE_HOST                            = 'io.github.ne0sight.gs_chrome_connector';
 
+UPDATE_URL                             = 'https://extensions.gnome.org/update-info/';
+
 DEFAULT_OPTIONS                                = {
        showReleaseNotes:       true,
        updateCheck:            true,
diff --git a/extension/include/update.js b/extension/include/update.js
index deea3a2..d7f3409 100644
--- a/extension/include/update.js
+++ b/extension/include/update.js
@@ -50,46 +50,71 @@ GSC.update = (function($) {
 
                                                request.installed = JSON.stringify(request.installed);
 
-                                               $.ajax({
-                                                       url: 'https://extensions.gnome.org/update-info/',
-                                                       data: request,
-                                                       method: 'GET',
-                                                       cache: false
-                                               }).done(function (data, textStatus, jqXHR) {
-                                                       
GSC.notifications.remove(NOTIFICATION_UPDATE_CHECK_FAILED);
-
-                                                       var toUpgrade = [];
-                                                       for (uuid in data)
+                                               chrome.permissions.contains({
+                                                       permissions: ["webRequest"]
+                                               }, function(webRequestEnabled) {
+                                                       if(webRequestEnabled)
                                                        {
-                                                               if (extensionsResponse.extensions[uuid] && 
$.inArray(data[uuid], ['upgrade', 'downgrade']) !== -1)
+                                                               chrome.webRequest.onErrorOccurred.addListener(
+                                                                       onNetworkError,
+                                                                       {
+                                                                               urls: [ UPDATE_URL + "*" ],
+                                                                               types: [ 'xmlhttprequest']
+                                                                       }
+                                                               );
+                                                       }
+
+                                                       $.ajax({
+                                                               url: UPDATE_URL,
+                                                               data: request,
+                                                               method: 'GET',
+                                                               cache: false
+                                                       }).done(function (data, textStatus, jqXHR) {
+                                                               
GSC.notifications.remove(NOTIFICATION_UPDATE_CHECK_FAILED);
+
+                                                               var toUpgrade = [];
+                                                               for (uuid in data)
+                                                               {
+                                                                       if 
(extensionsResponse.extensions[uuid] && $.inArray(data[uuid], ['upgrade', 'downgrade']) !== -1)
+                                                                       {
+                                                                               toUpgrade.push({
+                                                                                       title: 
extensionsResponse.extensions[uuid].name,
+                                                                                       message: 
m('extension_status_' + data[uuid])
+                                                                               });
+                                                                       }
+                                                               }
+
+                                                               if (toUpgrade.length > 0)
                                                                {
-                                                                       toUpgrade.push({
-                                                                               title: 
extensionsResponse.extensions[uuid].name,
-                                                                               message: 
m('extension_status_' + data[uuid])
+                                                                       
GSC.notifications.create(NOTIFICATION_UPDATE_AVAILABLE, {
+                                                                               type: 
chrome.notifications.TemplateType.LIST,
+                                                                               title: m('update_available'),
+                                                                               message: '',
+                                                                               items: toUpgrade
                                                                        });
                                                                }
-                                                       }
 
-                                                       if (toUpgrade.length > 0)
-                                                       {
-                                                               
GSC.notifications.create(NOTIFICATION_UPDATE_AVAILABLE, {
-                                                                       type: 
chrome.notifications.TemplateType.LIST,
-                                                                       title: m('update_available'),
-                                                                       message: '',
-                                                                       items: toUpgrade
+                                                               chrome.storage.local.set({
+                                                                       lastUpdateCheck: new 
Date().toLocaleString()
                                                                });
-                                                       }
+                                                       }).fail(function (jqXHR, textStatus, errorThrown) {
+                                                               if(textStatus === 'error' && !errorThrown)
+                                                               {
+                                                                       if(webRequestEnabled)
+                                                                       {
+                                                                               return;
+                                                                       }
 
-                                                       chrome.storage.local.set({
-                                                               lastUpdateCheck: new Date().toLocaleString()
-                                                       });
-                                               }).fail(function (jqXHR, textStatus, errorThrown) {
-                                                       if(textStatus === 'error' && !errorThrown)
-                                                       {
-                                                               textStatus = m('network_error');
-                                                       }
+                                                                       textStatus = m('network_error');
+                                                               }
 
-                                                       createUpdateFailedNotification(textStatus);
+                                                               createUpdateFailedNotification(textStatus);
+                                                       }).always(function() {
+                                                               if(webRequestEnabled)
+                                                               {
+                                                                       
chrome.webRequest.onErrorOccurred.removeListener(onNetworkError);
+                                                               }
+                                                       });
                                                });
                                        }
                                        else
@@ -115,6 +140,10 @@ GSC.update = (function($) {
                });
        }
 
+       function onNetworkError(details) {
+               createUpdateFailedNotification(details.error);
+       }
+
        function init() {
                chrome.alarms.onAlarm.addListener(function (alarm) {
                        if (alarm.name === ALARM_UPDATE_CHECK)
diff --git a/extension/manifest.json b/extension/manifest.json
index 75c70e3..2bbcf36 100644
--- a/extension/manifest.json
+++ b/extension/manifest.json
@@ -59,5 +59,6 @@
     "storage",
     "https://extensions.gnome.org/";
   ],
+  "optional_permissions": [ "webRequest" ],
   "web_accessible_resources": ["include/sweettooth-api.js"]
 }
diff --git a/extension/options.html b/extension/options.html
index 21cc735..9ae5297 100644
--- a/extension/options.html
+++ b/extension/options.html
@@ -31,6 +31,16 @@
                                        </dd>
                                </dl>
                                <dl>
+                                       <dt>
+                                               <label for='show_network_errors_yes' 
data-i18n="options_show_network_errors"></label>:<br />
+                                               <span class="notice wrapped" 
data-i18n="options_show_network_errors_notice"></span>
+                                       </dt>
+                                       <dd>
+                                               <label for='show_network_errors_yes' data-i18n="yes"></label> 
<input type='radio' id='show_network_errors_yes' name='show_network_errors' />
+                                               <label for='show_network_errors_no' data-i18n="no"></label> 
<input type='radio' id='show_network_errors_no' name='show_network_errors' />
+                                       </dd>
+                               </dl>
+                               <dl>
                                        <dt><span data-i18n="options_show_release_notes"></span>:</dt>
                                        <dd>
                                                <label for='show_release_notes_yes' data-i18n="yes"></label> 
<input type='radio' id='show_release_notes_yes' name='show_release_notes' />
diff --git a/extension/options.js b/extension/options.js
index 683b956..39aeab5 100644
--- a/extension/options.js
+++ b/extension/options.js
@@ -37,6 +37,12 @@ function restore_options()
 
                retrieveUpdateTimes();
        });
+
+       chrome.permissions.contains({
+               permissions: ["webRequest"]
+       }, function(result) {
+               setNetworkErrors(result);
+       });
 }
 
 function retrieveUpdateTimes()
@@ -87,10 +93,41 @@ function setReleaseNotes(result)
                $('#show_release_notes_no').prop('checked', true);
 }
 
+function setNetworkErrors(result)
+{
+       if(result)
+               $('#show_network_errors_yes').prop('checked', true);
+       else
+               $('#show_network_errors_no').prop('checked', true);
+}
+
+function handleWebrequestPermission()
+{
+       if($('#show_network_errors_yes').prop('checked'))
+       {
+               chrome.permissions.request({
+                       permissions: ["webRequest"]
+               }, function(granted) {
+                       setNetworkErrors(granted);
+               });
+       }
+       else
+       {
+               chrome.permissions.remove({
+                       permissions: ["webRequest"]
+               }, function(removed) {
+                       setNetworkErrors(!removed);
+               });
+       }
+}
+
 i18n();
 
 document.addEventListener('DOMContentLoaded', restore_options);
 document.getElementById('save').addEventListener('click', save_options);
+$.each(document.getElementsByName('show_network_errors'), function(index, control) {
+       control.addEventListener('change', handleWebrequestPermission);
+});
 
 if($('#translation_credits div').is(':empty'))
 {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]