Permalink
Browse files

Fix #10, handle case where navigator.mozApps is not supported by the…

… user agent. Also a new state, verifier.states.MozAppsNotSupported.
  • Loading branch information...
ianb committed Jun 29, 2012
1 parent 5f2ba10 commit b75f60b1307d9325d5c337a77a9d7cf20c579de1
Showing with 34 additions and 2 deletions.
  1. +2 −0 README.md
  2. +19 −2 receiptverifier.js
  3. +1 −0 test-ui.html
  4. +12 −0 test.html
View
@@ -193,6 +193,8 @@ The `verifier.state` object can be an instance of one of these items; each is a
**NetworkError**: some network error occurred that kept validation from completing. That is, a receipt seemed okay but we weren't able to contact the server to verify if. This will happen when the user agent is offline.
+**MozAppsNotSupported**: the `navigator.mozApps` API is not supported by this client. The app can't be "installed" on this browser.
+
**ServerError**: subclass of `NetworkError`; the server did something wrong. This might be an invalid response from the server, or a wifi login in the way, or the server is down, etc. Like a network error, it's not the user's fault!
**VerificationIncomplete**: this is the state until the verification actually completes.
View
@@ -98,6 +98,7 @@ Verifier.State.prototype.toString = function () {
Verifier.states = {};
Verifier.states.VerificationIncomplete = Verifier.State("VerificationIncomplete");
Verifier.states.NeedsInstall = Verifier.State("NeedsInstall");
+Verifier.states.MozAppsNotSupported = Verifier.State("MozAppsNotSupported");
Verifier.states.NetworkError = Verifier.State("NetworkError");
Verifier.states.NotInstalled = Verifier.State("NotInstalled", Verifier.states.NeedsInstall);
Verifier.states.NoReceipts = Verifier.State("NoReceipts", Verifier.states.NeedsInstall);
@@ -180,9 +181,14 @@ Verifier.prototype = {
},
verify: function (onVerified) {
+ var self = this;
this.state = new this.states.VerificationIncomplete(".verify() has not completed");
+ if (! navigator.mozApps) {
+ this.state = new this.states.MozAppsNotSupported("navigator.mozApps does not exist");
+ onVerified(self);
+ return;
+ }
var result = navigator.mozApps.getSelf();
- var self = this;
result.onsuccess = function () {
try {
self.app = this.result || null;
@@ -724,7 +730,8 @@ Prompter.prototype = {
invalidReceiptIssuer: 'You purchased this application from <%= error.iss %> which is not a store we have a relationship with. Please either <a href="<%= quote(storeURL) %>">re-purchase the application</a> or contact support: <%= supportHTML %>',
invalidFromStore: 'The store reports that your purchase receipt is invalid. Please <a href="<%= quote(storeURL) %>">visit the store to reinstall the application</a>.',
receiptFormatError: 'Your purchase receipt is malformed. Please <a href="<%= quote(storeURL) %>">visit the store to reinstall the application</a>.',
- genericError: 'An error has occurred. <a href="<%= quote(storeURL) %>">Reinstalling the application</a> may fix this problem. If not please contact support: <%= supportHTML %>'
+ genericError: 'An error has occurred. <a href="<%= quote(storeURL) %>">Reinstalling the application</a> may fix this problem. If not please contact support: <%= supportHTML %>',
+ mozAppsNotSupported: 'This browser or device does not support the Marketplace Apps system.'
},
respond: function (verifier) {
@@ -742,6 +749,10 @@ Prompter.prototype = {
verifier.state instanceof verifier.states.NetworkError) {
return;
}
+ if (verifier.state instanceof verifier.states.MozAppsNotSupported) {
+ this.handleMozAppsNotSupported(verifier);
+ return;
+ }
if (verifier.state instanceof verifier.states.InternalError) {
if (this.ignoreInternalError) {
return;
@@ -764,13 +775,19 @@ Prompter.prototype = {
}
});
this.handleReceiptError(verifier, bestReason);
+ return;
}
if (window.console && console.log) {
console.log('Unexpected state: ' + verifier.state);
}
throw 'Unexpected state in verifier: ' + verifier.state;
},
+ handleMozAppsNotSupported: function (verifier) {
+ var blocking = ! this.allowNoInstall;
+ this.display(this.render(this.templates.mozAppsNotSupported), blocking);
+ },
+
handleInternalError: function (verifier) {
if (this.fatalInternalError) {
this.display(this.render(this.templates.fatalInternalError), true);
View
@@ -83,6 +83,7 @@ <h1>mozmarket.receipts.Prompter UI</h1>
<button type="button" onclick="runTest(new states.NoValidReceipts(), new errors.InvalidReceiptIssuer('Bad issuer', {iss: 'https://store.example.com'}))">InvalidReceiptIssuer</button>
<button type="button" onclick="runTest(new states.NoValidReceipts(), new errors.ReceiptFormatError('something wrong'))">ReceiptFormatError</button>
<button type="button" onclick="runTest(new states.NoValidReceipts(), new errors.ReceiptParseError('something more wrong'))">ReceiptParseError</button>
+<button type="button" onclick="runTest(new states.MozAppsNotSupported())">mozApps not supported</button>
<br>
<label for=allowNoInstall>allowNoInstall: <input type=checkbox id=allowNoInstall></label>
View
@@ -267,6 +267,18 @@ <h2>Let's test caching!</h2>
<div class="test"><pre class="doctest">
+$ oldMozApps = navigator.mozApps;
+$ navigator.mozApps = null;
+$ verifier = new Verifier(owaArgs);
+$ verifier.verify(Spy('verify', {wait: true}));
+verify([Verifier state: [MozAppsNotSupported navigator.mozApps does not exist]])
+$ navigator.mozApps = oldMozApps;
+...
+
+</pre></div>
+
+<div class="test"><pre class="doctest">
+
$ writeln(Verifier.states);
{...}
$ writeln(Verifier.errors);

0 comments on commit b75f60b

Please sign in to comment.