Skip to content
Browse files

Merge branch 'master' of https://github.com/GoogleChrome/chrome-app-s…

  • Loading branch information...
2 parents e066c88 + 641c335 commit 111ee14f94d58c1a976610b2f20eb0ded961e8a3 @PaulKinlan PaulKinlan committed Nov 16, 2012
Showing with 122 additions and 33 deletions.
  1. +1 −1 restarted-demo/README.md
  2. +119 −8 restarted-demo/background.js
  3. +2 −2 restarted-demo/main.html
  4. +0 −22 restarted-demo/main.js
View
2 restarted-demo/README.md
@@ -2,7 +2,7 @@
When an app is restored after being unexpectedly terminated (eg, when the browser restarts) it will be sent an onRestarted event which should restore the app to the state it was in when it was last running.
-This demo app contains a counter which is reset when the app is launched, but preserved across app restarts.
+This demo app creates a new counter on launch and restores any existing counters across app restarts.
## APIs
View
127 restarted-demo/background.js
@@ -1,24 +1,135 @@
-function runApp(startedBy, data) {
+// All the counters.
+Counter.all = [];
+
+function Counter(id, clicks, startedBy) {
+ this.id = id;
+ this.clicks = clicks;
+ this.startedBy = startedBy;
+ this.saving = false;
+ this.listeners = [];
+ this.save();
+ Counter.all.push(this);
+}
+
+Counter.prototype.attachToWindow = function(win, document) {
+ win.onClosed.addListener(this.close.bind(this));
+
+ var self = this;
+ document.getElementById('started-by').innerText = this.startedBy;
+ this.addListener(function(clicks) {
+ document.getElementById('number').innerText = clicks;
+ });
+
+ document.getElementById('clickButton').addEventListener('click', function(e) {
+ self.increment();
+ }, false);
+
+ document.getElementById('logLocalStorage').addEventListener('click', function(e) {
+ logLocalStorage();
+ }, false);
+};
+
+Counter.prototype.addListener = function(l) {
+ this.listeners.push(l);
+ this.notifyClickCount();
+};
+
+Counter.prototype.clearListeners = function() {
+ this.listeners = [];
+};
+
+Counter.prototype.notifyClickCount = function() {
+ for (var i = 0; i < this.listeners.length; i++) {
+ this.listeners[i](this.clicks);
+ }
+};
+
+Counter.prototype.increment = function() {
+ this.clicks++;
+ this.notifyClickCount();
+ this.save();
+};
+
+Counter.prototype.save = function() {
+ if (this.saving)
+ return;
+
+ this.saving = true;
+
+ var self = this;
+ (function(clicks) {
+ var counters = {};
+ counters[self.id] = {'clicks': clicks};
+ var data = {'counters': counters};
+ chrome.storage.local.get('counters', function(data) {
+ if (!data.counters) {
+ data.counters = {};
+ }
+ data.counters[self.id] = {'clicks': clicks};
+ chrome.storage.local.set(data, function() {
+ self.saving = false;
+ if (self.clicks != clicks) {
+ // self.clicks changed while we were saving, so save again.
+ self.save();
+ }
+ });
+ });
+ })(this.clicks);
+};
+
+Counter.prototype.close = function() {
+ this.clearListeners();
+ var self = this;
+ chrome.storage.local.get('counters', function(data) {
+ delete data.counters[self.id];
+ chrome.storage.local.set(data);
+ });
+
+ // Remove self from global list.
+ var i = Counter.all.indexOf(this);
+ if (i != -1)
+ Counter.splice(i, 1);
+};
+
+function runApp(counter) {
chrome.app.window.create('main.html', {
+ id: counter.id + '',
width: 800,
height: 600
}, function(win) {
win.contentWindow.onload = function() {
- win.contentWindow.setStartedBy(startedBy);
- win.contentWindow.updateData(data);
+ counter.attachToWindow(win, win.contentWindow.document);
};
});
}
chrome.app.runtime.onLaunched.addListener(function() {
- var data = {'clicks': 0};
- chrome.storage.local.set(data, function() {
- runApp('launched', data);
+ if (Counter.all.length == 0) {
+ // We might have left over state from a previous hard shutdown.
+ chrome.storage.local.clear();
+ }
+ chrome.storage.local.get('nextId', function(data) {
+ if (!data.nextId)
+ data.nextId = 0;
+ var id = data.nextId++;
+ var counter = new Counter(id, 0, 'launched');
+ runApp(counter);
+ chrome.storage.local.set(data);
});
});
chrome.app.runtime.onRestarted.addListener(function() {
- chrome.storage.local.get('clicks', function(data) {
- runApp('restarted', data);
+ chrome.storage.local.get(null, function(data) {
+ for (var id in data.counters) {
+ var clicks = data.counters[id].clicks;
+ var counter = new Counter(id, clicks, 'restarted');
+ runApp(counter);
+ }
});
});
+
+function logLocalStorage() {
+ chrome.storage.local.get(null, function(data) {
+ console.log("local storage:", data);
+ });
+}
View
4 restarted-demo/main.html
@@ -10,6 +10,6 @@
<td>Clicks</td><td id='number'></td>
</tr>
</table>
-<input id='button' type='submit' value='Click'></input>
+<input id='clickButton' type='submit' value='Click'></input>
+<input id='logLocalStorage' type='submit' value='Log local storage'></input>
</body>
-<script src='main.js'></script>
View
22 restarted-demo/main.js
@@ -1,22 +0,0 @@
-function setStartedBy(startedBy) {
- document.getElementById('started-by').innerText = startedBy;
-}
-
-function updateData(data) {
- var p = document.getElementById('number');
- p.innerText = data.clicks;
- chrome.runtime.getBackgroundPage(function(c) {
- c.chrome.storage.local.set(data);
- });
-}
-
-document.getElementById('button').addEventListener('click', function(e) {
- chrome.runtime.getBackgroundPage(function(c) {
- c.chrome.storage.local.get('clicks', function(data) {
- data.clicks += 1;
- c.chrome.storage.local.set(data, function() {
- updateData(data);
- });
- });
- });
-}, false);

0 comments on commit 111ee14

Please sign in to comment.
Something went wrong with that request. Please try again.