Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / webapp / crd / js / mock_identity.js
blob7bdb446ed6ffd9123058b1267e938bcad4d9cf24
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 /**
6  * @fileoverview
7  * Mock implementation of chrome.identity.
8  */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 /**
16  * @constructor
17  */
18 remoting.MockIdentity = function() {
19   /**
20    * @type {remoting.MockIdentity.AccessToken}
21    * @private
22    */
23   this.accessToken_ = remoting.MockIdentity.AccessToken.NONE;
26 /**
27  * @param {Object} details
28  * @param {function(string)} callback
29  */
30 remoting.MockIdentity.prototype.getAuthToken = function(details, callback) {
31   window.setTimeout(callback.bind(null, this.accessToken_), 0);
34 /**
35  * @param {Object} details
36  * @param {function()} callback
37  */
38 remoting.MockIdentity.prototype.removeCachedAuthToken =
39     function(details, callback) {
40   window.setTimeout(callback, 0);
43 /**
44  * @param {Object} details
45  * @param {function()} callback
46  */
47 remoting.MockIdentity.prototype.launchWebAuthFlow =
48     function(details, callback) {
49   // TODO(jamiewalch): Work out how to test third-party auth.
50   console.error('No mock implementation for launchWebAuthFlow.');
54 /** @enum {string} */
55 remoting.MockIdentity.AccessToken = {
56   VALID: 'valid-token',
57   INVALID: 'invalid-token',
58   NONE: ''
61 /**
62  * @param {remoting.MockIdentity.AccessToken} accessToken
63  */
64 remoting.MockIdentity.prototype.setAccessToken = function(accessToken) {
65   this.accessToken_ = accessToken;
68 /**
69  * @param {string} token
70  * @param {Function} onDone
71  * @param {function(!remoting.Error)} onError
72  * @param {Array<*>} values
73  */
74 remoting.MockIdentity.validateTokenAndCall =
75     function(token, onDone, onError, values) {
76   if (token == remoting.MockIdentity.AccessToken.VALID) {
77     window.setTimeout(
78         onDone.apply.bind(onDone, null, values),
79         0);
80   } else {
81     window.setTimeout(
82         onError.bind(null, new remoting.Error(
83             remoting.Error.Tag.AUTHENTICATION_FAILED)),
84         0);
85   }
88 /**
89  * @param {Function} onDone
90  * @param {function(!remoting.Error)} onError
91  * @param {Array<*>} values
92  */
93 remoting.MockIdentity.prototype.validateTokenAndCall =
94     function(onDone, onError, values) {
95   remoting.MockIdentity.validateTokenAndCall(
96       this.accessToken_, onDone, onError, values);
99 /**
100  * @param {boolean} active
101  */
102 remoting.MockIdentity.setActive = function(active) {
103   chrome['identity'] =
104       active ? remoting.mockIdentity : remoting.savedIdentityApi;
107 /** @type {Object} */
108 remoting.savedIdentityApi = chrome.identity;
110 /** @type {remoting.MockIdentity} */
111 remoting.mockIdentity = new remoting.MockIdentity();