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/browser_context_keyed_service/browser_context_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 class PrivetTrafficDetector
;
30 struct DeviceDescription
;
32 // Contains logic related to notifications not tied actually displaying them.
33 class PrivetNotificationsListener
{
37 virtual ~Delegate() {}
39 // Notify user of the existence of device |device_name|.
40 virtual void PrivetNotify(bool multiple
, bool added
) = 0;
42 // Remove the noitification for |device_name| if it still exists.
43 virtual void PrivetRemoveNotification() = 0;
46 PrivetNotificationsListener(
47 scoped_ptr
<PrivetHTTPAsynchronousFactory
> privet_http_factory
,
49 virtual ~PrivetNotificationsListener();
51 // These two methods are akin to those of PrivetDeviceLister::Delegate. The
52 // user of PrivetNotificationListener should create a PrivetDeviceLister and
53 // forward device notifications to the PrivetNotificationLister.
54 void DeviceChanged(bool added
,
55 const std::string
& name
,
56 const DeviceDescription
& description
);
57 void DeviceRemoved(const std::string
& name
);
58 virtual void DeviceCacheFlushed();
61 struct DeviceContext
{
65 bool notification_may_be_active
;
67 scoped_ptr
<PrivetJSONOperation
> info_operation
;
68 scoped_ptr
<PrivetHTTPResolution
> privet_http_resolution
;
69 scoped_ptr
<PrivetHTTPClient
> privet_http
;
72 typedef std::map
<std::string
, linked_ptr
<DeviceContext
> > DeviceContextMap
;
74 void CreateInfoOperation(scoped_ptr
<PrivetHTTPClient
> http_client
);
75 void OnPrivetInfoDone(DeviceContext
* device
,
76 const base::DictionaryValue
* json_value
);
79 void NotifyDeviceRemoved();
82 scoped_ptr
<PrivetDeviceLister
> device_lister_
;
83 scoped_ptr
<PrivetHTTPAsynchronousFactory
> privet_http_factory_
;
84 DeviceContextMap devices_seen_
;
88 class PrivetNotificationService
89 : public BrowserContextKeyedService
,
90 public PrivetDeviceLister::Delegate
,
91 public PrivetNotificationsListener::Delegate
,
92 public base::SupportsWeakPtr
<PrivetNotificationService
> {
94 explicit PrivetNotificationService(content::BrowserContext
* profile
);
95 virtual ~PrivetNotificationService();
97 // PrivetDeviceLister::Delegate implementation:
98 virtual void DeviceChanged(bool added
, const std::string
& name
,
99 const DeviceDescription
& description
) OVERRIDE
;
100 virtual void DeviceRemoved(const std::string
& name
) OVERRIDE
;
102 // PrivetNotificationListener::Delegate implementation:
103 virtual void PrivetNotify(bool has_multiple
, bool added
) OVERRIDE
;
105 virtual void PrivetRemoveNotification() OVERRIDE
;
106 virtual void DeviceCacheFlushed() OVERRIDE
;
108 static bool IsEnabled();
109 static bool IsForced();
113 void OnNotificationsEnabledChanged();
116 content::BrowserContext
* profile_
;
117 scoped_ptr
<PrivetDeviceLister
> device_lister_
;
118 scoped_refptr
<ServiceDiscoverySharedClient
> service_discovery_client_
;
119 scoped_refptr
<PrivetTrafficDetector
> traffic_detector_
;
120 scoped_ptr
<PrivetNotificationsListener
> privet_notifications_listener_
;
121 BooleanPrefMember enable_privet_notification_member_
;
124 class PrivetNotificationDelegate
: public NotificationDelegate
{
126 explicit PrivetNotificationDelegate(content::BrowserContext
* profile
);
128 // NotificationDelegate implementation.
129 virtual std::string
id() const OVERRIDE
;
130 virtual content::RenderViewHost
* GetRenderViewHost() const OVERRIDE
;
131 virtual void Display() OVERRIDE
;
132 virtual void Error() OVERRIDE
;
133 virtual void Close(bool by_user
) OVERRIDE
;
134 virtual void Click() OVERRIDE
;
135 virtual void ButtonClick(int button_index
) OVERRIDE
;
138 void OpenTab(const GURL
& url
);
139 void DisableNotifications();
141 virtual ~PrivetNotificationDelegate();
143 content::BrowserContext
* profile_
;
146 } // namespace local_discovery
148 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_