OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/local_discovery/privet_notifications.h" | 5 #include "chrome/browser/local_discovery/privet_notifications.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/local_discovery/privet_device_lister_impl.h" | 10 #include "chrome/browser/local_discovery/privet_device_lister_impl.h" |
11 #include "chrome/browser/notifications/notification.h" | 11 #include "chrome/browser/notifications/notification.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_finder.h" |
| 15 #include "chrome/browser/ui/host_desktop.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
13 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
| 18 #include "content/public/browser/navigation_controller.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/common/page_transition_types.h" |
14 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 22 #include "grit/theme_resources.h" |
15 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "ui/base/resource/resource_bundle.h" |
| 25 #include "ui/message_center/notifier_settings.h" |
16 | 26 |
17 namespace local_discovery { | 27 namespace local_discovery { |
18 | 28 |
19 namespace { | 29 namespace { |
20 const int kTenMinutesInSeconds = 600; | 30 const int kTenMinutesInSeconds = 600; |
21 const char kPrivetInfoKeyUptime[] = "uptime"; | 31 const char kPrivetInfoKeyUptime[] = "uptime"; |
22 const char kPrivetNotificationIDPrefix[] = "privet_notification:"; | 32 const char kPrivetNotificationIDPrefix[] = "privet_notification:"; |
23 const char kPrivetNotificationOriginUrl[] = "chrome://devices"; | 33 const char kPrivetNotificationOriginUrl[] = "chrome://devices"; |
24 } | 34 } |
25 | 35 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 153 |
144 void PrivetNotificationService::DeviceRemoved(const std::string& name) { | 154 void PrivetNotificationService::DeviceRemoved(const std::string& name) { |
145 privet_notifications_listener_->DeviceRemoved(name); | 155 privet_notifications_listener_->DeviceRemoved(name); |
146 } | 156 } |
147 | 157 |
148 void PrivetNotificationService::PrivetNotify( | 158 void PrivetNotificationService::PrivetNotify( |
149 const std::string& device_name, | 159 const std::string& device_name, |
150 const std::string& human_readable_name, | 160 const std::string& human_readable_name, |
151 const std::string& description) { | 161 const std::string& description) { |
152 Profile* profile_object = Profile::FromBrowserContext(profile_); | 162 Profile* profile_object = Profile::FromBrowserContext(profile_); |
| 163 message_center::RichNotificationData rich_notification_data; |
| 164 |
| 165 rich_notification_data.buttons.push_back( |
| 166 message_center::ButtonInfo(l10n_util::GetStringUTF16( |
| 167 IDS_LOCAL_DISOCVERY_NOTIFICATION_BUTTON_PRINTER))); |
| 168 |
153 Notification notification( | 169 Notification notification( |
| 170 message_center::NOTIFICATION_TYPE_SIMPLE, |
154 GURL(kPrivetNotificationOriginUrl), | 171 GURL(kPrivetNotificationOriginUrl), |
155 GURL(), | |
156 l10n_util::GetStringUTF16(IDS_LOCAL_DISOCVERY_NOTIFICATION_TITLE_PRINTER), | 172 l10n_util::GetStringUTF16(IDS_LOCAL_DISOCVERY_NOTIFICATION_TITLE_PRINTER), |
157 l10n_util::GetStringFUTF16( | 173 l10n_util::GetStringFUTF16( |
158 IDS_LOCAL_DISOCVERY_NOTIFICATION_CONTENTS_PRINTER, | 174 IDS_LOCAL_DISOCVERY_NOTIFICATION_CONTENTS_PRINTER, |
159 UTF8ToUTF16(human_readable_name)), | 175 UTF8ToUTF16(human_readable_name)), |
| 176 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 177 IDR_LOCAL_DISCOVERY_CLOUDPRINT_ICON), |
160 WebKit::WebTextDirectionDefault, | 178 WebKit::WebTextDirectionDefault, |
| 179 message_center::NotifierId( |
| 180 message_center::NotifierId::SYSTEM_COMPONENT), |
161 l10n_util::GetStringUTF16( | 181 l10n_util::GetStringUTF16( |
162 IDS_LOCAL_DISOCVERY_NOTIFICATION_DISPLAY_SOURCE_PRINTER), | 182 IDS_LOCAL_DISOCVERY_NOTIFICATION_DISPLAY_SOURCE_PRINTER), |
163 UTF8ToUTF16(kPrivetNotificationIDPrefix + | 183 UTF8ToUTF16(kPrivetNotificationIDPrefix + |
164 device_name), | 184 device_name), |
165 new PrivetNotificationDelegate(device_name)); | 185 rich_notification_data, |
| 186 new PrivetNotificationDelegate(device_name, profile_)); |
166 | 187 |
167 notification_manager_->Add(notification, profile_object); | 188 notification_manager_->Add(notification, profile_object); |
168 } | 189 } |
169 | 190 |
170 void PrivetNotificationService::PrivetRemoveNotification( | 191 void PrivetNotificationService::PrivetRemoveNotification( |
171 const std::string& device_name) { | 192 const std::string& device_name) { |
172 notification_manager_->CancelById(kPrivetNotificationIDPrefix + device_name); | 193 notification_manager_->CancelById(kPrivetNotificationIDPrefix + device_name); |
173 } | 194 } |
174 | 195 |
175 void PrivetNotificationService::Start() { | 196 void PrivetNotificationService::Start() { |
176 service_discovery_client_ = ServiceDiscoveryHostClientFactory::GetClient(); | 197 service_discovery_client_ = ServiceDiscoveryHostClientFactory::GetClient(); |
177 device_lister_.reset(new PrivetDeviceListerImpl(service_discovery_client_, | 198 device_lister_.reset(new PrivetDeviceListerImpl(service_discovery_client_, |
178 this)); | 199 this)); |
179 device_lister_->Start(); | 200 device_lister_->Start(); |
180 | 201 |
181 scoped_ptr<PrivetHTTPAsynchronousFactory> http_factory( | 202 scoped_ptr<PrivetHTTPAsynchronousFactory> http_factory( |
182 new PrivetHTTPAsynchronousFactoryImpl(service_discovery_client_.get(), | 203 new PrivetHTTPAsynchronousFactoryImpl(service_discovery_client_.get(), |
183 profile_->GetRequestContext())); | 204 profile_->GetRequestContext())); |
184 | 205 |
185 privet_notifications_listener_.reset(new PrivetNotificationsListener( | 206 privet_notifications_listener_.reset(new PrivetNotificationsListener( |
186 http_factory.Pass(), this)); | 207 http_factory.Pass(), this)); |
187 } | 208 } |
188 | 209 |
189 PrivetNotificationDelegate::PrivetNotificationDelegate( | 210 PrivetNotificationDelegate::PrivetNotificationDelegate( |
190 const std::string& device_id) : device_id_(device_id) { | 211 const std::string& device_id, content::BrowserContext* profile) |
| 212 : device_id_(device_id), profile_(profile) { |
191 } | 213 } |
192 | 214 |
193 PrivetNotificationDelegate::~PrivetNotificationDelegate() { | 215 PrivetNotificationDelegate::~PrivetNotificationDelegate() { |
194 } | 216 } |
195 | 217 |
196 std::string PrivetNotificationDelegate::id() const { | 218 std::string PrivetNotificationDelegate::id() const { |
197 return kPrivetNotificationIDPrefix + device_id_; | 219 return kPrivetNotificationIDPrefix + device_id_; |
198 } | 220 } |
199 | 221 |
200 content::RenderViewHost* PrivetNotificationDelegate::GetRenderViewHost() const { | 222 content::RenderViewHost* PrivetNotificationDelegate::GetRenderViewHost() const { |
201 return NULL; | 223 return NULL; |
202 } | 224 } |
203 | 225 |
204 void PrivetNotificationDelegate::Display() { | 226 void PrivetNotificationDelegate::Display() { |
205 } | 227 } |
206 | 228 |
207 void PrivetNotificationDelegate::Error() { | 229 void PrivetNotificationDelegate::Error() { |
208 LOG(ERROR) << "Error displaying privet notification " << device_id_; | 230 LOG(ERROR) << "Error displaying privet notification " << device_id_; |
209 } | 231 } |
210 | 232 |
211 void PrivetNotificationDelegate::Close(bool by_user) { | 233 void PrivetNotificationDelegate::Close(bool by_user) { |
212 } | 234 } |
213 | 235 |
214 void PrivetNotificationDelegate::Click() { | 236 void PrivetNotificationDelegate::Click() { |
215 } | 237 } |
216 | 238 |
| 239 void PrivetNotificationDelegate::ButtonClick(int button_index) { |
| 240 if (button_index == 0) { |
| 241 // TODO(noamsml): Direct-to-register URL |
| 242 OpenTab(GURL(kPrivetNotificationOriginUrl)); |
| 243 } |
| 244 } |
| 245 |
| 246 void PrivetNotificationDelegate::OpenTab(const GURL& url) { |
| 247 Profile* profile_obj = Profile::FromBrowserContext(profile_); |
| 248 |
| 249 Browser* browser = FindOrCreateTabbedBrowser(profile_obj, |
| 250 chrome::GetActiveDesktop()); |
| 251 content::WebContents::CreateParams create_params(profile_obj); |
| 252 |
| 253 scoped_ptr<content::WebContents> contents( |
| 254 content::WebContents::Create(create_params)); |
| 255 contents->GetController().LoadURL(url, |
| 256 content::Referrer(), |
| 257 content::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 258 ""); |
| 259 |
| 260 browser->tab_strip_model()->AppendWebContents(contents.release(), true); |
| 261 } |
| 262 |
| 263 |
217 } // namespace local_discovery | 264 } // namespace local_discovery |
OLD | NEW |