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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// 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 <stdio.h>

#include "gears/notifier/notification.h"

#ifdef OS_MACOSX
#include <CoreFoundation/CoreFoundation.h>
#include "gears/base/common/string_utils_osx.h"
#endif
#if !BROWSER_NONE
#include "gears/base/common/js_runner.h"
#endif // !BROWSER_NONE
#include "gears/base/common/security_model.h"
#include "gears/base/common/string_utils.h"
#include "third_party/scoped_ptr/scoped_ptr.h"

#if !BROWSER_NONE
DECLARE_DISPATCHER(GearsNotification);

template<>
void Dispatcher<GearsNotification>::Init() {
RegisterMethod("addAction", &GearsNotification::AddAction);
RegisterProperty("description", &GearsNotification::GetDescription,
&GearsNotification::SetDescription);
RegisterProperty("displayAtTime", &GearsNotification::GetDisplayAtTime,
&GearsNotification::SetDisplayAtTime);
RegisterProperty("displayUntilTime", &GearsNotification::GetDisplayUntilTime,
&GearsNotification::SetDisplayUntilTime);
RegisterProperty("icon", &GearsNotification::GetIcon,
&GearsNotification::SetIcon);
RegisterProperty("id", &GearsNotification::GetId,
&GearsNotification::SetId);
RegisterProperty("subtitle", &GearsNotification::GetSubtitle,
&GearsNotification::SetSubtitle);
RegisterProperty("title", &GearsNotification::GetTitle,
&GearsNotification::SetTitle);
}

const std::string GearsNotification::kModuleName("Notification");
#endif // !BROWSER_NONE

std::string16 GenerateGuid() {
std::string16 wide_guid_str;
#ifdef WIN32
GUID guid = {0};
HRESULT hr = ::CoCreateGuid(&guid);
assert(SUCCEEDED(hr));
char16 wide_guid_buf[40] = {0};
int num = ::StringFromGUID2(guid,
reinterpret_cast<LPOLESTR>(wide_guid_buf),
ARRAYSIZE(wide_guid_buf));
assert(num);
wide_guid_str = wide_guid_buf + 1; // remove opening bracket
wide_guid_str.erase(wide_guid_str.length() - 1); // remove closing bracket
#elif defined(OS_ANDROID) || (defined(LINUX) && !defined(OS_MACOSX))
FILE *fptr = fopen("/proc/sys/kernel/random/uuid", "r");
assert(fptr);
if (fptr) {
char guid_str[37] = {0};
fgets(guid_str, sizeof(guid_str) - 1, fptr);
fclose(fptr);
UTF8ToString16(guid_str, &wide_guid_str);
}
#elif defined(OS_MACOSX)
CFUUIDRef guid = CFUUIDCreate(NULL);
CFStringRef guid_cfstr = CFUUIDCreateString(NULL, guid);
CFStringRefToString16(guid_cfstr, &wide_guid_str);
CFRelease(guid);
CFRelease(guid_cfstr);
#else
#error "Unsupported platform."
#endif // WIN32
return wide_guid_str;
}

GearsNotification::GearsNotification()
:
#if !BROWSER_NONE
ModuleImplBaseClass(kModuleName),
#endif
version_(kNotificationVersion),
display_at_time_ms_(0),
display_until_time_ms_(0),
send_retries_(0) {
id_ = GenerateGuid();
}

void GearsNotification::CopyFrom(const GearsNotification& from) {
version_ = from.version_;
title_ = from.title_;
subtitle_ = from.subtitle_;
icon_url_ = from.icon_url_;
icon_raw_data_ = from.icon_raw_data_;
security_origin_.CopyFrom(from.security_origin_);
id_ = from.id_;
description_ = from.description_;
display_at_time_ms_ = from.display_at_time_ms_;
display_until_time_ms_ = from.display_until_time_ms_;
actions_ = from.actions_;

send_retries_ = from.send_retries_;
}

bool GearsNotification::Serialize(Serializer *out) const {
out->WriteInt(version_);
out->WriteString(title_.c_str());
out->WriteString(subtitle_.c_str());
out->WriteString(icon_url_.c_str());
out->WriteString(security_origin_.url().c_str());
out->WriteString(id_.c_str());
out->WriteString(description_.c_str());
out->WriteInt64(display_at_time_ms_);
out->WriteInt64(display_until_time_ms_);
out->WriteInt(static_cast<int>(actions_.size()));
for (size_t i = 0; i < actions_.size(); ++i) {
out->WriteString(actions_[i].text.c_str());
out->WriteString(actions_[i].url.c_str());
}
int icon_byte_count = std::max(static_cast<int>(icon_raw_data_.size()), 0);
out->WriteInt(icon_byte_count);
if (icon_byte_count) {
out->WriteBytes(&icon_raw_data_.at(0), icon_raw_data_.size());
}
return true;
}

bool GearsNotification::Deserialize(Deserializer *in) {
int action_size = 0;
std::string16 security_origin_url;
if (!in->ReadInt(&version_) ||
version_ != kNotificationVersion ||
!in->ReadString(&title_) ||
!in->ReadString(&subtitle_) ||
!in->ReadString(&icon_url_) ||
!in->ReadString(&security_origin_url) ||
!in->ReadString(&id_) ||
!in->ReadString(&description_) ||
!in->ReadInt64(&display_at_time_ms_) ||
!in->ReadInt64(&display_until_time_ms_) ||
!in->ReadInt(&action_size)) {
return false;
}
if (!security_origin_.InitFromUrl(security_origin_url.c_str())) {
return false;
}

for (int i = 0; i < action_size; ++i) {
NotificationAction action;
if (!in->ReadString(&action.text) || !in->ReadString(&action.url)) {
return false;
}
actions_.push_back(action);
}
int icon_byte_count = 0;
in->ReadInt(&icon_byte_count);
if (icon_byte_count != 0 && icon_byte_count != kNotificationIconByteSize) {
return false;
}
icon_raw_data_.resize(icon_byte_count);
if (icon_byte_count) {
in->ReadBytes(&icon_raw_data_.at(0), icon_raw_data_.size());
}
return true;
}

void GearsNotification::set_icon_raw_data(
const std::vector<uint8>& icon_raw_data) {
if (icon_raw_data.size() != static_cast<size_t>(kNotificationIconByteSize)) {
icon_raw_data_.clear();
return;
}
icon_raw_data_ = icon_raw_data;
}

#if !BROWSER_NONE
void GearsNotification::GetTitle(JsCallContext *context) {
context->SetReturnValue(JSPARAM_STRING16, &title_);
}

void GearsNotification::SetTitle(JsCallContext *context) {
std::string16 title;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_STRING16, &title },
};
context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set())
return;

title_ = title;
}

void GearsNotification::GetIcon(JsCallContext *context) {
context->SetReturnValue(JSPARAM_STRING16, &icon_url_);
}

void GearsNotification::SetIcon(JsCallContext *context) {
std::string16 icon;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_STRING16, &icon },
};
context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set())
return;

icon_url_ = icon;
}

void GearsNotification::GetId(JsCallContext *context) {
context->SetReturnValue(JSPARAM_STRING16, &id_);
}

void GearsNotification::SetId(JsCallContext *context) {
std::string16 id;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_STRING16, &id },
};
context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set())
return;

if (id.empty()) {
context->SetException(STRING16(L"id can not be empty string."));
return;
}
id_ = id;
}

void GearsNotification::GetSubtitle(JsCallContext *context) {
context->SetReturnValue(JSPARAM_STRING16, &subtitle_);
}

void GearsNotification::SetSubtitle(JsCallContext *context) {
std::string16 subtitle;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_STRING16, &subtitle },
};
context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set())
return;

subtitle_ = subtitle;
}

void GearsNotification::GetDescription(JsCallContext *context) {
context->SetReturnValue(JSPARAM_STRING16, &description_);
}

void GearsNotification::SetDescription(JsCallContext *context) {
std::string16 description;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_STRING16, &description },
};
context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set())
return;

description_ = description;
}

void GearsNotification::GetDisplayAtTime(JsCallContext *context) {
if (display_at_time_ms_) {
JsRunnerInterface *js_runner = GetJsRunner();
scoped_ptr<JsObject> obj(js_runner->NewDate(display_at_time_ms_));
context->SetReturnValue(JSPARAM_OBJECT, obj.get());
} else {
context->SetReturnValue(JSPARAM_NULL, 0);
}
}

void GearsNotification::SetDisplayAtTime(JsCallContext *context) {
JsObject obj;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_OBJECT, &obj },
};
context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set())
return;

JsRunnerInterface *js_runner = GetJsRunner();
if (!js_runner->ConvertJsObjectToDate(&obj, &display_at_time_ms_)) {
context->SetException(STRING16(L"failed to set value for displayAtTime."));
return;
}
}

void GearsNotification::GetDisplayUntilTime(JsCallContext *context) {
if (display_until_time_ms_) {
JsRunnerInterface *js_runner = GetJsRunner();
scoped_ptr<JsObject> obj(js_runner->NewDate(display_until_time_ms_));
context->SetReturnValue(JSPARAM_OBJECT, obj.get());
} else {
context->SetReturnValue(JSPARAM_NULL, 0);
}
}

void GearsNotification::SetDisplayUntilTime(JsCallContext *context) {
JsObject obj;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_OBJECT, &obj },
};
context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set())
return;

JsRunnerInterface *js_runner = GetJsRunner();
if (!js_runner->ConvertJsObjectToDate(&obj, &display_until_time_ms_)) {
context->SetException(
STRING16(L"failed to set value for displayUntilTime."));
return;
}
}

void GearsNotification::AddAction(JsCallContext *context) {
NotificationAction action;
JsArgument argv[] = {
{ JSPARAM_REQUIRED, JSPARAM_STRING16, &action.text },
{ JSPARAM_REQUIRED, JSPARAM_STRING16, &action.url },
};
context->GetArguments(ARRAYSIZE(argv), argv);
if (context->is_exception_set())
return;

if (action.text.empty()) {
context->SetException(STRING16(L"text can not be empty string."));
return;
}
if (action.url.empty()) {
context->SetException(STRING16(L"url can not be empty string."));
return;
}
actions_.push_back(action);
}

#endif // !BROWSER_NONE

#endif // OFFICIAL_BUILD
Show details Hide details

Change log

r2831 by gears.daemon on Sep 10, 2008   Diff
[Author: nigeltao]

Move CreateModule<T> out of
base/xx/module_wrapper.h and into
base/common/base_class.h, unifying the
three CreateModule<T>
implementations, and allowing us to delete
the
base/common/module_wrapper.h file.

Also, eliminate the DECLARE_GEARS_WRAPPER
macro, since it doesn't
...
Go to: 
Project members, sign in to write a code review

Older revisions

r2735 by gears.daemon on Aug 19, 2008   Diff
[Author: jianli]

Allow notifier proxy to resend the IPC
message when it failed to send it out
last time.
...
r2461 by gears.daemon on Jul 22, 2008   Diff
[Author: nigeltao]

Eliminate ModuleImplBaseClassVirtual,
since all Modules are
now Dispatcher-backed, on Firefox.
...
r2393 by gears.daemon on Jul 16, 2008   Diff
[Author: jianli]

Add const modifier to the
serialization methods.

...
All revisions of this file

File info

Size: 11689 bytes, 364 lines