forked from yogeshmundada/appu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.js
More file actions
145 lines (123 loc) · 4.91 KB
/
Copy pathmisc.js
File metadata and controls
145 lines (123 loc) · 4.91 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//Function to see if Appu server is up
//Also tells the server that this appu installation is still running
function pii_check_if_stats_server_up() {
try {
var wr = {};
wr.guid = (sign_in_status == 'signed-in') ? pii_vault.guid : '';
wr.version = pii_vault.config.current_version;
wr.deviceid = (sign_in_status == 'signed-in') ? pii_vault.config.deviceid : 'Not-reported';
$.post(server_url, JSON.stringify(wr),
function(data, textStatus, jqxhr) {
var is_up = false;
stats_message = /Hey ((?:[0-9]{1,3}\.){3}[0-9]{1,3}), Appu Stats Server is UP!/;
is_up = (stats_message.exec(data) != null);
console.log("Appu stats server, is_up? : "+ is_up);
})
.error(function (data, status) {
console.log("Appu: Could not check if server is up: " + server_url
+ ", status: " + status.toString());
print_appu_error("Appu Error: Seems like server was down. " +
"Status: " + status.toString() + " "
+ (new Date()));
});
}
catch (e) {
console.log("Error while checking if stats server is up");
}
last_server_contact = new Date();
}
function pii_modify_status(message) {
if (message.status == "enable") {
clearInterval(pii_vault.config.enable_timer);
pii_vault.config.status = "active";
pii_vault.config.disable_period = -1;
flush_selective_entries("config", ["enable_timer", "status", "disable_period"]);
chrome.browserAction.setIcon({path:'images/appu_new19.png'});
chrome.tabs.query({}, function(all_tabs) {
for(var i = 0; i < all_tabs.length; i++) {
chrome.tabs.sendMessage(all_tabs[i].id, {
type: "status-enabled",
show_monitor_icon: pii_vault.options.monitor_icon_setting});
}
});
}
else if (message.status == "disable") {
pii_vault.config.status = "disabled";
pii_vault.config.disable_period = message.minutes;
pii_vault.config.disable_start = (new Date()).toString();
pii_vault.config.enable_timer = setInterval(start_time_loop, 1000);
flush_selective_entries("config", ["disable_start", "enable_timer", "status", "disable_period"]);
pii_vault.current_report.appu_disabled.push(message.minutes);
flush_selective_entries("current_report", ["appu_disabled"]);
chrome.browserAction.setIcon({path:'images/appu_new19_offline.png'});
console.log((new Date()) + ": Disabling Appu for " + message.minutes + " minutes");
chrome.tabs.query({}, function(all_tabs) {
for(var i = 0; i < all_tabs.length; i++) {
chrome.tabs.sendMessage(all_tabs[i].id, {type: "status-disabled"});
}
});
}
}
function background_tasks() {
//report = pii_vault.past_reports[report_number - 2];
for (var i = 0; i < pii_vault.past_reports.length; i++) {
var cr = pii_vault.past_reports[i];
if (cr.actual_report_send_time == 'Not delivered yet') {
//To adjust for current_report(=1) and start index (0 instead of 1)
var report_number = i + 2;
console.log("APPU DEBUG: Report " + cr.reportid + " is undelivered");
if (report_number in delivery_attempts) {
var dat = delivery_attempts[report_number];
var curr_time = new Date();
var td = curr_time.getTime() - dat.getTime();
if (td < (60 * 60 * 24 * 1000)) {
//Less than 24-hours, Skip
// console.log("APPU DEBUG: Report " + report_number +
// " was already attempted to " +
// "be delivered, so skipping");
continue;
}
}
delivery_attempts[report_number] = new Date();
// console.log("APPU DEBUG: Attempting to send report " + report_number);
pii_send_report(report_number);
}
}
//If its been 24 hours, since we talked to server, just send a quick "I am alive"
//message
var curr_time = new Date();
var td = curr_time.getTime() - last_server_contact.getTime();
if (td > (60 * 60 * 24 * 1000)) {
pii_check_if_stats_server_up();
}
}
function start_time_loop() {
var curr_time = new Date();
if ((curr_time - (new Date(pii_vault.config.disable_start))) >
(60 * 1000 * pii_vault.config.disable_period)) {
clearInterval(pii_vault.config.enable_timer);
pii_vault.config.status = "active";
pii_vault.config.disable_period = -1;
flush_selective_entries("config", ["enable_timer", "status", "disable_period"]);
chrome.browserAction.setIcon({path:'images/appu_new19.png'});
console.log((new Date()) + ": Enabling Appu");
chrome.tabs.query({}, function(all_tabs) {
for(var i = 0; i < all_tabs.length; i++) {
chrome.tabs.sendMessage(all_tabs[i].id, {
type: "status-enabled",
show_monitor_icon: pii_vault.options.monitor_icon_setting
});
}
});
}
}
function shorten_reporting_time() {
update_reporting_interval(60, 1);
pii_vault.current_report.scheduled_report_time = new Date();
flush_current_report();
}
function restore_reporting_time() {
//Random time between 5 pm to 8 pm. Do we need to adjust according to local time?
var rand_minutes = 1020 + Math.floor(Math.random() * 1000)%180;
update_reporting_interval(4320, rand_minutes);
}