• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
16 
17 class NotificationUIManager;
18 
19 namespace content {
20 class BrowserContext;
21 }  // namespace content
22 
23 namespace local_discovery {
24 
25 class ServiceDiscoverySharedClient;
26 class PrivetDeviceLister;
27 class PrivetHTTPAsynchronousFactory;
28 class PrivetHTTPResolution;
29 class PrivetTrafficDetector;
30 struct DeviceDescription;
31 
32 // Contains logic related to notifications not tied actually displaying them.
33 class PrivetNotificationsListener : public PrivetInfoOperation::Delegate {
34  public:
35   class Delegate {
36    public:
~Delegate()37     virtual ~Delegate() {}
38 
39     // Notify user of the existence of device |device_name|.
40     virtual void PrivetNotify(bool multiple, bool added) = 0;
41 
42     // Remove the noitification for |device_name| if it still exists.
43     virtual void PrivetRemoveNotification() = 0;
44   };
45 
46   PrivetNotificationsListener(
47       scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory,
48       Delegate* delegate);
49   virtual ~PrivetNotificationsListener();
50 
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();
59 
60   // PrivetInfoOperation::Delegate implementation.
61   virtual void OnPrivetInfoDone(
62       PrivetInfoOperation* operation,
63       int http_code,
64       const base::DictionaryValue* json_value) OVERRIDE;
65 
66  private:
67   struct DeviceContext {
68     DeviceContext();
69     ~DeviceContext();
70 
71     bool notification_may_be_active;
72     bool registered;
73     scoped_ptr<PrivetInfoOperation> info_operation;
74     scoped_ptr<PrivetHTTPResolution> privet_http_resolution;
75     scoped_ptr<PrivetHTTPClient> privet_http;
76   };
77 
78   typedef std::map<std::string, linked_ptr<DeviceContext> > DeviceContextMap;
79 
80   void CreateInfoOperation(scoped_ptr<PrivetHTTPClient> http_client);
81 
82   void NotifyDeviceRemoved();
83 
84   Delegate* delegate_;
85   scoped_ptr<PrivetDeviceLister> device_lister_;
86   scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_;
87   DeviceContextMap devices_seen_;
88   int devices_active_;
89 };
90 
91 class 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(bool has_multiple, bool added) OVERRIDE;
107 
108   virtual void PrivetRemoveNotification() OVERRIDE;
109   virtual void DeviceCacheFlushed() OVERRIDE;
110 
111   static bool IsEnabled();
112   static bool IsForced();
113 
114  private:
115   void Start();
116   void OnNotificationsEnabledChanged();
117   void StartLister();
118 
119   content::BrowserContext* profile_;
120   scoped_ptr<PrivetDeviceLister> device_lister_;
121   scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_;
122   scoped_refptr<PrivetTrafficDetector> traffic_detector_;
123   scoped_ptr<PrivetNotificationsListener> privet_notifications_listener_;
124   BooleanPrefMember enable_privet_notification_member_;
125 };
126 
127 class PrivetNotificationDelegate : public NotificationDelegate {
128  public:
129   explicit PrivetNotificationDelegate(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   void DisableNotifications();
143 
144   virtual ~PrivetNotificationDelegate();
145 
146   content::BrowserContext* profile_;
147 };
148 
149 }  // namespace local_discovery
150 
151 #endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_
152