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.
5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_
11 #include "base/prefs/pref_member.h"
12 #include "chrome/browser/local_discovery/privet_device_lister.h"
13 #include "chrome/browser/local_discovery/privet_http.h"
14 #include "chrome/browser/notifications/notification_delegate.h"
15 #include "components/keyed_service/core/keyed_service.h"
17 class NotificationUIManager
;
21 } // namespace content
23 namespace local_discovery
{
25 class ServiceDiscoverySharedClient
;
26 class PrivetDeviceLister
;
27 class PrivetHTTPAsynchronousFactory
;
28 class PrivetHTTPResolution
;
29 struct DeviceDescription
;
31 #if defined(ENABLE_MDNS)
32 class PrivetTrafficDetector
;
35 // Contains logic related to notifications not tied actually displaying them.
36 class PrivetNotificationsListener
{
40 virtual ~Delegate() {}
42 // Notify user of the existence of device |device_name|.
43 virtual void PrivetNotify(int devices_active
, bool added
) = 0;
45 // Remove the noitification for |device_name| if it still exists.
46 virtual void PrivetRemoveNotification() = 0;
49 PrivetNotificationsListener(
50 scoped_ptr
<PrivetHTTPAsynchronousFactory
> privet_http_factory
,
52 virtual ~PrivetNotificationsListener();
54 // These two methods are akin to those of PrivetDeviceLister::Delegate. The
55 // user of PrivetNotificationListener should create a PrivetDeviceLister and
56 // forward device notifications to the PrivetNotificationLister.
57 void DeviceChanged(bool added
,
58 const std::string
& name
,
59 const DeviceDescription
& description
);
60 void DeviceRemoved(const std::string
& name
);
61 virtual void DeviceCacheFlushed();
64 struct DeviceContext
{
68 bool notification_may_be_active
;
70 scoped_ptr
<PrivetJSONOperation
> info_operation
;
71 scoped_ptr
<PrivetHTTPResolution
> privet_http_resolution
;
72 scoped_ptr
<PrivetHTTPClient
> privet_http
;
75 typedef std::map
<std::string
, linked_ptr
<DeviceContext
> > DeviceContextMap
;
77 void CreateInfoOperation(scoped_ptr
<PrivetHTTPClient
> http_client
);
78 void OnPrivetInfoDone(DeviceContext
* device
,
79 const base::DictionaryValue
* json_value
);
82 void NotifyDeviceRemoved();
85 scoped_ptr
<PrivetDeviceLister
> device_lister_
;
86 scoped_ptr
<PrivetHTTPAsynchronousFactory
> privet_http_factory_
;
87 DeviceContextMap devices_seen_
;
91 class PrivetNotificationService
92 : public KeyedService
,
93 public PrivetDeviceLister::Delegate
,
94 public PrivetNotificationsListener::Delegate
,
95 public base::SupportsWeakPtr
<PrivetNotificationService
> {
97 explicit PrivetNotificationService(content::BrowserContext
* profile
);
98 ~PrivetNotificationService() override
;
100 // PrivetDeviceLister::Delegate implementation:
101 void DeviceChanged(bool added
,
102 const std::string
& name
,
103 const DeviceDescription
& description
) override
;
104 void DeviceRemoved(const std::string
& name
) override
;
106 // PrivetNotificationListener::Delegate implementation:
107 void PrivetNotify(int devices_active
, bool added
) override
;
109 void PrivetRemoveNotification() override
;
110 void DeviceCacheFlushed() override
;
112 static bool IsEnabled();
113 static bool IsForced();
117 void OnNotificationsEnabledChanged();
120 content::BrowserContext
* profile_
;
121 scoped_ptr
<PrivetDeviceLister
> device_lister_
;
122 scoped_refptr
<ServiceDiscoverySharedClient
> service_discovery_client_
;
123 scoped_ptr
<PrivetNotificationsListener
> privet_notifications_listener_
;
124 BooleanPrefMember enable_privet_notification_member_
;
126 #if defined(ENABLE_MDNS)
127 scoped_refptr
<PrivetTrafficDetector
> traffic_detector_
;
128 #endif // ENABLE_MDNS
131 class PrivetNotificationDelegate
: public NotificationDelegate
{
133 explicit PrivetNotificationDelegate(content::BrowserContext
* profile
);
135 // NotificationDelegate implementation.
136 std::string
id() const override
;
137 void ButtonClick(int button_index
) override
;
140 void OpenTab(const GURL
& url
);
141 void DisableNotifications();
143 ~PrivetNotificationDelegate() override
;
145 content::BrowserContext
* profile_
;
148 } // namespace local_discovery
150 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_