Chromium Code Reviews| 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 } // namespace | |
| 32 | |
| 33 class BalloonViewAsh::IconFetcher : public content::WebContentsObserver { | |
| 34 public: | |
| 35 IconFetcher(content::WebContents* web_contents, | |
| 36 const std::string& notification_id, | |
| 37 const GURL& icon_url) | |
| 38 : content::WebContentsObserver(web_contents), | |
| 39 request_id_(0), | |
| 40 notification_id_(notification_id), | |
| 41 icon_url_(icon_url) { | |
| 42 Observe(web_contents); | |
| 43 content::RenderViewHost* host = web_contents->GetRenderViewHost(); | |
| 44 host->Send(new IconMsg_DownloadFavicon(host->GetRoutingID(), | |
| 45 ++request_id_, | |
| 46 icon_url, | |
| 47 kNotificationIconImageSize)); | |
| 48 } | |
| 49 | |
| 50 // content::WebContentsObserver override. | |
| 51 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | |
| 52 bool message_handled = false; // Allow other handlers to receive these. | |
| 53 IPC_BEGIN_MESSAGE_MAP(IconFetcher, message) | |
| 54 IPC_MESSAGE_HANDLER(IconHostMsg_DidDownloadFavicon, OnDidDownloadFavicon) | |
| 55 IPC_MESSAGE_UNHANDLED(message_handled = false) | |
| 56 IPC_END_MESSAGE_MAP() | |
| 57 return message_handled; | |
| 58 } | |
| 59 | |
| 60 void OnDidDownloadFavicon(int id, | |
| 61 const GURL& image_url, | |
| 62 bool errored, | |
| 63 const SkBitmap& bitmap) { | |
| 64 if (image_url != icon_url_ || id != request_id_) | |
| 65 return; | |
| 66 ash::Shell::GetInstance()->status_area_widget()-> | |
| 67 web_notification_tray()->SetNotificationImage( | |
| 68 notification_id_, gfx::ImageSkia(bitmap)); | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 int request_id_; | |
| 73 std::string notification_id_; | |
| 74 GURL icon_url_; | |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(IconFetcher); | |
| 77 }; | |
| 78 | |
| 79 BalloonViewAsh::BalloonViewAsh(BalloonCollection* collection) | |
| 80 : collection_(collection), | |
| 81 balloon_(NULL) { | |
| 82 } | |
| 83 | |
| 84 BalloonViewAsh::~BalloonViewAsh() { | |
| 85 } | |
| 86 | |
| 87 // BalloonView interface. | |
| 88 void BalloonViewAsh::Show(Balloon* balloon) { | |
| 89 balloon_ = balloon; | |
| 90 const Notification& notification = balloon_->notification(); | |
| 91 std::string extension_id = GetExtensionId(balloon); | |
| 92 ash::Shell::GetInstance()->status_area_widget()-> | |
| 93 web_notification_tray()->AddNotification( | |
|
oshima
2012/06/13 23:58:02
how about defining accessor function to WebNotific
stevenjb
2012/06/14 04:27:35
Also a good idea. Done.
| |
| 94 notification.notification_id(), | |
| 95 notification.title(), | |
| 96 notification.body(), | |
| 97 notification.display_source(), | |
| 98 extension_id); | |
| 99 FetchIcon(notification); | |
| 100 } | |
| 101 | |
| 102 void BalloonViewAsh::Update() { | |
| 103 const Notification& notification = balloon_->notification(); | |
| 104 ash::Shell::GetInstance()->status_area_widget()-> | |
| 105 web_notification_tray()->UpdateNotification( | |
| 106 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 ash::Shell::GetInstance()->status_area_widget()-> | |
| 120 web_notification_tray()->RemoveNotification( | |
| 121 notification.notification_id()); | |
| 122 } | |
| 123 | |
| 124 gfx::Size BalloonViewAsh::GetSize() const { | |
| 125 return gfx::Size(); | |
| 126 } | |
| 127 | |
| 128 BalloonHost* BalloonViewAsh::GetHost() const { | |
| 129 return NULL; | |
| 130 } | |
| 131 | |
| 132 void BalloonViewAsh::FetchIcon(const Notification& notification) { | |
| 133 if (!notification.icon().empty()) { | |
| 134 ash::Shell::GetInstance()->status_area_widget()-> | |
| 135 web_notification_tray()->SetNotificationImage( | |
| 136 notification.notification_id(), notification.icon()); | |
| 137 return; | |
| 138 } | |
| 139 if (notification.icon_url().is_empty()) | |
| 140 return; | |
| 141 content::RenderViewHost* rvh = notification.GetRenderViewHost(); | |
| 142 if (!rvh) { | |
| 143 LOG(WARNING) << "Notification has icon url but no RenderViewHost"; | |
| 144 return; | |
| 145 } | |
| 146 content::WebContents* web_contents = | |
| 147 content::WebContents::FromRenderViewHost(rvh); | |
| 148 if (!web_contents) { | |
| 149 LOG(WARNING) << "Notification has icon url but no WebContents"; | |
| 150 return; | |
| 151 } | |
| 152 icon_fetcher_.reset(new IconFetcher(web_contents, | |
| 153 notification.notification_id(), | |
| 154 notification.icon_url())); | |
| 155 } | |
| 156 | |
| 157 std::string BalloonViewAsh::GetExtensionId(Balloon* balloon) { | |
| 158 ExtensionService* extension_service = | |
| 159 balloon_->profile()->GetExtensionService(); | |
| 160 const GURL& origin = balloon_->notification().origin_url(); | |
| 161 const extensions::Extension* extension = | |
| 162 extension_service->extensions()->GetExtensionOrAppByURL( | |
| 163 ExtensionURLInfo(origin)); | |
| 164 if (extension) | |
| 165 return extension->id(); | |
| 166 return std::string(); | |
| 167 } | |
| OLD | NEW |