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