Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Unified Diff: chrome/browser/notifier/synced_notification.cc

Issue 11745024: Synced Notification Sync Change Processor (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Synced Notifications Change Processor - CR Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/notifier/synced_notification.cc
diff --git a/chrome/browser/notifier/synced_notification.cc b/chrome/browser/notifier/synced_notification.cc
new file mode 100644
index 0000000000000000000000000000000000000000..92e0434fc67a651e94f8ebe08df0ee6749af0d4f
--- /dev/null
+++ b/chrome/browser/notifier/synced_notification.cc
@@ -0,0 +1,269 @@
+// 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/notifier/synced_notification.h"
+
+#include "sync/protocol/sync.pb.h"
+#include "sync/protocol/synced_notification_specifics.pb.h"
+
+namespace {
+const char kExtensionScheme[] = "chrome-extension://";
+} // namespace
+
+namespace notifier {
+
+// An important side effect of the constructor is that it keeps the
+// SyncNotificationSpecifics alive by putting it into the contained sync entity.
+// At construction time, copy the data we might need from the sync_data
+// protobuf object.
+SyncedNotification::SyncedNotification(const syncer::SyncData& sync_data)
+ : sync_data_(sync_data), has_local_changes_(false) {
+ title_ = GetTitle(sync_data);
+ app_id_ = GetAppId(sync_data);
+ coalescing_key_ = GetCoalescingKey(sync_data);
+ first_external_id_ = GetFirstExternalId(sync_data);
+ notification_id_ = GetNotificationId(sync_data);
+ body_ = GetBody(sync_data);
+ origin_url_ = GetOriginUrl(sync_data);
+ icon_url_ = GetIconUrl(sync_data);
+ image_url_ = GetImageUrl(sync_data);
dcheng 2013/01/25 22:23:48 Use initializer format for these? Then you can con
Pete Williamson 2013/01/26 02:17:09 Done.
+}
+
+SyncedNotification::~SyncedNotification() {}
+
+// TODO(petewil): Consider the timestamp too once it gets added to the protobuf.
+bool SyncedNotification::Equals(const SyncedNotification& other) const {
+ // Two notifications are equal if the <appId/coalescingKey> pair matches.
+ return (notification_id() == other.notification_id());
+}
+
+// Set the read state on the notification, returns true for success.
+bool SyncedNotification::SetReadState(
+ sync_pb::CoalescedSyncedNotification_ReadState readState) {
+
+ // TODO(petewil): implement
+ return true;
+}
+
+// Mark this notification as having been read locally.
+void SyncedNotification::NotificationHasBeenRead() {
+ // We set the is_local_ flag to true since we modified it locally
+ // then we create a sync change object to pass back up.
+ has_local_changes_ = true;
+
+ bool success = SetReadState(
+ sync_pb::CoalescedSyncedNotification_ReadState_READ);
+ DCHECK(success);
+}
+
+// mark this notification as having been dismissed locally
+void SyncedNotification::NotificationHasBeenDeleted() {
+ // We set the is_deleted_ flag to true since we modified it locally
+ // then we create a sync change object to pass back up.
+ has_local_changes_ = true;
+
+ bool success = SetReadState(
+ sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED);
+ DCHECK(success);
+}
+
+// TODO(petewil): Consider whether the repeated code below can be re-used.
+// A first attempt to do so failed.
+
+const sync_pb::SyncedNotificationSpecifics*
+SyncedNotification::GetSyncedNotificationSpecifics() {
+ return &(sync_data_.GetSpecifics().synced_notification());
+}
+
+std::string SyncedNotification::GetFirstExternalId(
+ const syncer::SyncData& sync_data) const {
+ if (!sync_data.GetSpecifics().synced_notification().
+ has_coalesced_notification())
+ return "";
+ if (sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().notification_size() < 1)
+ return "";
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().notification(0).has_external_id())
+ return "";
+
+ return sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().notification(0).external_id();
+}
+
+std::string SyncedNotification::GetTitle(
+ const syncer::SyncData& sync_data) const {
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().has_layout_type())
+ return "";
+
+ const sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType layout_type =
+ sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().layout_type();
+
+ // Depending on the layout type, get the proper title.
+ switch (layout_type) {
+ case sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT: {
+ // If we have title and subtext, get that title.
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().
+ title_and_subtext_data().has_title())
+ return "";
+
+ return sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().
+ title_and_subtext_data().title();
+ }
+
+ case sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_IMAGE: {
+ // If we have title and image, get that title.
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().
+ title_and_image_data().has_title())
+ return "";
+
+ return sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().
+ title_and_image_data().title();
+ }
+ default: {
+ // This is an error case, we should never get here unless the protobuf
+ // is bad, or a new type is introduced and this code does not get updated.
+ DCHECK(false);
dcheng 2013/01/25 22:23:48 NOTREACHED() is more canonical.
Pete Williamson 2013/01/26 02:17:09 Done.
+ return "";
+ }
+ }
+}
+
+std::string SyncedNotification::GetAppId(
+ const syncer::SyncData& sync_data) const {
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().id().
+ has_app_id())
+ return "";
+ return sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().id().app_id();
+}
+
+std::string SyncedNotification::GetCoalescingKey(
+ const syncer::SyncData& sync_data) const {
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().id().
+ has_coalescing_key())
+ return "";
+ return sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().id().coalescing_key();
+}
+
+GURL SyncedNotification::GetOriginUrl(
+ const syncer::SyncData& sync_data) const {
+ std::string origin_url(kExtensionScheme);
+ origin_url += app_id_;
+ return GURL(origin_url);
+}
+
+GURL SyncedNotification::GetIconUrl(const syncer::SyncData& sync_data) const {
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().has_layout_type())
+ return GURL();
+
+ const sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType layout_type =
+ sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().layout_type();
+
+ // Depending on the layout type, get the icon.
+ if (sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT
+ == layout_type) {
+
+ // If we have title and subtext, get that icon.
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().
+ title_and_subtext_data().icon().has_url())
+ return GURL();
+
+ return GURL(sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().
+ title_and_subtext_data().icon().url());
+
+ }
+ return GURL();
+}
+
+GURL SyncedNotification::GetImageUrl(const syncer::SyncData& sync_data) const {
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().has_layout_type())
+ return GURL();
+
+ const sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType layout_type =
+ sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().layout_type();
+
+ // Depending on the layout type, get the image.
+ if (sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_IMAGE
+ == layout_type) {
+
+ // If we have title and subtext, get that image.
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().
+ title_and_image_data().image().has_url())
+ return GURL();
+
+ return GURL(sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().
+ title_and_image_data().image().url());
+
+ }
+ return GURL();
+}
+
+std::string SyncedNotification::GetBody(
+ const syncer::SyncData& sync_data) const {
+ // If we have subtext data, concatenate the text lines and return it.
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().has_layout_type())
+ return "";
+
+ const sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType layout_type =
+ sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().layout_type();
+
+ // Check if this layout type includes body text.
+ if (sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT
+ == layout_type) {
+
+ // If we have title and subtext, get the text.
+ if (!sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().
+ has_title_and_subtext_data())
+ return "";
+ int subtext_lines = sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().
+ title_and_subtext_data().subtext_size();
+ if (subtext_lines < 1)
+ return "";
+
+ std::string subtext;
+ for (int ii = 0; ii < subtext_lines; ++ii) {
+ subtext += sync_data.GetSpecifics().synced_notification().
+ coalesced_notification().render_info().layout().
+ title_and_subtext_data().subtext(ii);
+ if ( ii < subtext_lines - 1)
+ subtext += '\n';
+ }
+ return subtext;
+ }
+ return "";
+}
+
+std::string SyncedNotification::GetNotificationId(
+ const syncer::SyncData& sync_data) const {
+ // Append the coalescing key to the app id to get the unique id.
+ std::string id = app_id_;
+ id += "/";
+ id += coalescing_key_;
+
+ return id;
+}
+
+} // namespace notifier

Powered by Google App Engine
This is Rietveld 408576698