OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @fileoverview Utility objects and functions for Google Now extension. | 8 * @fileoverview Utility objects and functions for Google Now extension. |
9 * Most important entities here: | 9 * Most important entities here: |
10 * (1) 'wrapper' is a module used to add error handling and other services to | 10 * (1) 'wrapper' is a module used to add error handling and other services to |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 */ | 95 */ |
96 function verify(condition, message) { | 96 function verify(condition, message) { |
97 if (!condition) | 97 if (!condition) |
98 throw buildErrorWithMessageForServer('ASSERT: ' + message); | 98 throw buildErrorWithMessageForServer('ASSERT: ' + message); |
99 } | 99 } |
100 | 100 |
101 /** | 101 /** |
102 * Builds a request to the notification server. | 102 * Builds a request to the notification server. |
103 * @param {string} method Request method. | 103 * @param {string} method Request method. |
104 * @param {string} handlerName Server handler to send the request to. | 104 * @param {string} handlerName Server handler to send the request to. |
105 * @param {string=} contentType Value for the Content-type header. | 105 * @param {string=} opt_contentType Value for the Content-type header. |
106 * @return {XMLHttpRequest} Server request. | 106 * @return {XMLHttpRequest} Server request. |
107 */ | 107 */ |
108 function buildServerRequest(method, handlerName, contentType) { | 108 function buildServerRequest(method, handlerName, opt_contentType) { |
109 var request = new XMLHttpRequest(); | 109 var request = new XMLHttpRequest(); |
110 | 110 |
111 request.responseType = 'text'; | 111 request.responseType = 'text'; |
112 request.open(method, NOTIFICATION_CARDS_URL + '/' + handlerName, true); | 112 request.open(method, NOTIFICATION_CARDS_URL + '/' + handlerName, true); |
113 if (contentType) | 113 if (opt_contentType) |
114 request.setRequestHeader('Content-type', contentType); | 114 request.setRequestHeader('Content-type', opt_contentType); |
115 | 115 |
116 return request; | 116 return request; |
117 } | 117 } |
118 | 118 |
119 /** | 119 /** |
120 * Sends an error report to the server. | 120 * Sends an error report to the server. |
121 * @param {Error} error Error to send. | 121 * @param {Error} error Error to send. |
122 */ | 122 */ |
123 function sendErrorReport(error) { | 123 function sendErrorReport(error) { |
124 // Don't remove 'error.stack.replace' below! | 124 // Don't remove 'error.stack.replace' below! |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 if (error.canSendMessageToServer) | 162 if (error.canSendMessageToServer) |
163 errorText = errorText + ': ' + error.message; | 163 errorText = errorText + ': ' + error.message; |
164 | 164 |
165 var errorObject = { | 165 var errorObject = { |
166 message: errorText, | 166 message: errorText, |
167 file: file, | 167 file: file, |
168 line: line, | 168 line: line, |
169 trace: filteredStack | 169 trace: filteredStack |
170 }; | 170 }; |
171 | 171 |
| 172 // We use relatively direct calls here because the instrumentation may be in |
| 173 // a bad state. Wrappers and promises should not be involved in the reporting. |
172 var request = buildServerRequest('POST', 'jserrors', 'application/json'); | 174 var request = buildServerRequest('POST', 'jserrors', 'application/json'); |
173 request.onloadend = function(event) { | 175 request.onloadend = function(event) { |
174 console.log('sendErrorReport status: ' + request.status); | 176 console.log('sendErrorReport status: ' + request.status); |
175 }; | 177 }; |
176 | 178 |
177 chrome.identity.getAuthToken({interactive: false}, function(token) { | 179 chrome.identity.getAuthToken({interactive: false}, function(token) { |
178 if (token) { | 180 if (token) { |
179 request.setRequestHeader('Authorization', 'Bearer ' + token); | 181 request.setRequestHeader('Authorization', 'Bearer ' + token); |
180 request.send(JSON.stringify(errorObject)); | 182 request.send(JSON.stringify(errorObject)); |
181 } | 183 } |
(...skipping 843 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1025 // One hour is just an arbitrary amount of time chosen. | 1027 // One hour is just an arbitrary amount of time chosen. |
1026 chrome.alarms.create(alarmName, {periodInMinutes: 60}); | 1028 chrome.alarms.create(alarmName, {periodInMinutes: 60}); |
1027 | 1029 |
1028 return { | 1030 return { |
1029 addListener: addListener, | 1031 addListener: addListener, |
1030 getAuthToken: getAuthToken, | 1032 getAuthToken: getAuthToken, |
1031 isSignedIn: isSignedIn, | 1033 isSignedIn: isSignedIn, |
1032 removeToken: removeToken | 1034 removeToken: removeToken |
1033 }; | 1035 }; |
1034 } | 1036 } |
OLD | NEW |