privet_notifications.h revision 58537e28ecd584eab876aee8be7156509866d23a
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 virtual void DeviceCacheFlushed(); 58 59 // PrivetInfoOperation::Delegate implementation. 60 virtual void OnPrivetInfoDone( 61 PrivetInfoOperation* operation, 62 int http_code, 63 const base::DictionaryValue* json_value) OVERRIDE; 64 65 private: 66 struct DeviceContext { 67 DeviceContext(); 68 ~DeviceContext(); 69 70 bool notification_may_be_active; 71 bool registered; 72 std::string human_readable_name; 73 std::string description; 74 scoped_ptr<PrivetInfoOperation> info_operation; 75 scoped_ptr<PrivetHTTPAsynchronousFactory::Resolution> 76 privet_http_resolution; 77 scoped_ptr<PrivetHTTPClient> privet_http; 78 }; 79 80 typedef std::map<std::string, linked_ptr<DeviceContext> > DeviceContextMap; 81 82 void CreateInfoOperation(scoped_ptr<PrivetHTTPClient> http_client); 83 84 Delegate* delegate_; 85 scoped_ptr<PrivetDeviceLister> device_lister_; 86 scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_; 87 DeviceContextMap devices_seen_; 88}; 89 90class PrivetNotificationService 91 : public BrowserContextKeyedService, 92 public PrivetDeviceLister::Delegate, 93 public PrivetNotificationsListener::Delegate, 94 public base::SupportsWeakPtr<PrivetNotificationService> { 95 public: 96 PrivetNotificationService(content::BrowserContext* profile, 97 NotificationUIManager* notification_manager); 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 117 content::BrowserContext* profile_; 118 NotificationUIManager* notification_manager_; 119 scoped_ptr<PrivetDeviceLister> device_lister_; 120 scoped_refptr<ServiceDiscoveryHostClient> service_discovery_client_; 121 scoped_ptr<PrivetNotificationsListener> privet_notifications_listener_; 122}; 123 124class PrivetNotificationDelegate : public NotificationDelegate { 125 public: 126 explicit PrivetNotificationDelegate(const std::string& device_id, 127 content::BrowserContext* profile); 128 129 // NotificationDelegate implementation. 130 virtual std::string id() const OVERRIDE; 131 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE; 132 virtual void Display() OVERRIDE; 133 virtual void Error() OVERRIDE; 134 virtual void Close(bool by_user) OVERRIDE; 135 virtual void Click() OVERRIDE; 136 virtual void ButtonClick(int button_index) OVERRIDE; 137 138 private: 139 void OpenTab(const GURL& url); 140 141 virtual ~PrivetNotificationDelegate(); 142 143 std::string device_id_; 144 content::BrowserContext* profile_; 145}; 146 147} // namespace local_discovery 148 149#endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_ 150