privet_notifications.h revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_ 6#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_ 7 8#include <map> 9#include <string> 10 11#include "base/memory/linked_ptr.h" 12#include "base/memory/scoped_ptr.h" 13#include "chrome/browser/local_discovery/privet_device_lister.h" 14#include "chrome/browser/local_discovery/privet_device_lister_impl.h" 15#include "chrome/browser/local_discovery/privet_http.h" 16#include "chrome/browser/local_discovery/privet_http_asynchronous_factory.h" 17#include "chrome/browser/local_discovery/service_discovery_host_client.h" 18#include "chrome/browser/notifications/notification_delegate.h" 19#include "chrome/browser/notifications/notification_ui_manager.h" 20#include "components/browser_context_keyed_service/browser_context_keyed_service.h" 21#include "ui/message_center/notification_delegate.h" 22 23namespace content { 24class BrowserContext; 25} // namespace content 26 27namespace local_discovery { 28 29// Contains logic related to notifications not tied actually displaying them. 30class PrivetNotificationsListener : public PrivetInfoOperation::Delegate { 31 public: 32 class Delegate { 33 public: 34 virtual ~Delegate() {} 35 36 // Notify user of the existence of device |device_name|. 37 virtual void PrivetNotify(const std::string& device_name, 38 const std::string& human_readable_name, 39 const std::string& description) = 0; 40 41 // Remove the noitification for |device_name| if it still exists. 42 virtual void PrivetRemoveNotification(const std::string& device_name) = 0; 43 }; 44 45 PrivetNotificationsListener( 46 scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory, 47 Delegate* delegate); 48 virtual ~PrivetNotificationsListener(); 49 50 // These two methods are akin to those of PrivetDeviceLister::Delegate. The 51 // user of PrivetNotificationListener should create a PrivetDeviceLister and 52 // forward device notifications to the PrivetNotificationLister. 53 void DeviceChanged(bool added, 54 const std::string& name, 55 const DeviceDescription& description); 56 void DeviceRemoved(const std::string& name); 57 58 // PrivetInfoOperation::Delegate implementation. 59 virtual void OnPrivetInfoDone( 60 PrivetInfoOperation* operation, 61 int http_code, 62 const base::DictionaryValue* json_value) OVERRIDE; 63 64 private: 65 struct DeviceContext { 66 DeviceContext(); 67 ~DeviceContext(); 68 69 bool notification_may_be_active; 70 bool registered; 71 std::string human_readable_name; 72 std::string description; 73 scoped_ptr<PrivetInfoOperation> info_operation; 74 scoped_ptr<PrivetHTTPAsynchronousFactory::Resolution> 75 privet_http_resolution; 76 scoped_ptr<PrivetHTTPClient> privet_http; 77 }; 78 79 typedef std::map<std::string, linked_ptr<DeviceContext> > DeviceContextMap; 80 81 void CreateInfoOperation(scoped_ptr<PrivetHTTPClient> http_client); 82 83 Delegate* delegate_; 84 scoped_ptr<PrivetDeviceLister> device_lister_; 85 scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_; 86 DeviceContextMap devices_seen_; 87}; 88 89class PrivetNotificationService 90 : public BrowserContextKeyedService, 91 public PrivetDeviceLister::Delegate, 92 public PrivetNotificationsListener::Delegate, 93 public base::SupportsWeakPtr<PrivetNotificationService> { 94 public: 95 PrivetNotificationService(content::BrowserContext* profile, 96 NotificationUIManager* notification_manager); 97 virtual ~PrivetNotificationService(); 98 99 // PrivetDeviceLister::Delegate implementation: 100 virtual void DeviceChanged(bool added, const std::string& name, 101 const DeviceDescription& description) OVERRIDE; 102 virtual void DeviceRemoved(const std::string& name) OVERRIDE; 103 104 // PrivetNotificationListener::Delegate implementation: 105 virtual void PrivetNotify(const std::string& device_name, 106 const std::string& human_readable_name, 107 const std::string& description) OVERRIDE; 108 109 virtual void PrivetRemoveNotification( 110 const std::string& device_name) OVERRIDE; 111 private: 112 void Start(); 113 114 content::BrowserContext* profile_; 115 NotificationUIManager* notification_manager_; 116 scoped_ptr<PrivetDeviceLister> device_lister_; 117 scoped_refptr<ServiceDiscoveryHostClient> service_discovery_client_; 118 scoped_ptr<PrivetNotificationsListener> privet_notifications_listener_; 119}; 120 121class PrivetNotificationDelegate : public NotificationDelegate { 122 public: 123 explicit PrivetNotificationDelegate(const std::string& device_id, 124 content::BrowserContext* profile); 125 126 // NotificationDelegate implementation. 127 virtual std::string id() const OVERRIDE; 128 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE; 129 virtual void Display() OVERRIDE; 130 virtual void Error() OVERRIDE; 131 virtual void Close(bool by_user) OVERRIDE; 132 virtual void Click() OVERRIDE; 133 virtual void ButtonClick(int button_index) OVERRIDE; 134 135 private: 136 void OpenTab(const GURL& url); 137 138 virtual ~PrivetNotificationDelegate(); 139 140 std::string device_id_; 141 content::BrowserContext* profile_; 142}; 143 144} // namespace local_discovery 145 146#endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_ 147