What's new? | Help | Directory | Sign in
Google
             
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2008, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifdef OFFICIAL_BUILD
// The notification API has not been finalized for official builds.
#else
#include "gears/notifier/notifier.h"

#include "gears/base/common/security_model.h"
#include "gears/base/common/serialization.h"
#include "gears/base/common/string_utils.h"
#include "gears/base/common/timed_call.h"
#include "gears/notifier/balloons.h"
#include "gears/notifier/const_notifier.h"
#include "gears/notifier/notification.h"
#include "gears/notifier/notification_manager.h"
#include "gears/notifier/notifier_process.h"
#include "gears/notifier/user_activity.h"
#if USING_CCTESTS
#include "gears/notifier/unit_test.h"
#endif // USING_CCTESTS
#include "third_party/glint/include/platform.h"
#include "third_party/glint/include/work_item.h"

// Constants.
const int kInactivityToShutdownIntervalMs = 30 * 60 * 1000; // 30m

// Posted as workitem from IPC worker thread to main UI thread.
class NotificationTask : public glint::WorkItem {
public:
enum Action { ACTION_ADD, ACTION_DELETE };
NotificationTask(Notifier *notifier,
const GearsNotification &notification,
Action action)
: action_(action),
notifier_(notifier) {
assert(notifier);
notification_.CopyFrom(notification);
}

void Post() {
glint::platform()->PostWorkItem(NULL, this);
}

virtual void Run() {
if (action_ == ACTION_ADD) {
notifier_->AddNotification(notification_);
} else if (action_ == ACTION_DELETE) {
notifier_->RemoveNotification(notification_.security_origin(),
notification_.id());
} else {
assert(false);
}
}
private:
Action action_;
Notifier *notifier_;
GearsNotification notification_;
};

Notifier::Notifier()
: running_(false),
to_restart_(false) {
}

Notifier::~Notifier() {
}

bool Notifier::Initialize() {
user_activity_monitor_.reset(UserActivityMonitor::Create());
user_activity_monitor_->AddObserver(this);
notification_manager_.reset(
new NotificationManager(user_activity_monitor_.get(), this, this));

GearsNotification::RegisterAsSerializable();
IpcMessageQueue *ipc_message_queue = IpcMessageQueue::GetSystemQueue();
if (!ipc_message_queue) {
return false;
}
ipc_message_queue->RegisterHandler(kDesktop_AddNotification, this);
ipc_message_queue->RegisterHandler(kDesktop_RemoveNotification, this);

if (!RegisterProcess()) {
return false;
}

// Load saved notifications if needed.
if (!notification_manager_->LoadNotifications()) {
LOG(("Saved notifications not loaded (most likely they didn't exist).\n"));
}

running_ = true;

return true;
}

void Notifier::Terminate() {
// Restart the instance if needed.
if (to_restart_) {
NotifierProcess::StartProcess(kRestartCmdLineSwitch, NULL, true);
}
}

void Notifier::Restart() {
// Protect from multiple restart.
if (to_restart_) {
return;
}

// Unregister notifier process so that we will not receive messages.
UnregisterProcess();

// Save the notifiations.
notification_manager_->SaveNotifications();

to_restart_ = true;
RequestQuit();
}

// This call comes on a worker thread that services the inter-process
// communication mechanism. So we need to make copies of all receipts and
// ship over by FedEx.
void Notifier::HandleIpcMessage(IpcProcessId source_process_id,
int message_type,
const IpcMessageData *message_data) {
switch (message_type) {
case kDesktop_AddNotification: {
const GearsNotification *notification =
static_cast<const GearsNotification*>(message_data);
(new NotificationTask(this,
*notification,
NotificationTask::ACTION_ADD))->Post();
break;
}

case kDesktop_RemoveNotification: {
const GearsNotification *notification =
static_cast<const GearsNotification*>(message_data);
(new NotificationTask(this,
*notification,
NotificationTask::ACTION_DELETE))->Post();
break;
}

case kDesktop_RestartNotifierImmediately:
Restart();
break;

default:
assert(false);
break;
}
}

bool Notifier::IsRestartNeeded() const {
return false;
}

bool Notifier::CanShutdownOnLackOfActivity() const {
// We will shutdown the notifier on lack of activity that is defined as
// the following:
// 1) No notification is showing or pending to show.
// 2) The user is idle or away.
// 3) Condition 1) and 2) have been met for a period of time, i.e. 30 minutes.
// (This condition is enforced by shutdown_on_lack_of_activity_timer_.)
return notification_manager_.get() &&
!notification_manager_->showing_notifications() &&
!notification_manager_->has_pending_notifications() &&
user_activity_monitor_.get() &&
(user_activity_monitor_->user_mode() == USER_IDLE_MODE ||
user_activity_monitor_->user_mode() == USER_AWAY_MODE);
}

void Notifier::CheckShutdownOnLackOfActivity() {
if (CanShutdownOnLackOfActivity()) {
// Since condition 1 and 2 for shutdown have been met, start a timer to see
// if they are still met after a period of time (condition 3).
if (!shutdown_on_lack_of_activity_timer_.get()) {
shutdown_on_lack_of_activity_timer_.reset(
new TimedCall(kInactivityToShutdownIntervalMs,
false,
&Notifier::ShutdownOnLackOfActivity,
this));
}
} else {
shutdown_on_lack_of_activity_timer_.reset();
}
}

void Notifier::OnBalloonSpaceChanged() {
CheckShutdownOnLackOfActivity();
}

void Notifier::OnUserActivityChange() {
CheckShutdownOnLackOfActivity();
}

void Notifier::ShutdownOnLackOfActivity(void *arg) {
assert(arg);

Notifier *this_ptr = reinterpret_cast<Notifier*>(arg);

this_ptr->shutdown_on_lack_of_activity_timer_.reset();
if (this_ptr->CanShutdownOnLackOfActivity()) {
LOG(("Shut down notifier on lack of activity\n"));
this_ptr->UnregisterProcess();
this_ptr->RequestQuit();
}
}

void Notifier::AddNotification(const GearsNotification &notification) {
LOG(("Add notification %S - %S ($S)\n",
notification.security_origin().url().c_str(),
notification.id().c_str(),
notification.title().c_str()));
notification_manager_->Add(notification);

// Run the check so that it sees there has been activity.
CheckShutdownOnLackOfActivity();
}

void Notifier::RemoveNotification(const SecurityOrigin &security_origin,
const std::string16 &id) {
LOG(("Remove notification %S - %S\n", security_origin.url().c_str(),
id.c_str()));
notification_manager_->Delete(security_origin, id);

// Run the check so that it sees there has been activity.
CheckShutdownOnLackOfActivity();
}

#endif // OFFICIAL_BUILD
Show details Hide details

Change log

r2692 by gears.daemon on Aug 12, 2008   Diff
[Author: jianli]

Support shutting down notifier on lack of
activity.

PRESUBMIT=passed
R=levin
CC=gears-eng@googlegroups.com
DELTA=105  (98 added, 0 deleted, 7
changed)
OCL=7958964
SCL=7965607
Go to: 
Project members, sign in to write a code review

Older revisions

r2641 by gears.daemon on Aug 07, 2008   Diff
[Author: jianli]

Implement user activity detection for
Linux platform:
 * Detect input activity: including
...
r2597 by gears.daemon on Aug 05, 2008   Diff
[Author: dimich]

Reflect changes in Gllint (SVN
revision 48)
Add Autorelease pool to logging
...
r2540 by gears.daemon on Jul 30, 2008   Diff
[Author: jianli]

Hook up the code to monitor the full
screen presentation mode and show/hide
balloons accordingly.
...
All revisions of this file

File info

Size: 8445 bytes, 254 lines