Home | History | Annotate | Download | only in sync_notifier
      1 // Copyright 2014 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 #include "chrome/browser/notifications/sync_notifier/synced_notification_app_info.h"
      6 
      7 #include "chrome/browser/notifications/sync_notifier/synced_notification_app_info_service.h"
      8 #include "chrome/browser/profiles/profile.h"
      9 #include "sync/api/sync_data.h"
     10 #include "sync/protocol/synced_notification_app_info_specifics.pb.h"
     11 
     12 using chrome::ImageHolder;
     13 using chrome::ImageHolderDelegate;
     14 
     15 namespace notifier {
     16 
     17 SyncedNotificationAppInfo::SyncedNotificationAppInfo(
     18     Profile* const profile,
     19     const std::string& settings_display_name,
     20     SyncedNotificationAppInfoService* synced_notification_app_info_service)
     21     : profile_(profile),
     22       settings_display_name_(settings_display_name),
     23       synced_notification_app_info_service_(
     24           synced_notification_app_info_service) {}
     25 
     26 SyncedNotificationAppInfo::~SyncedNotificationAppInfo() {}
     27 
     28 GURL SyncedNotificationAppInfo::settings_icon_url() {
     29   if (settings_holder_ != NULL)
     30     return settings_holder_->low_dpi_url();
     31   else
     32     return GURL();
     33 }
     34 
     35 bool SyncedNotificationAppInfo::HasAppId(const std::string& app_id) {
     36   std::vector<std::string>::iterator it;
     37 
     38   for (it = app_ids_.begin(); it != app_ids_.end(); ++it) {
     39     if (app_id == *it)
     40       return true;
     41   }
     42 
     43   return false;
     44 }
     45 
     46 void SyncedNotificationAppInfo::AddAppId(const std::string& app_id) {
     47   if (HasAppId(app_id))
     48     return;
     49 
     50   app_ids_.push_back(app_id);
     51 }
     52 
     53 void SyncedNotificationAppInfo::RemoveAppId(const std::string& app_id) {
     54   std::vector<std::string>::iterator it;
     55 
     56   for (it = app_ids_.begin(); it != app_ids_.end(); ++it) {
     57     if (app_id == *it) {
     58       app_ids_.erase(it);
     59       return;
     60     }
     61   }
     62 }
     63 
     64 void SyncedNotificationAppInfo::SetWelcomeLinkUrl(
     65     const GURL& welcome_link_url) {
     66   welcome_link_url_ = welcome_link_url;
     67 }
     68 
     69 void SyncedNotificationAppInfo::SetSettingsURLs(
     70     const GURL& settings_low_dpi, const GURL& settings_high_dpi) {
     71   settings_holder_.reset(new ImageHolder(settings_low_dpi,
     72                                          settings_high_dpi,
     73                                          profile_,
     74                                          this));
     75 }
     76 
     77 void SyncedNotificationAppInfo::SetMonochromeURLs(
     78     const GURL& monochrome_low_dpi, const GURL& monochrome_high_dpi) {
     79   monochrome_holder_.reset(new ImageHolder(monochrome_low_dpi,
     80                                            monochrome_high_dpi,
     81                                            profile_,
     82                                            this));
     83 }
     84 
     85 void SyncedNotificationAppInfo::SetWelcomeURLs(
     86     const GURL& welcome_low_dpi, const GURL& welcome_high_dpi) {
     87   welcome_holder_.reset(new ImageHolder(welcome_low_dpi,
     88                                         welcome_high_dpi,
     89                                         profile_,
     90                                         this));
     91 }
     92 
     93 gfx::Image SyncedNotificationAppInfo::icon() {
     94   if (settings_holder_ != NULL)
     95     return settings_holder_->low_dpi_image();
     96   else
     97     return gfx::Image();
     98 }
     99 
    100 std::vector<std::string> SyncedNotificationAppInfo::GetAppIdList() {
    101   std::vector<std::string> app_id_list;
    102   std::vector<std::string>::iterator it;
    103   for (it = app_ids_.begin(); it != app_ids_.end(); ++it) {
    104     app_id_list.push_back(*it);
    105   }
    106 
    107   return app_id_list;
    108 }
    109 
    110 // TODO: rename, queing now happens elsewhere.
    111 // Fill up the queue of bitmaps to fetch.
    112 void SyncedNotificationAppInfo::QueueBitmapFetchJobs() {
    113   // If there are no bitmaps to fetch, call OnBitmapFetchesDone.
    114   if (AreAllBitmapsFetched()) {
    115       synced_notification_app_info_service_->OnBitmapFetchesDone(
    116           added_app_ids_, removed_app_ids_);
    117     DVLOG(2) << "AppInfo object with no bitmaps, we should add some. "
    118              << this->settings_display_name_;
    119     return;
    120   }
    121 }
    122 
    123 // Start the bitmap fetching.  When it is complete, the callback
    124 // will notify the ChromeNotifierService of the new app info availablity.
    125 void SyncedNotificationAppInfo::StartBitmapFetch() {
    126   if (settings_holder_.get() != NULL)
    127     settings_holder_->StartFetch();
    128   if (monochrome_holder_.get() != NULL)
    129     monochrome_holder_->StartFetch();
    130   if (welcome_holder_.get() != NULL)
    131     welcome_holder_->StartFetch();
    132 }
    133 
    134 // Method inherited from ImageHolderDelegate
    135 void SyncedNotificationAppInfo::OnFetchComplete() {
    136   if (AreAllBitmapsFetched()) {
    137     if (synced_notification_app_info_service_ != NULL) {
    138       synced_notification_app_info_service_->OnBitmapFetchesDone(
    139           added_app_ids_, removed_app_ids_);
    140     }
    141   }
    142 }
    143 
    144 // Check to see if we have responses for all the bitmaps we got a URL for.
    145 bool SyncedNotificationAppInfo::AreAllBitmapsFetched() {
    146   bool done =
    147       (settings_holder_.get() == NULL || settings_holder_->IsFetchingDone()) &&
    148       (monochrome_holder_.get() == NULL ||
    149        monochrome_holder_->IsFetchingDone()) &&
    150       (welcome_holder_.get() == NULL || welcome_holder_->IsFetchingDone());
    151 
    152   return done;
    153 }
    154 
    155 message_center::NotifierId SyncedNotificationAppInfo::GetNotifierId() {
    156   return message_center::NotifierId(
    157       message_center::NotifierId::SYNCED_NOTIFICATION_SERVICE,
    158       settings_display_name_);
    159 }
    160 
    161 }  // namespace notifier
    162