| OLD | NEW |
| (Empty) | |
| 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 #include "chrome/browser/ui/views/ash/balloon_view_ash.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "ash/system/status_area_widget.h" |
| 9 #include "ash/system/web_notification/web_notification_tray.h" |
| 10 #include "base/logging.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/notifications/balloon_collection.h" |
| 13 #include "chrome/browser/notifications/notification.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/icon_messages.h" |
| 16 #include "content/public/browser/render_process_host.h" |
| 17 #include "content/public/browser/render_view_host.h" |
| 18 #include "content/public/browser/site_instance.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/browser/web_contents_delegate.h" |
| 21 #include "content/public/browser/web_contents_observer.h" |
| 22 #include "ipc/ipc_message.h" |
| 23 #include "ipc/ipc_message_macros.h" |
| 24 #include "ui/gfx/image/image_skia.h" |
| 25 #include "webkit/glue/image_resource_fetcher.h" |
| 26 |
| 27 namespace { |
| 28 |
| 29 const int kNotificationIconImageSize = 32; |
| 30 |
| 31 ash::WebNotificationTray* GetWebNotificationTray() { |
| 32 return ash::Shell::GetInstance()-> |
| 33 status_area_widget()->web_notification_tray(); |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 38 class BalloonViewAsh::IconFetcher : public content::WebContentsObserver { |
| 39 public: |
| 40 IconFetcher(content::WebContents* web_contents, |
| 41 const std::string& notification_id, |
| 42 const GURL& icon_url) |
| 43 : content::WebContentsObserver(web_contents), |
| 44 request_id_(0), |
| 45 notification_id_(notification_id), |
| 46 icon_url_(icon_url) { |
| 47 Observe(web_contents); |
| 48 content::RenderViewHost* host = web_contents->GetRenderViewHost(); |
| 49 host->Send(new IconMsg_DownloadFavicon(host->GetRoutingID(), |
| 50 ++request_id_, |
| 51 icon_url, |
| 52 kNotificationIconImageSize)); |
| 53 } |
| 54 |
| 55 // content::WebContentsObserver override. |
| 56 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| 57 bool message_handled = false; // Allow other handlers to receive these. |
| 58 IPC_BEGIN_MESSAGE_MAP(IconFetcher, message) |
| 59 IPC_MESSAGE_HANDLER(IconHostMsg_DidDownloadFavicon, OnDidDownloadFavicon) |
| 60 IPC_MESSAGE_UNHANDLED(message_handled = false) |
| 61 IPC_END_MESSAGE_MAP() |
| 62 return message_handled; |
| 63 } |
| 64 |
| 65 void OnDidDownloadFavicon(int id, |
| 66 const GURL& image_url, |
| 67 bool errored, |
| 68 const SkBitmap& bitmap) { |
| 69 if (image_url != icon_url_ || id != request_id_) |
| 70 return; |
| 71 GetWebNotificationTray()->SetNotificationImage( |
| 72 notification_id_, gfx::ImageSkia(bitmap)); |
| 73 } |
| 74 |
| 75 private: |
| 76 int request_id_; |
| 77 std::string notification_id_; |
| 78 GURL icon_url_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(IconFetcher); |
| 81 }; |
| 82 |
| 83 BalloonViewAsh::BalloonViewAsh(BalloonCollection* collection) |
| 84 : collection_(collection), |
| 85 balloon_(NULL) { |
| 86 } |
| 87 |
| 88 BalloonViewAsh::~BalloonViewAsh() { |
| 89 } |
| 90 |
| 91 // BalloonView interface. |
| 92 void BalloonViewAsh::Show(Balloon* balloon) { |
| 93 balloon_ = balloon; |
| 94 const Notification& notification = balloon_->notification(); |
| 95 std::string extension_id = GetExtensionId(balloon); |
| 96 GetWebNotificationTray()->AddNotification(notification.notification_id(), |
| 97 notification.title(), |
| 98 notification.body(), |
| 99 notification.display_source(), |
| 100 extension_id); |
| 101 FetchIcon(notification); |
| 102 } |
| 103 |
| 104 void BalloonViewAsh::Update() { |
| 105 const Notification& notification = balloon_->notification(); |
| 106 GetWebNotificationTray()->UpdateNotification(notification.notification_id(), |
| 107 notification.title(), |
| 108 notification.body()); |
| 109 FetchIcon(notification); |
| 110 } |
| 111 |
| 112 void BalloonViewAsh::RepositionToBalloon() { |
| 113 } |
| 114 |
| 115 void BalloonViewAsh::Close(bool by_user) { |
| 116 Notification notification(balloon_->notification()); // Copy notification |
| 117 collection_->OnBalloonClosed(balloon_); // Deletes balloon. |
| 118 notification.Close(by_user); |
| 119 GetWebNotificationTray()->RemoveNotification(notification.notification_id()); |
| 120 } |
| 121 |
| 122 gfx::Size BalloonViewAsh::GetSize() const { |
| 123 return gfx::Size(); |
| 124 } |
| 125 |
| 126 BalloonHost* BalloonViewAsh::GetHost() const { |
| 127 return NULL; |
| 128 } |
| 129 |
| 130 void BalloonViewAsh::FetchIcon(const Notification& notification) { |
| 131 if (!notification.icon().empty()) { |
| 132 ash::Shell::GetInstance()->status_area_widget()-> |
| 133 web_notification_tray()->SetNotificationImage( |
| 134 notification.notification_id(), notification.icon()); |
| 135 return; |
| 136 } |
| 137 if (notification.icon_url().is_empty()) |
| 138 return; |
| 139 content::RenderViewHost* rvh = notification.GetRenderViewHost(); |
| 140 if (!rvh) { |
| 141 LOG(WARNING) << "Notification has icon url but no RenderViewHost"; |
| 142 return; |
| 143 } |
| 144 content::WebContents* web_contents = |
| 145 content::WebContents::FromRenderViewHost(rvh); |
| 146 if (!web_contents) { |
| 147 LOG(WARNING) << "Notification has icon url but no WebContents"; |
| 148 return; |
| 149 } |
| 150 icon_fetcher_.reset(new IconFetcher(web_contents, |
| 151 notification.notification_id(), |
| 152 notification.icon_url())); |
| 153 } |
| 154 |
| 155 std::string BalloonViewAsh::GetExtensionId(Balloon* balloon) { |
| 156 ExtensionService* extension_service = |
| 157 balloon_->profile()->GetExtensionService(); |
| 158 const GURL& origin = balloon_->notification().origin_url(); |
| 159 const extensions::Extension* extension = |
| 160 extension_service->extensions()->GetExtensionOrAppByURL( |
| 161 ExtensionURLInfo(origin)); |
| 162 if (extension) |
| 163 return extension->id(); |
| 164 return std::string(); |
| 165 } |
| OLD | NEW |