Home | History | Annotate | Download | only in sync_notifier
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // This class represents the data for a single Synced Notification.
      6 // It should map 1-1 to all the data in synced_notification_sepcifics.proto,
      7 // and the data and render protobufs that the specifics protobuf contains.
      8 
      9 #ifndef CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_SYNCED_NOTIFICATION_H_
     10 #define CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_SYNCED_NOTIFICATION_H_
     11 
     12 #include <string>
     13 
     14 #include "base/gtest_prod_util.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/memory/scoped_vector.h"
     17 #include "chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher.h"
     18 #include "sync/api/sync_data.h"
     19 #include "sync/protocol/sync.pb.h"
     20 #include "ui/gfx/image/image.h"
     21 #include "url/gurl.h"
     22 
     23 namespace sync_pb {
     24 class SyncedNotificationSpecifics;
     25 }
     26 
     27 class NotificationUIManager;
     28 class Profile;
     29 
     30 namespace notifier {
     31 
     32 class ChromeNotifierService;
     33 
     34 class SyncedNotification : public NotificationBitmapFetcherDelegate {
     35  public:
     36   explicit SyncedNotification(const syncer::SyncData& sync_data);
     37 
     38   virtual ~SyncedNotification();
     39 
     40   enum ReadState {
     41     kUnread = 1,
     42     kRead = 2,
     43     kDismissed = 3,
     44   };
     45 
     46   static const int kUndefinedPriority = 65535;
     47 
     48   void Update(const syncer::SyncData& sync_data);
     49 
     50   // Here are some helper functions to get individual data parts out of a
     51   // SyncedNotification.
     52   std::string GetTitle() const;
     53   std::string GetHeading() const;
     54   std::string GetDescription() const;
     55   std::string GetAnnotation() const;
     56   std::string GetAppId() const;
     57   std::string GetKey() const;
     58   GURL GetOriginUrl() const;
     59   GURL GetAppIconUrl() const;
     60   GURL GetImageUrl() const;
     61   std::string GetText() const;
     62   ReadState GetReadState() const;
     63   uint64 GetCreationTime() const;
     64   int GetPriority() const;
     65   std::string GetDefaultDestinationTitle() const;
     66   GURL GetDefaultDestinationIconUrl() const;
     67   GURL GetDefaultDestinationUrl() const;
     68   std::string GetButtonTitle(unsigned int which_button) const;
     69   GURL GetButtonIconUrl(unsigned int which_button) const;
     70   GURL GetButtonUrl(unsigned int which_button) const;
     71   GURL GetProfilePictureUrl(unsigned int which_url) const;
     72   size_t GetProfilePictureCount() const;
     73   size_t GetNotificationCount() const;
     74   size_t GetButtonCount() const;
     75   std::string GetContainedNotificationTitle(int index) const;
     76   std::string GetContainedNotificationMessage(int index) const;
     77   std::string GetSendingServiceId() const;
     78 
     79 
     80   bool EqualsIgnoringReadState(const SyncedNotification& other) const;
     81 
     82   void NotificationHasBeenRead();
     83   void NotificationHasBeenDismissed();
     84 
     85   // Fill up the queue of bitmaps to fetch.
     86   void QueueBitmapFetchJobs(NotificationUIManager* notification_manager,
     87                             ChromeNotifierService* notifier_service,
     88                             Profile* profile);
     89 
     90   // Start the bitmap fetching.  When it is complete, the callback
     91   // will call Show().
     92   void StartBitmapFetch();
     93 
     94   // Display the notification in the notification center
     95   void Show(NotificationUIManager* notification_manager,
     96             ChromeNotifierService* notifier_service,
     97             Profile* profile);
     98 
     99   // This gets a pointer to the SyncedNotificationSpecifics part
    100   // of the sync data.
    101   sync_pb::EntitySpecifics GetEntitySpecifics() const;
    102 
    103   // Write a notification to the console log.
    104   void LogNotification();
    105 
    106  private:
    107   // Helper function to mark a notification as read or dismissed.
    108   void SetReadState(const ReadState& read_state);
    109 
    110   // Method inherited from NotificationBitmapFetcher delegate.
    111   virtual void OnFetchComplete(const GURL url, const SkBitmap* bitmap) OVERRIDE;
    112 
    113   // If this bitmap has a valid GURL, create a fetcher for it.
    114   void AddBitmapToFetchQueue(const GURL& gurl);
    115 
    116   sync_pb::SyncedNotificationSpecifics specifics_;
    117   NotificationUIManager* notification_manager_;
    118   ChromeNotifierService* notifier_service_;
    119   Profile* profile_;
    120   ScopedVector<NotificationBitmapFetcher> fetchers_;
    121   int active_fetcher_count_;
    122   gfx::Image app_icon_bitmap_;
    123   gfx::Image sender_bitmap_;
    124   gfx::Image image_bitmap_;
    125   std::vector<gfx::Image> button_bitmaps_;
    126 
    127   FRIEND_TEST_ALL_PREFIXES(SyncedNotificationTest, AddBitmapToFetchQueueTest);
    128   FRIEND_TEST_ALL_PREFIXES(SyncedNotificationTest, OnFetchCompleteTest);
    129   FRIEND_TEST_ALL_PREFIXES(SyncedNotificationTest, QueueBitmapFetchJobsTest);
    130 
    131   DISALLOW_COPY_AND_ASSIGN(SyncedNotification);
    132 };
    133 
    134 }  // namespace notifier
    135 
    136 #endif  // CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_SYNCED_NOTIFICATION_H_
    137