| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/ash/balloon_view_ash.h" | 5 #include "chrome/browser/ui/views/balloon_view_win.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | |
| 8 #include "ash/system/web_notification/web_notification_tray.h" | |
| 9 #include "base/logging.h" | 7 #include "base/logging.h" |
| 10 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 11 #include "base/values.h" | 9 #include "base/values.h" |
| 12 #include "chrome/browser/favicon/favicon_util.h" | 10 #include "chrome/browser/favicon/favicon_util.h" |
| 13 #include "chrome/browser/notifications/balloon_collection.h" | 11 #include "chrome/browser/notifications/balloon_collection.h" |
| 14 #include "chrome/browser/notifications/notification.h" | 12 #include "chrome/browser/notifications/notification.h" |
| 13 #include "chrome/browser/ui/views/message_center/message_center_tray_host_win.h" |
| 15 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
| 16 #include "content/public/browser/render_view_host.h" | 15 #include "content/public/browser/render_view_host.h" |
| 17 #include "content/public/browser/site_instance.h" | 16 #include "content/public/browser/site_instance.h" |
| 18 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/browser/web_contents_delegate.h" | 18 #include "content/public/browser/web_contents_delegate.h" |
| 20 #include "content/public/browser/web_contents_observer.h" | 19 #include "content/public/browser/web_contents_observer.h" |
| 21 #include "ipc/ipc_message.h" | 20 #include "ipc/ipc_message.h" |
| 22 #include "ipc/ipc_message_macros.h" | 21 #include "ipc/ipc_message_macros.h" |
| 23 #include "ui/gfx/image/image_skia.h" | 22 #include "ui/gfx/image/image_skia.h" |
| 24 #include "ui/message_center/message_center.h" | 23 #include "ui/message_center/message_center.h" |
| 25 #include "ui/message_center/message_center_constants.h" | 24 #include "ui/message_center/message_center_constants.h" |
| 26 #include "webkit/glue/image_resource_fetcher.h" | 25 #include "webkit/glue/image_resource_fetcher.h" |
| 27 | 26 |
| 28 namespace { | 27 namespace { |
| 29 | 28 |
| 30 typedef base::Callback<void(const gfx::ImageSkia&)> SetImageCallback; | 29 typedef base::Callback<void(const gfx::ImageSkia&)> SetImageCallback; |
| 31 | 30 |
| 32 const int kPrimaryIconImageSize = 64; | 31 const int kPrimaryIconImageSize = 64; |
| 33 const int kSecondaryIconImageSize = 15; | 32 const int kSecondaryIconImageSize = 15; |
| 34 | 33 |
| 35 // static | 34 // static |
| 36 message_center::MessageCenter* GetMessageCenter() { | 35 message_center::MessageCenter* GetMessageCenter() { |
| 37 return ash::Shell::GetInstance()->GetWebNotificationTray()->message_center(); | 36 return ui::MessageCenterTrayHostWin::GetInstance()->message_center(); |
| 38 } | 37 } |
| 39 | 38 |
| 40 } // namespace | 39 } // namespace |
| 41 | 40 |
| 42 // TODO(dharcourt): Delay showing the notification until all images are | 41 // TODO(dharcourt): Delay showing the notification until all images are |
| 43 // downloaded, and return an error to the notification creator/API caller | 42 // downloaded, and return an error to the notification creator/API caller |
| 44 // instead of showing a partial notification if any image download fails. | 43 // instead of showing a partial notification if any image download fails. |
| 45 class BalloonViewAsh::ImageDownload | 44 class BalloonViewWin::ImageDownload |
| 46 : public base::SupportsWeakPtr<ImageDownload> { | 45 : public base::SupportsWeakPtr<ImageDownload> { |
| 47 public: | 46 public: |
| 48 // Note that the setter callback passed in will not be called if the image | 47 // Note that the setter callback passed in will not be called if the image |
| 49 // download fails for any reason. | 48 // download fails for any reason. |
| 50 ImageDownload(const Notification& notification, | 49 ImageDownload(const Notification& notification, |
| 51 const GURL& url, | 50 const GURL& url, |
| 52 int size, | 51 int size, |
| 53 const SetImageCallback& callback); | 52 const SetImageCallback& callback); |
| 54 virtual ~ImageDownload(); | 53 virtual ~ImageDownload(); |
| 55 | 54 |
| 56 private: | 55 private: |
| 57 // FaviconHelper callback. | 56 // FaviconHelper callback. |
| 58 virtual void Downloaded(int download_id, | 57 virtual void Downloaded(int download_id, |
| 59 const GURL& image_url, | 58 const GURL& image_url, |
| 60 int requested_size, | 59 int requested_size, |
| 61 const std::vector<SkBitmap>& bitmaps); | 60 const std::vector<SkBitmap>& bitmaps); |
| 62 | 61 |
| 63 const GURL& url_; | 62 const GURL& url_; |
| 64 int size_; | 63 int size_; |
| 65 SetImageCallback callback_; | 64 SetImageCallback callback_; |
| 66 | 65 |
| 67 DISALLOW_COPY_AND_ASSIGN(ImageDownload); | 66 DISALLOW_COPY_AND_ASSIGN(ImageDownload); |
| 68 }; | 67 }; |
| 69 | 68 |
| 70 BalloonViewAsh::ImageDownload::ImageDownload(const Notification& notification, | 69 BalloonViewWin::ImageDownload::ImageDownload(const Notification& notification, |
| 71 const GURL& url, | 70 const GURL& url, |
| 72 int size, | 71 int size, |
| 73 const SetImageCallback& callback) | 72 const SetImageCallback& callback) |
| 74 : url_(url), | 73 : url_(url), |
| 75 size_(size), | 74 size_(size), |
| 76 callback_(callback) { | 75 callback_(callback) { |
| 77 content::RenderViewHost* host = notification.GetRenderViewHost(); | 76 content::RenderViewHost* host = notification.GetRenderViewHost(); |
| 78 if (!host) { | 77 if (!host) { |
| 79 LOG(WARNING) << "Notification needs an image but has no RenderViewHost"; | 78 LOG(WARNING) << "Notification needs an image but has no RenderViewHost"; |
| 80 return; | 79 return; |
| 81 } | 80 } |
| 82 | 81 |
| 83 content::WebContents* contents = | 82 content::WebContents* contents = |
| 84 content::WebContents::FromRenderViewHost(host); | 83 content::WebContents::FromRenderViewHost(host); |
| 85 if (!contents) { | 84 if (!contents) { |
| 86 LOG(WARNING) << "Notification needs an image but has no WebContents"; | 85 LOG(WARNING) << "Notification needs an image but has no WebContents"; |
| 87 return; | 86 return; |
| 88 } | 87 } |
| 89 | 88 |
| 90 contents->DownloadFavicon(url_, size_, base::Bind(&ImageDownload::Downloaded, | 89 contents->DownloadFavicon(url_, size_, base::Bind(&ImageDownload::Downloaded, |
| 91 AsWeakPtr())); | 90 AsWeakPtr())); |
| 92 } | 91 } |
| 93 | 92 |
| 94 | 93 BalloonViewWin::ImageDownload::~ImageDownload() { |
| 95 BalloonViewAsh::ImageDownload::~ImageDownload() { | |
| 96 } | 94 } |
| 97 | 95 |
| 98 void BalloonViewAsh::ImageDownload::Downloaded( | 96 void BalloonViewWin::ImageDownload::Downloaded( |
| 99 int download_id, | 97 int download_id, |
| 100 const GURL& image_url, | 98 const GURL& image_url, |
| 101 int requested_size, | 99 int requested_size, |
| 102 const std::vector<SkBitmap>& bitmaps) { | 100 const std::vector<SkBitmap>& bitmaps) { |
| 103 if (bitmaps.empty()) | 101 if (bitmaps.empty()) |
| 104 return; | 102 return; |
| 105 gfx::ImageSkia image(bitmaps[0]); | 103 gfx::ImageSkia image(bitmaps[0]); |
| 106 callback_.Run(image); | 104 callback_.Run(image); |
| 107 } | 105 } |
| 108 | 106 |
| 109 BalloonViewAsh::BalloonViewAsh(BalloonCollection* collection) | 107 BalloonViewWin::BalloonViewWin(BalloonCollection* collection) |
| 110 : collection_(collection), | 108 : collection_(collection), |
| 111 balloon_(NULL) { | 109 balloon_(NULL) { |
| 112 } | 110 } |
| 113 | 111 |
| 114 BalloonViewAsh::~BalloonViewAsh() { | 112 BalloonViewWin::~BalloonViewWin() { |
| 115 } | 113 } |
| 116 | 114 |
| 117 // BalloonView interface. | 115 // BalloonView interface. |
| 118 void BalloonViewAsh::Show(Balloon* balloon) { | 116 void BalloonViewWin::Show(Balloon* balloon) { |
| 119 balloon_ = balloon; | 117 balloon_ = balloon; |
| 120 const Notification& notification = balloon_->notification(); | 118 const Notification& notification = balloon_->notification(); |
| 121 notification_id_ = notification.notification_id(); | 119 notification_id_ = notification.notification_id(); |
| 122 GetMessageCenter()->AddNotification(notification.type(), | 120 GetMessageCenter()->AddNotification(notification.type(), |
| 123 notification_id_, | 121 notification_id_, |
| 124 notification.title(), | 122 notification.title(), |
| 125 notification.body(), | 123 notification.body(), |
| 126 notification.display_source(), | 124 notification.display_source(), |
| 127 balloon->GetExtensionId(), | 125 balloon->GetExtensionId(), |
| 128 notification.optional_fields()); | 126 notification.optional_fields()); |
| 129 DownloadImages(notification); | 127 DownloadImages(notification); |
| 130 } | 128 } |
| 131 | 129 |
| 132 void BalloonViewAsh::Update() { | 130 void BalloonViewWin::Update() { |
| 133 std::string previous_notification_id = notification_id_; | 131 std::string previous_notification_id = notification_id_; |
| 134 const Notification& notification = balloon_->notification(); | 132 const Notification& notification = balloon_->notification(); |
| 135 notification_id_ = notification.notification_id(); | 133 notification_id_ = notification.notification_id(); |
| 136 GetMessageCenter()->UpdateNotification(previous_notification_id, | 134 GetMessageCenter()->UpdateNotification(previous_notification_id, |
| 137 notification_id_, | 135 notification_id_, |
| 138 notification.title(), | 136 notification.title(), |
| 139 notification.body(), | 137 notification.body(), |
| 140 notification.optional_fields()); | 138 notification.optional_fields()); |
| 141 DownloadImages(notification); | 139 DownloadImages(notification); |
| 142 } | 140 } |
| 143 | 141 |
| 144 void BalloonViewAsh::RepositionToBalloon() { | 142 void BalloonViewWin::RepositionToBalloon() { |
| 145 } | 143 } |
| 146 | 144 |
| 147 void BalloonViewAsh::Close(bool by_user) { | 145 void BalloonViewWin::Close(bool by_user) { |
| 148 Notification notification(balloon_->notification()); | 146 Notification notification(balloon_->notification()); |
| 149 collection_->OnBalloonClosed(balloon_); // Deletes balloon. | 147 collection_->OnBalloonClosed(balloon_); // Deletes balloon. |
| 150 notification.Close(by_user); | 148 notification.Close(by_user); |
| 151 GetMessageCenter()->RemoveNotification(notification.notification_id()); | 149 GetMessageCenter()->RemoveNotification(notification.notification_id()); |
| 152 } | 150 } |
| 153 | 151 |
| 154 gfx::Size BalloonViewAsh::GetSize() const { | 152 gfx::Size BalloonViewWin::GetSize() const { |
| 155 return gfx::Size(); | 153 return gfx::Size(); |
| 156 } | 154 } |
| 157 | 155 |
| 158 BalloonHost* BalloonViewAsh::GetHost() const { | 156 BalloonHost* BalloonViewWin::GetHost() const { |
| 159 return NULL; | 157 return NULL; |
| 160 } | 158 } |
| 161 | 159 |
| 162 void BalloonViewAsh::SetNotificationIcon(const std::string& id, | 160 void BalloonViewWin::SetNotificationIcon(const std::string& id, |
| 163 const gfx::ImageSkia& image) { | 161 const gfx::ImageSkia& image) { |
| 164 GetMessageCenter()->SetNotificationPrimaryIcon(id, image); | 162 GetMessageCenter()->SetNotificationPrimaryIcon(id, image); |
| 165 } | 163 } |
| 166 | 164 |
| 167 void BalloonViewAsh::SetNotificationImage(const std::string& id, | 165 void BalloonViewWin::SetNotificationImage(const std::string& id, |
| 168 const gfx::ImageSkia& image) { | 166 const gfx::ImageSkia& image) { |
| 169 GetMessageCenter()->SetNotificationImage(id, image); | 167 GetMessageCenter()->SetNotificationImage(id, image); |
| 170 } | 168 } |
| 171 | 169 |
| 172 void BalloonViewAsh::DownloadImages(const Notification& notification) { | 170 void BalloonViewWin::DownloadImages(const Notification& notification) { |
| 173 // Cancel any previous downloads. | 171 // Cancel any previous downloads. |
| 174 downloads_.clear(); | 172 downloads_.clear(); |
| 175 | 173 |
| 176 // Set the notification's primary icon, or start a download for it. | 174 // Set the notification's primary icon, or start a download for it. |
| 177 if (!notification.icon().isNull()) { | 175 if (!notification.icon().isNull()) { |
| 178 SetNotificationIcon(notification_id_, notification.icon()); | 176 SetNotificationIcon(notification_id_, notification.icon()); |
| 179 } else if (!notification.icon_url().is_empty()) { | 177 } else if (!notification.icon_url().is_empty()) { |
| 180 downloads_.push_back(linked_ptr<ImageDownload>(new ImageDownload( | 178 downloads_.push_back(linked_ptr<ImageDownload>(new ImageDownload( |
| 181 notification, notification.icon_url(), | 179 notification, notification.icon_url(), |
| 182 message_center::kNotificationIconWidth, | 180 message_center::kNotificationIconWidth, |
| 183 base::Bind(&BalloonViewAsh::SetNotificationIcon, | 181 base::Bind(&BalloonViewWin::SetNotificationIcon, |
| 184 base::Unretained(this), notification.notification_id())))); | 182 base::Unretained(this), notification.notification_id())))); |
| 185 } | 183 } |
| 186 | 184 |
| 187 // Start a download for the notification's image if appropriate. | 185 // Start a download for the notification's image if appropriate. |
| 188 const base::DictionaryValue* optional_fields = notification.optional_fields(); | 186 const base::DictionaryValue* optional_fields = notification.optional_fields(); |
| 189 if (optional_fields && | 187 if (optional_fields && |
| 190 optional_fields->HasKey(ui::notifications::kImageUrlKey)) { | 188 optional_fields->HasKey(ui::notifications::kImageUrlKey)) { |
| 191 string16 url; | 189 string16 url; |
| 192 optional_fields->GetString(ui::notifications::kImageUrlKey, &url); | 190 optional_fields->GetString(ui::notifications::kImageUrlKey, &url); |
| 193 if (!url.empty()) { | 191 if (!url.empty()) { |
| 194 downloads_.push_back(linked_ptr<ImageDownload>(new ImageDownload( | 192 downloads_.push_back(linked_ptr<ImageDownload>(new ImageDownload( |
| 195 notification, GURL(url), | 193 notification, GURL(url), |
| 196 message_center::kNotificationPreferredImageSize, | 194 message_center::kNotificationPreferredImageSize, |
| 197 base::Bind(&BalloonViewAsh::SetNotificationImage, | 195 base::Bind(&BalloonViewWin::SetNotificationImage, |
| 198 base::Unretained(this), notification.notification_id())))); | 196 base::Unretained(this), notification.notification_id())))); |
| 199 } | 197 } |
| 200 } | 198 } |
| 201 } | 199 } |
| OLD | NEW |