summaryrefslogtreecommitdiffstats
path: root/chrome/browser/notifications/sync_notifier/synced_notification.cc
blob: 99fc4ffc61f0f793b378c5bb9d8ef375183c71b5 (plain)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/notifications/sync_notifier/synced_notification.h"

#include "base/basictypes.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/browser/notifications/sync_notifier/chrome_notifier_delegate.h"
#include "sync/protocol/sync.pb.h"
#include "sync/protocol/synced_notification_specifics.pb.h"
#include "ui/gfx/image/image.h"
#include "ui/message_center/message_center_util.h"
#include "ui/message_center/notification_types.h"

namespace {
const char kExtensionScheme[] = "chrome-extension://";

bool UseRichNotifications() {
  return message_center::IsRichNotificationEnabled();
}

}  // namespace

namespace notifier {

COMPILE_ASSERT(static_cast<sync_pb::CoalescedSyncedNotification_ReadState>(
                   SyncedNotification::kUnread) ==
               sync_pb::CoalescedSyncedNotification_ReadState_UNREAD,
               local_enum_must_match_protobuf_enum);
COMPILE_ASSERT(static_cast<sync_pb::CoalescedSyncedNotification_ReadState>(
                   SyncedNotification::kRead) ==
               sync_pb::CoalescedSyncedNotification_ReadState_READ,
               local_enum_must_match_protobuf_enum);
COMPILE_ASSERT(static_cast<sync_pb::CoalescedSyncedNotification_ReadState>(
                   SyncedNotification::kDismissed) ==
               sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED,
               local_enum_must_match_protobuf_enum);

SyncedNotification::SyncedNotification(const syncer::SyncData& sync_data) {
  Update(sync_data);
}

SyncedNotification::~SyncedNotification() {}

void SyncedNotification::Update(const syncer::SyncData& sync_data) {
  // TODO(petewil): Let's add checking that the notification looks valid.
  specifics_.CopyFrom(sync_data.GetSpecifics().synced_notification());
}

sync_pb::EntitySpecifics SyncedNotification::GetEntitySpecifics() const {
  sync_pb::EntitySpecifics entity_specifics;
  entity_specifics.mutable_synced_notification()->CopyFrom(specifics_);
  return entity_specifics;
}

void SyncedNotification::Show(NotificationUIManager* notification_manager,
                              ChromeNotifierService* notifier_service,
                              Profile* profile) {
  // Set up the fields we need to send and create a Notification object.
  GURL image_url = GetImageUrl();
  string16 text = UTF8ToUTF16(GetText());
  string16 heading = UTF8ToUTF16(GetHeading());
  // TODO(petewil): Eventually put the display name of the sending service here.
  string16 display_source = UTF8ToUTF16(GetOriginUrl().spec());
  string16 replace_key = UTF8ToUTF16(GetKey());

  // The delegate will eventually catch calls that the notification
  // was read or deleted, and send the changes back to the server.
  scoped_refptr<NotificationDelegate> delegate =
      new ChromeNotifierDelegate(GetKey(), notifier_service);

  // TODO(petewil): For now, just punt on dismissed notifications until
  // I change the interface to let NotificationUIManager know the right way.
  if (SyncedNotification::kRead == GetReadState() ||
      SyncedNotification::kDismissed == GetReadState() ) {
    DVLOG(2) << "Dismissed notification arrived"
             << GetHeading() << " " << GetText();
    return;
  }

  // Some inputs and fields are only used if there is a notification center.
  if (UseRichNotifications()) {
    base::Time creation_time =
        base::Time::FromDoubleT(static_cast<double>(GetCreationTime()));
    int priority = GetPriority();
    int notification_count = GetNotificationCount();
    int button_count = GetButtonCount();
    // TODO(petewil): Refactor this for an arbitrary number of buttons.
    std::string button_one_title = GetButtonOneTitle();
    std::string button_two_title = GetButtonTwoTitle();

    // Deduce which notification template to use from the data.
    message_center::NotificationType notification_type =
        message_center::NOTIFICATION_TYPE_BASE_FORMAT;
    if (!image_url.is_empty()) {
      notification_type = message_center::NOTIFICATION_TYPE_IMAGE;
    } else if (notification_count > 1) {
      notification_type = message_center::NOTIFICATION_TYPE_MULTIPLE;
    } else if (button_count > 0) {
      notification_type = message_center::NOTIFICATION_TYPE_BASE_FORMAT;
    }

    // Fill the optional fields with the information we need to make a
    // notification.
    message_center::RichNotificationData rich_notification_data;
    rich_notification_data.timestamp = creation_time;
    if (priority != SyncedNotification::kUndefinedPriority)
      rich_notification_data.priority = priority;
    if (!button_one_title.empty()) {
      message_center::ButtonInfo button_info(UTF8ToUTF16(button_one_title));
      rich_notification_data.buttons.push_back(button_info);
      // TODO(petewil): Add a button icon here.
    }
    if (!button_two_title.empty()) {
      message_center::ButtonInfo button_info(UTF8ToUTF16(button_two_title));
      rich_notification_data.buttons.push_back(button_info);
      // TODO(petewil): Add a button icon here.
    }

    // Fill the individual notification fields for a multiple notification.
    if (notification_count > 1) {
      for (int ii = 0; ii < notification_count; ++ii) {
        message_center::NotificationItem item(
            UTF8ToUTF16(GetContainedNotificationTitle(ii)),
            UTF8ToUTF16(GetContainedNotificationMessage(ii)));
        rich_notification_data.items.push_back(item);
      }
    }

    // TODO(petewil): Add code here that sets the various notification images
    // that have been decoded.  Also, don't use the notification tray icon once
    // this is enabled, that could cause user confusion.
    Notification ui_notification(notification_type,
                                 GetOriginUrl(),
                                 heading,
                                 text,
                                 gfx::Image(),
                                 WebKit::WebTextDirectionDefault,
                                 display_source,
                                 replace_key,
                                 rich_notification_data,
                                 delegate.get());
    notification_manager->Add(ui_notification, profile);
  } else {

    Notification ui_notification(GetOriginUrl(),
                                 GetAppIconUrl(),
                                 heading,
                                 text,
                                 WebKit::WebTextDirectionDefault,
                                 display_source,
                                 replace_key,
                                 delegate.get());

    notification_manager->Add(ui_notification, profile);

  }

  DVLOG(1) << "Showing Synced Notification! " << heading << " " << text
           << " " << GetAppIconUrl() << " " << replace_key << " "
           << GetReadState();

  return;
}

// TODO(petewil): Consider the timestamp too once it gets added to the protobuf.
// TODO: add more fields in here
bool SyncedNotification::EqualsIgnoringReadState(
    const SyncedNotification& other) const {
  return (GetTitle() == other.GetTitle() &&
          GetAppId() == other.GetAppId() &&
          GetKey() == other.GetKey() &&
          GetText() == other.GetText() &&
          GetOriginUrl() == other.GetOriginUrl() &&
          GetAppIconUrl() == other.GetAppIconUrl() &&
          GetImageUrl() == other.GetImageUrl() );
}

// Set the read state on the notification, returns true for success.
void SyncedNotification::SetReadState(const ReadState& read_state) {

  // convert the read state to the protobuf type for read state
  if (kDismissed == read_state)
    specifics_.mutable_coalesced_notification()->set_read_state(
        sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED);
  else if (kUnread == read_state)
    specifics_.mutable_coalesced_notification()->set_read_state(
        sync_pb::CoalescedSyncedNotification_ReadState_UNREAD);
  else if (kRead == read_state)
    specifics_.mutable_coalesced_notification()->set_read_state(
        sync_pb::CoalescedSyncedNotification_ReadState_READ);
  else
    NOTREACHED();
}

void SyncedNotification::NotificationHasBeenDismissed() {
  SetReadState(kDismissed);
}

std::string SyncedNotification::GetTitle() const {
  if (!specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().has_title())
    return std::string();

  return specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().title();
}

std::string SyncedNotification::GetHeading() const {
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      simple_collapsed_layout().has_heading())
    return std::string();

  return specifics_.coalesced_notification().render_info().collapsed_info().
      simple_collapsed_layout().heading();
}

std::string SyncedNotification::GetDescription() const {
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      simple_collapsed_layout().has_description())
    return std::string();

  return specifics_.coalesced_notification().render_info().collapsed_info().
      simple_collapsed_layout().description();
}

std::string SyncedNotification::GetAppId() const {
  if (!specifics_.coalesced_notification().has_app_id())
    return std::string();
  return specifics_.coalesced_notification().app_id();
}

std::string SyncedNotification::GetKey() const {
  if (!specifics_.coalesced_notification().has_key())
    return std::string();
  return specifics_.coalesced_notification().key();
}

GURL SyncedNotification::GetOriginUrl() const {
  std::string origin_url(kExtensionScheme);
  origin_url += GetAppId();
  return GURL(origin_url);
}

// TODO(petewil): This only returns the first icon. We should make all the
// icons available.
GURL SyncedNotification::GetAppIconUrl() const {
  if (specifics_.coalesced_notification().render_info().expanded_info().
      collapsed_info_size() == 0)
    return GURL();

  if (!specifics_.coalesced_notification().render_info().expanded_info().
      collapsed_info(0).simple_collapsed_layout().has_app_icon())
    return GURL();

  return GURL(specifics_.coalesced_notification().render_info().
              expanded_info().collapsed_info(0).simple_collapsed_layout().
              app_icon().url());
}

// TODO(petewil): This currenly only handles the first image from the first
// collapsed item, someday return all images.
GURL SyncedNotification::GetImageUrl() const {
  if (specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().media_size() == 0)
    return GURL();

  if (!specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().media(0).image().has_url())
    return GURL();

  return GURL(specifics_.coalesced_notification().render_info().
              expanded_info().simple_expanded_layout().media(0).image().url());
}

std::string SyncedNotification::GetText() const {
  if (!specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().has_text())
    return std::string();

  return specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().text();
}

SyncedNotification::ReadState SyncedNotification::GetReadState() const {
  DCHECK(specifics_.coalesced_notification().has_read_state());

  sync_pb::CoalescedSyncedNotification_ReadState found_read_state =
      specifics_.coalesced_notification().read_state();

  if (found_read_state ==
      sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED) {
    return kDismissed;
  } else if (found_read_state ==
             sync_pb::CoalescedSyncedNotification_ReadState_UNREAD) {
    return kUnread;
  } else if (found_read_state ==
             sync_pb::CoalescedSyncedNotification_ReadState_READ) {
    return kRead;
  } else {
    NOTREACHED();
    return static_cast<SyncedNotification::ReadState>(found_read_state);
  }
}

// Time in milliseconds since the unix epoch, or 0 if not available.
uint64 SyncedNotification::GetCreationTime() const {
  if (!specifics_.coalesced_notification().has_creation_time_msec())
    return 0;

  return specifics_.coalesced_notification().creation_time_msec();
}

int SyncedNotification::GetPriority() const {
  if (!specifics_.coalesced_notification().has_priority())
    return kUndefinedPriority;
  int protobuf_priority = specifics_.coalesced_notification().priority();

  // Convert the prioroty to the scheme used by the notification center.
  if (protobuf_priority ==
      sync_pb::CoalescedSyncedNotification_Priority_LOW) {
    return message_center::LOW_PRIORITY;
  } else if (protobuf_priority ==
             sync_pb::CoalescedSyncedNotification_Priority_STANDARD) {
    return message_center::DEFAULT_PRIORITY;
  } else if (protobuf_priority ==
             sync_pb::CoalescedSyncedNotification_Priority_HIGH) {
    return message_center::HIGH_PRIORITY;
  } else {
    // Complain if this is a new priority we have not seen before.
    DCHECK(protobuf_priority <
           sync_pb::CoalescedSyncedNotification_Priority_LOW  ||
           sync_pb::CoalescedSyncedNotification_Priority_HIGH <
           protobuf_priority);
    return kUndefinedPriority;
  }
}

int SyncedNotification::GetNotificationCount() const {
  return specifics_.coalesced_notification().render_info().
      expanded_info().collapsed_info_size();
}

int SyncedNotification::GetButtonCount() const {
  return specifics_.coalesced_notification().render_info().collapsed_info().
      target_size();
}

std::string SyncedNotification::GetDefaultDestinationTitle() const {
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      default_destination().icon().has_alt_text()) {
    return std::string();
  }
  return specifics_.coalesced_notification().render_info().collapsed_info().
      default_destination().icon().alt_text();
}

std::string SyncedNotification::GetDefaultDestinationIconUrl() const {
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      default_destination().icon().has_url()) {
    return std::string();
  }
  return specifics_.coalesced_notification().render_info().collapsed_info().
      default_destination().icon().url();
}

std::string SyncedNotification::GetDefaultDestinationUrl() const {
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      default_destination().has_url()) {
    return std::string();
  }
  return specifics_.coalesced_notification().render_info().collapsed_info().
      default_destination().url();
}

std::string SyncedNotification::GetButtonOneTitle() const {
  // Must ensure that we have a target before trying to access it.
  if (GetButtonCount() < 1)
    return std::string();
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      target(0).action().icon().has_alt_text()) {
    return std::string();
  }
  return specifics_.coalesced_notification().render_info().collapsed_info().
      target(0).action().icon().alt_text();
}

std::string SyncedNotification::GetButtonOneIconUrl() const {
  // Must ensure that we have a target before trying to access it.
  if (GetButtonCount() < 1)
    return std::string();
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      target(0).action().icon().has_url()) {
    return std::string();
  }
  return specifics_.coalesced_notification().render_info().collapsed_info().
      target(0).action().icon().url();
}

std::string SyncedNotification::GetButtonOneUrl() const {
  // Must ensure that we have a target before trying to access it.
  if (GetButtonCount() < 1)
    return std::string();
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      target(0).action().has_url()) {
    return std::string();
  }
  return specifics_.coalesced_notification().render_info().collapsed_info().
      target(0).action().url();
}

std::string SyncedNotification::GetButtonTwoTitle() const {
  // Must ensure that we have a target before trying to access it.
  if (GetButtonCount() < 2)
    return std::string();
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      target(1).action().icon().has_alt_text()) {
    return std::string();
  }
  return specifics_.coalesced_notification().render_info().collapsed_info().
      target(1).action().icon().alt_text();
}

std::string SyncedNotification::GetButtonTwoIconUrl() const {
  // Must ensure that we have a target before trying to access it.
  if (GetButtonCount() < 2)
    return std::string();
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      target(1).action().icon().has_url()) {
    return std::string();
  }
  return specifics_.coalesced_notification().render_info().collapsed_info().
      target(1).action().icon().url();
}

std::string SyncedNotification::GetButtonTwoUrl() const {
  // Must ensure that we have a target before trying to access it.
  if (GetButtonCount() < 2)
    return std::string();
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      target(1).action().has_url()) {
    return std::string();
  }
  return specifics_.coalesced_notification().render_info().collapsed_info().
      target(1).action().url();
}

std::string SyncedNotification::GetContainedNotificationTitle(
    int index) const {
  if (specifics_.coalesced_notification().render_info().expanded_info().
      collapsed_info_size() < index + 1)
    return std::string();

  return specifics_.coalesced_notification().render_info().expanded_info().
      collapsed_info(index).simple_collapsed_layout().heading();
}

std::string SyncedNotification::GetContainedNotificationMessage(
    int index) const {
  if (specifics_.coalesced_notification().render_info().expanded_info().
      collapsed_info_size() < index + 1)
    return std::string();

  return specifics_.coalesced_notification().render_info().expanded_info().
      collapsed_info(index).simple_collapsed_layout().description();
}

}  // namespace notifier