Chromium Code Reviews| 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 var Galore = Galore || {}; | 5 var STOPPED = "Stopped"; |
| 6 | 6 var RECORDING = "Recording"; |
| 7 Galore.controller = { | 7 var PAUSED_RECORDING = "Recording Paused"; |
| 8 /** @constructor */ | 8 var PAUSED_PLAYING = "Playing Paused"; |
| 9 create: function() { | 9 var PLAYING = "Playing"; |
| 10 var controller = Object.create(this); | 10 |
| 11 controller.api = chrome; | 11 var recordingState = STOPPED; |
| 12 controller.counter = 0; | 12 |
| 13 return controller; | 13 // Timestamp when current segment started. |
| 14 }, | 14 var segmentStart; |
| 15 | 15 // Segment duration accumulated before pause button was hit. |
| 16 createWindow: function() { | 16 var pausedDuration; |
| 17 chrome.storage.sync.get('settings', this.onSettingsFetched_.bind(this)); | 17 // The array of segments, with delay and action. |
| 18 }, | 18 var recordingList; |
| 19 | 19 // When this timer fires, the next segment from recordingList should be played. |
| 20 /** @private */ | 20 var playingTimer; |
| 21 onSettingsFetched_: function(items) { | 21 var currentSegmentIndex; |
| 22 var request = new XMLHttpRequest(); | 22 // A set of web Notifications - used to delete them during playback by id. |
| 23 var settings = items.settings || {}; | 23 var webNotifications = {}; |
| 24 var source = settings.data || '/data/' + this.getDataVersion_(); | 24 |
| 25 request.open('GET', source, true); | 25 var recorderButtons = [ "play", "record", "pause", "stop"]; |
| 26 request.responseType = 'text'; | 26 var recorderButtonStates = [ |
| 27 request.onload = this.onDataFetched_.bind(this, settings, request); | 27 { state: STOPPED, enabled: "play record" }, |
| 28 request.send(); | 28 { state: RECORDING, enabled: "pause stop" }, |
| 29 }, | 29 { state: PAUSED_RECORDING, enabled: "record stop" }, |
| 30 | 30 { state: PAUSED_PLAYING, enabled: "play stop" }, |
| 31 /** @private */ | 31 { state: PLAYING, enabled: "pause stop" } |
| 32 onDataFetched_: function(settings, request) { | 32 ]; |
| 33 var count = 0; | 33 |
| 34 var data = JSON.parse(request.response); | 34 function updateButtonsState() { |
| 35 recorderButtonStates.map(function(entry) { | |
| 36 if (entry.state != recordingState) | |
| 37 return; | |
| 38 var disabled = recorderButtons.slice(0); // copy | |
| 39 var enabled = entry.enabled.split(" "); | |
| 40 for (var i = 0; i < enabled.length; i++) { | |
| 41 disabled.splice(disabled.indexOf(enabled[i]), 1); | |
| 42 enabled[i] = "#" + enabled[i]; | |
| 43 } | |
| 44 for (var i = 0; i < disabled.length; i++) { | |
| 45 disabled[i] = "#" + disabled[i]; | |
| 46 } | |
| 47 getElements(disabled.join(", ")).forEach(function(element) { | |
| 48 element.setAttribute("disabled", "true") | |
| 49 }) | |
| 50 getElements(enabled.join(", ")).forEach(function(element) { | |
| 51 element.removeAttribute("disabled") | |
| 52 }) | |
| 53 }) | |
| 54 } | |
| 55 | |
| 56 | |
| 57 function setRecordingState(newState) { | |
| 58 setRecorderStatusText(newState); | |
| 59 recordingState = newState; | |
| 60 updateButtonsState(); | |
| 61 } | |
| 62 | |
| 63 function updateRecordingStats(context) { | |
| 64 var length = 0; | |
| 65 var segmentCnt = 0; | |
| 66 recordingList.slice(currentSegmentIndex).forEach(function(segment) { | |
| 67 length += segment.delay || 0; | |
| 68 segmentCnt++; | |
| 69 }) | |
| 70 updateRecordingStatsDisplay(context + ": " + (segmentCnt-1) + " segments, " + | |
| 71 Math.floor(length/1000) + " seconds."); | |
| 72 } | |
| 73 | |
| 74 function loadRecording() { | |
| 75 chrome.storage.local.get("recording", function(items) { | |
| 76 recordingList = JSON.parse(items["recording"] || "[]"); | |
| 77 setRecordingState(STOPPED); | |
| 78 updateRecordingStats("Loaded record"); | |
| 79 }) | |
| 80 } | |
| 81 | |
| 82 function finalizeRecording() { | |
| 83 chrome.storage.local.set({"recording": JSON.stringify(recordingList)}); | |
| 84 updateRecordingStats("Recorded"); | |
| 85 } | |
| 86 | |
| 87 function setPreviousSegmentDuration() { | |
| 88 var now = new Date().getTime(); | |
| 89 var delay = now - segmentStart; | |
| 90 segmentStart = now; | |
| 91 recordingList[recordingList.length - 1].delay = delay; | |
| 92 } | |
| 93 | |
| 94 function recordCreate(kind, id, options) { | |
| 95 if (recordingState != RECORDING) | |
| 96 return; | |
| 97 setPreviousSegmentDuration(); | |
| 98 recordingList.push({ type: "create", kind: kind, id: id, options: options }); | |
| 99 updateRecordingStats("Recording"); | |
| 100 } | |
| 101 | |
| 102 function recordDelete(kind, id) { | |
| 103 if (recordingState != RECORDING) | |
| 104 return; | |
| 105 setPreviousSegmentDuration(); | |
| 106 recordingList.push({ type: "delete", kind: kind, id: id }); | |
| 107 updateRecordingStats("Recording"); | |
| 108 } | |
| 109 | |
| 110 function startPlaying() { | |
| 111 if (recordingList.length < 2) | |
| 112 return false; | |
| 113 | |
| 114 setRecordingState(PLAYING); | |
| 115 | |
| 116 if (playingTimer) | |
| 117 clearTimeout(playingTimer); | |
| 118 | |
| 119 webNotifications = {}; | |
| 120 currentSegmentIndex = 0; | |
| 121 playingTimer = setTimeout(playNextSegment, | |
| 122 recordingList[currentSegmentIndex].delay); | |
| 123 updateRecordingStats("Playing"); | |
| 124 } | |
| 125 | |
| 126 function playNextSegment() { | |
| 127 currentSegmentIndex++; | |
| 128 var segment = recordingList[currentSegmentIndex]; | |
| 129 if (!segment) { | |
| 130 stopPlaying(); | |
| 131 return; | |
| 132 } | |
| 133 | |
| 134 if (segment.type == "create") { | |
| 135 createNotificationForPlay(segment.kind, segment.id, segment.options); | |
| 136 } else { // type == "delete" | |
| 137 deleteNotificaitonForPlay(segment.kind, segment.id); | |
| 138 } | |
| 139 playingTimer = setTimeout(playNextSegment, | |
| 140 recordingList[currentSegmentIndex].delay); | |
| 141 segmentStart = new Date().getTime(); | |
| 142 updateRecordingStats("Playing"); | |
| 143 } | |
| 144 | |
| 145 function deleteNotificaitonForPlay(kind, id) { | |
|
dewittj
2014/06/07 00:08:04
spelling :)
| |
| 146 if (kind == 'web') { | |
| 147 webNotifications[id].close(); | |
| 148 } else { | |
| 149 chrome.notifications.clear(id, function() {}); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 function createNotificationForPlay(kind, id, options) { | |
| 154 if (kind == 'web') { | |
| 155 webNotifications[id] = createWebNotification(id, options); | |
| 156 } else { | |
| 157 var type = options.type; | |
| 158 var priority = options.priority; | |
| 159 createRichNotification(id, type, priority, options); | |
| 160 } | |
| 161 } | |
| 162 function stopPlaying() { | |
| 163 currentSegmentIndex = 0; | |
| 164 clearTimeout(playingTimer); | |
| 165 updateRecordingStats("Record"); | |
| 166 setRecordingState(STOPPED); | |
| 167 } | |
| 168 | |
| 169 function pausePlaying() { | |
| 170 clearTimeout(playingTimer); | |
| 171 pausedDuration = new Date().getTime() - segmentStart; | |
| 172 setRecordingState(PAUSED_PLAYING); | |
| 173 } | |
| 174 | |
| 175 function unpausePlaying() { | |
| 176 var remainingInSegment = | |
| 177 recordingList[currentSegmentIndex].delay - pausedDuration; | |
| 178 if (remainingInSegment < 0) | |
| 179 remainingInSegment = 0; | |
| 180 playingTimer = setTimeout(playNextSegment, remainingInSegment); | |
| 181 segmentStart = new Date().getTime() - pausedDuration; | |
| 182 } | |
| 183 | |
| 184 function onRecord() { | |
| 185 if (recordingState == STOPPED) { | |
| 186 segmentStart = new Date().getTime(); | |
| 187 pausedDuration = 0; | |
| 188 // This item is only needed to keep a duration of the delay between start | |
| 189 // and first action. | |
| 190 recordingList = [ { type:"start" } ]; | |
| 191 } else if (recordingState == PAUSED_RECORDING) { | |
| 192 segmentStart = new Date().getTime() - pausedDuration; | |
| 193 pausedDuration = 0; | |
| 194 } else { | |
| 195 return; | |
| 196 } | |
| 197 updateRecordingStats("Recording"); | |
| 198 setRecordingState(RECORDING); | |
| 199 } | |
| 200 | |
| 201 function pauseRecording() { | |
| 202 pausedDuration = new Date().getTime() - segmentStart; | |
| 203 segmentStart = 0; | |
| 204 setRecordingState(PAUSED_RECORDING); | |
| 205 } | |
| 206 | |
| 207 function onPause() { | |
| 208 if (recordingState == RECORDING) { | |
| 209 pauseRecording(); | |
| 210 } else if (recordingState == PLAYING) { | |
| 211 pausePlaying(); | |
| 212 } else { | |
| 213 return; | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 function onStop() { | |
| 218 switch (recordingState) { | |
| 219 case PAUSED_RECORDING: | |
| 220 segmentStart = new Date().getTime() - pausedDuration; | |
| 221 // fall through | |
| 222 case RECORDING: | |
| 223 finalizeRecording(); | |
| 224 break; | |
| 225 case PLAYING: | |
| 226 case PAUSED_PLAYING: | |
| 227 stopPlaying(); | |
| 228 break; | |
| 229 } | |
| 230 setRecordingState(STOPPED); | |
| 231 } | |
| 232 | |
| 233 function onPlay() { | |
| 234 if (recordingState == STOPPED) { | |
| 235 if (!startPlaying()) | |
| 236 return; | |
| 237 } else if (recordingState == PAUSED_PLAYING) { | |
| 238 unpausePlaying(); | |
| 239 } | |
| 240 setRecordingState(PLAYING); | |
| 241 } | |
| 242 | |
| 243 function createWindow() { | |
| 244 chrome.storage.local.get('settings', onSettingsFetched); | |
| 245 } | |
| 246 | |
| 247 function onSettingsFetched(items) { | |
| 248 settings = items.settings || settings; | |
| 249 var request = new XMLHttpRequest(); | |
| 250 var source = '/data/data.json'; | |
| 251 request.open('GET', source, true); | |
| 252 request.responseType = 'text'; | |
| 253 request.onload = onDataFetched; | |
| 254 request.send(); | |
| 255 } | |
| 256 | |
| 257 function onDataFetched() { | |
| 258 var data = JSON.parse(this.response); | |
| 259 createAppWindow(function() { | |
| 260 // Create notification buttons. | |
| 35 data.forEach(function(section) { | 261 data.forEach(function(section) { |
| 262 var type = section.notificationType; | |
| 36 (section.notificationOptions || []).forEach(function(options) { | 263 (section.notificationOptions || []).forEach(function(options) { |
| 37 ++count; | 264 addNotificationButton(section.sectionName, |
| 38 this.fetchImages_(options, function() { | 265 options.title, |
| 39 if (--count == 0) | 266 options.iconUrl, |
| 40 this.onImagesFetched_(settings, data); | 267 function() { createNotification(type, options) }); |
| 41 }.bind(this)); | 268 }); |
| 42 }, this); | |
| 43 }, this); | |
| 44 }, | |
| 45 | |
| 46 /** @private */ | |
| 47 onImagesFetched_: function(settings, data) { | |
| 48 this.settings = settings; | |
| 49 this.view = Galore.view.create(this.settings, function() { | |
| 50 // Create buttons. | |
| 51 data.forEach(function(section) { | |
| 52 var defaults = section.globals || data[0].globals; | |
| 53 var type = section.notificationType; | |
| 54 (section.notificationOptions || []).forEach(function(options) { | |
| 55 var defaulted = this.getDefaultedOptions_(options, defaults); | |
| 56 var create = this.createNotification_.bind(this, type, defaulted); | |
| 57 this.view.addNotificationButton(section.sectionName, | |
| 58 defaulted.title, | |
| 59 defaulted.iconUrl, | |
| 60 create); | |
| 61 }, this); | |
| 62 }, this); | |
| 63 // Set the API entry point and use it to set event listeners. | |
| 64 this.api = this.getApi_(data); | |
| 65 if (this.api) | |
| 66 this.addListeners_(this.api, data[0].events); | |
| 67 // Display the completed and ready window. | |
| 68 this.view.showWindow(); | |
| 69 }.bind(this), this.onSettingsChange_.bind(this)); | |
| 70 }, | |
| 71 | |
| 72 /** @private */ | |
| 73 fetchImages_: function(options, onFetched) { | |
| 74 var count = 0; | |
| 75 var replacements = {}; | |
| 76 this.mapStrings_(options, function(string) { | |
| 77 if (string.indexOf("/images/") == 0 || string.search(/https?:\//) == 0) { | |
| 78 ++count; | |
| 79 this.fetchImage_(string, function(url) { | |
| 80 replacements[string] = url; | |
| 81 if (--count == 0) { | |
| 82 this.mapStrings_(options, function(string) { | |
| 83 return replacements[string] || string; | |
| 84 }); | |
| 85 onFetched.call(this, options); | |
| 86 } | |
| 87 }); | |
| 88 } | |
| 89 }); | 269 }); |
| 90 }, | 270 loadRecording(); |
| 91 | 271 addListeners(); |
| 92 /** @private */ | 272 showWindow(); |
| 93 fetchImage_: function(url, onFetched) { | 273 }); |
| 94 var request = new XMLHttpRequest(); | 274 } |
| 95 request.open('GET', url, true); | 275 |
| 96 request.responseType = 'blob'; | 276 function onSettingsChange(settings) { |
| 97 request.onload = function() { | 277 chrome.storage.local.set({settings: settings}); |
| 98 var url = window.URL.createObjectURL(request.response); | 278 } |
| 99 onFetched.call(this, url); | 279 |
| 100 }.bind(this); | 280 function createNotification(type, options) { |
| 101 request.send(); | 281 var id = getNextId(); |
| 102 }, | 282 var priority = Number(settings.priority || 0); |
| 103 | 283 if (type == 'webkit') |
| 104 /** @private */ | 284 createWebNotification(id, options); |
| 105 onSettingsChange_: function(settings) { | 285 else |
| 106 this.settings = settings; | 286 createRichNotification(id, type, priority, options); |
| 107 chrome.storage.sync.set({settings: this.settings}); | 287 } |
| 108 }, | 288 |
| 109 | 289 function createWebNotification(id, options) { |
| 110 /** @private */ | 290 var iconUrl = options.iconUrl; |
| 111 createNotification_: function(type, options) { | 291 var title = options.title; |
| 112 var id = this.getNextId_(); | 292 var message = options.message; |
| 113 var priority = Number(this.settings.priority || 0); | 293 var n = new Notification(title, { |
| 114 var expanded = this.getExpandedOptions_(options, id, type, priority); | 294 body: message, |
| 115 if (type == 'webkit') | 295 icon: iconUrl, |
| 116 this.createWebKitNotification_(expanded); | 296 tag: id |
| 117 else | 297 }); |
| 118 this.createRichNotification_(expanded, id, type, priority); | 298 n.onshow = function() { logEvent('WebNotification #' + id + ': onshow'); } |
| 119 }, | 299 n.onclick = function() { logEvent('WebNotification #' + id + ': onclick'); } |
| 120 | 300 n.onclose = function() { |
| 121 /** @private */ | 301 logEvent('WebNotification #' + id + ': onclose'); |
| 122 createWebKitNotification_: function(options) { | 302 recordDelete('web', id); |
| 123 var iconUrl = options.iconUrl; | 303 } |
| 124 var title = options.title; | 304 logCreate('Web', id, 'title: "' + title + '"'); |
| 125 var message = options.message; | 305 recordCreate('web', id, options); |
| 126 new Notification(title, { | 306 return n; |
| 127 body: message, | 307 } |
| 128 icon: iconUrl | 308 |
| 129 }); | 309 function createRichNotification(id, type, priority, options) { |
| 130 this.handleEvent_('create', '?', 'title: "' + title + '"'); | 310 options["type"] = type; |
| 131 }, | 311 options["priority"] = priority; |
| 132 | 312 chrome.notifications.create(id, options, function() { |
| 133 /** @private */ | 313 var argument1 = 'type: "' + type + '"'; |
| 134 createRichNotification_: function(options, id, type, priority) { | 314 var argument2 = 'priority: ' + priority; |
| 135 this.api.create(id, options, function() { | 315 var argument3 = 'title: "' + options.title + '"'; |
| 136 var argument1 = 'type: "' + type + '"'; | 316 logCreate('Rich', id, argument1, argument2, argument3); |
| 137 var argument2 = 'priority: ' + priority; | 317 }); |
| 138 var argument3 = 'title: "' + options.title + '"'; | 318 recordCreate('rich', id, options); |
| 139 this.handleEvent_('create', id, argument1, argument2, argument3); | 319 } |
| 140 }.bind(this)); | 320 |
| 141 }, | 321 var counter = 0; |
| 142 | 322 function getNextId() { |
| 143 /** @private */ | 323 return String(counter++); |
| 144 getNextId_: function() { | 324 } |
| 145 this.counter += 1; | 325 |
| 146 return String(this.counter); | 326 function addListeners() { |
| 147 }, | 327 chrome.notifications.onClosed.addListener(onClosed); |
| 148 | 328 chrome.notifications.onClicked.addListener(onClicked); |
| 149 /** @private */ | 329 chrome.notifications.onButtonClicked.addListener(onButtonClicked); |
| 150 getDefaultedOptions_: function(options, defaults) { | 330 } |
| 151 var defaulted = this.deepCopy_(options); | 331 |
| 152 Object.keys(defaults || {}).forEach(function (key) { | 332 function logCreate(kind, id, var_args) { |
| 153 defaulted[key] = options[key] || defaults[key]; | 333 logEvent(kind + ' Notification #' + id + ': created ' + '(' + |
| 154 }); | 334 Array.prototype.slice.call(arguments, 2).join(', ') + ')'); |
| 155 return defaulted; | 335 } |
| 156 }, | 336 |
| 157 | 337 function onClosed(id) { |
| 158 /** @private */ | 338 logEvent('Notification #' + id + ': onClosed'); |
| 159 getExpandedOptions_: function(options, id, type, priority) { | 339 recordDelete('rich', id); |
| 160 var expanded = this.deepCopy_(options); | 340 } |
| 161 return this.mapStrings_(expanded, function(string) { | 341 |
| 162 return this.getExpandedOption_(string, id, type, priority); | 342 function onClicked(id) { |
| 163 }, this); | 343 logEvent('Notification #' + id + ': onClicked'); |
| 164 }, | 344 } |
| 165 | 345 |
| 166 /** @private */ | 346 function onButtonClicked(id, index) { |
| 167 getExpandedOption_: function(option, id, type, priority) { | 347 logEvent('Notification #' + id + ': onButtonClicked, btn: ' + index); |
| 168 if (option == '$!') { | 348 } |
| 169 option = priority; // Avoids making priorities into strings. | |
| 170 } else { | |
| 171 option = option.replace(/\$#/g, id); | |
| 172 option = option.replace(/\$\?/g, type); | |
| 173 option = option.replace(/\$\!/g, priority); | |
| 174 } | |
| 175 return option; | |
| 176 }, | |
| 177 | |
| 178 /** @private */ | |
| 179 deepCopy_: function(value) { | |
| 180 var copy = value; | |
| 181 if (Array.isArray(value)) { | |
| 182 copy = value.map(this.deepCopy_, this); | |
| 183 } else if (value && typeof value === 'object') { | |
| 184 copy = {} | |
| 185 Object.keys(value).forEach(function (key) { | |
| 186 copy[key] = this.deepCopy_(value[key]); | |
| 187 }, this); | |
| 188 } | |
| 189 return copy; | |
| 190 }, | |
| 191 | |
| 192 /** @private */ | |
| 193 mapStrings_: function(value, map) { | |
| 194 var mapped = value; | |
| 195 if (typeof value === 'string') { | |
| 196 mapped = map.call(this, value); | |
| 197 mapped = (typeof mapped !== 'undefined') ? mapped : value; | |
| 198 } else if (value && typeof value == 'object') { | |
| 199 Object.keys(value).forEach(function (key) { | |
| 200 mapped[key] = this.mapStrings_(value[key], map); | |
| 201 }, this); | |
| 202 } | |
| 203 return mapped; | |
| 204 }, | |
| 205 | |
| 206 /** @private */ | |
| 207 addListeners_: function(api, events) { | |
| 208 (events || []).forEach(function(event) { | |
| 209 var listener = this.handleEvent_.bind(this, event); | |
| 210 if (api[event]) | |
| 211 api[event].addListener(listener); | |
| 212 else | |
| 213 console.log('Event ' + event + ' not defined.'); | |
| 214 }, this); | |
| 215 }, | |
| 216 | |
| 217 /** @private */ | |
| 218 handleEvent_: function(event, id, var_args) { | |
| 219 this.view.logEvent('Notification #' + id + ': ' + event + '(' + | |
| 220 Array.prototype.slice.call(arguments, 2).join(', ') + | |
| 221 ')'); | |
| 222 }, | |
| 223 | |
| 224 /** @private */ | |
| 225 getDataVersion_: function() { | |
| 226 var version = navigator.appVersion.replace(/^.* Chrome\//, ''); | |
| 227 return (version > '28.0.1500.70') ? '28.0.1500.70.json' : | |
| 228 (version > '27.0.1433.1') ? '27.0.1433.1.json' : | |
| 229 (version > '27.0.1432.2') ? '27.0.1432.2.json' : | |
| 230 '27.0.0.0.json'; | |
| 231 }, | |
| 232 | |
| 233 /** @private */ | |
| 234 getApi_: function(data) { | |
| 235 var path = data[0].api || 'notifications'; | |
| 236 var api = chrome; | |
| 237 path.split('.').forEach(function(key) { api = api && api[key]; }); | |
| 238 if (!api) | |
| 239 this.view.logError('No API found - chrome.' + path + ' is undefined'); | |
| 240 return api; | |
| 241 } | |
| 242 | |
| 243 }; | |
| OLD | NEW |