forked from mohamedmansour/google-plus-extension-jsapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background_controller.js
87 lines (79 loc) · 2.45 KB
/
background_controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Manages a single instance of the entire application.
*
* @author Mohamed Mansour 2011 (http://mohamedmansour.com)
* @constructor
*/
BackgroundController = function() {
this.plus = new ContentScriptAPIBridge();
this.onExtensionLoaded();
};
/**
* @return the native Plus API. Goes past the content script bridge.
*/
BackgroundController.prototype.getAPI = function() {
return this.plus.plus;
};
/**
* Triggered when the extension just loaded. Should be the first thing
* that happens when chrome loads the extension.
*/
BackgroundController.prototype.onExtensionLoaded = function() {
var currVersion = chrome.app.getDetails().version;
var prevVersion = settings.version;
if (currVersion != prevVersion) {
// Check if we just installed this extension.
if (typeof prevVersion == 'undefined') {
this.onInstall();
} else {
this.onUpdate(prevVersion, currVersion);
}
settings.version = currVersion;
}
};
/**
* Triggered when the extension just installed.
*/
BackgroundController.prototype.onInstall = function() {
};
/**
* Triggered when the extension just uploaded to a new version. DB Migrations
* notifications, etc should go here.
*
* @param {string} previous The previous version.
* @param {string} current The new version updating to.
*/
BackgroundController.prototype.onUpdate = function(previous, current) {
};
/**
* Initialize the main Background Controller
*/
BackgroundController.prototype.init = function() {
chrome.extension.onRequest.addListener(this.onExternalRequest.bind(this));
};
/**
* Listen on requests coming from content scripts.
*
* @param {object} request The request object to match data.
* @param {object} sender The sender object to know what the source it.
* @param {Function} sendResponse The response callback.
*/
BackgroundController.prototype.onExternalRequest = function(request, sender, sendResponse) {
if (request.method == 'PlusAPI') { // API Bridge
this.plus.routeMessage(sendResponse, request.data)
}
else if (request.method == 'DataAPI') { // WebStorage
this.plus.routeMessage(sendResponse, request.data)
}
else if (request.method == 'PersistSetting') { // LocalStorage
settings[request.data.key] = request.data.value;
}
else if (request.method == 'GetSetting') { // LocalStorage
sendResponse({data: settings[request.data]});
}
else {
sendResponse({});
}
};
var backgroundController = new BackgroundController();
backgroundController.init();