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