privet_notifications.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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/keyed_service/core/keyed_service.h"
16
17class NotificationUIManager;
18
19namespace content {
20class BrowserContext;
21}  // namespace content
22
23namespace local_discovery {
24
25class ServiceDiscoverySharedClient;
26class PrivetDeviceLister;
27class PrivetHTTPAsynchronousFactory;
28class PrivetHTTPResolution;
29class PrivetTrafficDetector;
30struct DeviceDescription;
31
32// Contains logic related to notifications not tied actually displaying them.
33class PrivetNotificationsListener  {
34 public:
35  class Delegate {
36   public:
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 private:
61  struct DeviceContext {
62    DeviceContext();
63    ~DeviceContext();
64
65    bool notification_may_be_active;
66    bool registered;
67    scoped_ptr<PrivetJSONOperation> info_operation;
68    scoped_ptr<PrivetHTTPResolution> privet_http_resolution;
69    scoped_ptr<PrivetHTTPClient> privet_http;
70  };
71
72  typedef std::map<std::string, linked_ptr<DeviceContext> > DeviceContextMap;
73
74  void CreateInfoOperation(scoped_ptr<PrivetHTTPClient> http_client);
75  void OnPrivetInfoDone(DeviceContext* device,
76                        const base::DictionaryValue* json_value);
77
78
79  void NotifyDeviceRemoved();
80
81  Delegate* delegate_;
82  scoped_ptr<PrivetDeviceLister> device_lister_;
83  scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_;
84  DeviceContextMap devices_seen_;
85  int devices_active_;
86};
87
88class PrivetNotificationService
89    : public KeyedService,
90      public PrivetDeviceLister::Delegate,
91      public PrivetNotificationsListener::Delegate,
92      public base::SupportsWeakPtr<PrivetNotificationService> {
93 public:
94  explicit PrivetNotificationService(content::BrowserContext* profile);
95  virtual ~PrivetNotificationService();
96
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;
101
102  // PrivetNotificationListener::Delegate implementation:
103  virtual void PrivetNotify(bool has_multiple, bool added) OVERRIDE;
104
105  virtual void PrivetRemoveNotification() OVERRIDE;
106  virtual void DeviceCacheFlushed() OVERRIDE;
107
108  static bool IsEnabled();
109  static bool IsForced();
110
111 private:
112  void Start();
113  void OnNotificationsEnabledChanged();
114  void StartLister();
115
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_;
122};
123
124class PrivetNotificationDelegate : public NotificationDelegate {
125 public:
126  explicit PrivetNotificationDelegate(content::BrowserContext* profile);
127
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;
136
137 private:
138  void OpenTab(const GURL& url);
139  void DisableNotifications();
140
141  virtual ~PrivetNotificationDelegate();
142
143  content::BrowserContext* profile_;
144};
145
146}  // namespace local_discovery
147
148#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_
149