summaryrefslogtreecommitdiffstats
path: root/chrome/browser/local_discovery/privet_notifications.h
blob: 7bbb771dcdb7c3ac7671860a30fd8babf1252417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_
#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_

#include <map>
#include <string>

#include "base/prefs/pref_member.h"
#include "chrome/browser/local_discovery/privet_device_lister.h"
#include "chrome/browser/local_discovery/privet_http.h"
#include "chrome/browser/notifications/notification_delegate.h"
#include "components/browser_context_keyed_service/browser_context_keyed_service.h"

class NotificationUIManager;

namespace content {
class BrowserContext;
}  // namespace content

namespace local_discovery {

class ServiceDiscoverySharedClient;
class PrivetDeviceLister;
class PrivetHTTPAsynchronousFactory;
class PrivetHTTPResolution;
class PrivetTrafficDetector;
struct DeviceDescription;

// Contains logic related to notifications not tied actually displaying them.
class PrivetNotificationsListener  {
 public:
  class Delegate {
   public:
    virtual ~Delegate() {}

    // Notify user of the existence of device |device_name|.
    virtual void PrivetNotify(bool multiple, bool added) = 0;

    // Remove the noitification for |device_name| if it still exists.
    virtual void PrivetRemoveNotification() = 0;
  };

  PrivetNotificationsListener(
      scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory,
      Delegate* delegate);
  virtual ~PrivetNotificationsListener();

  // These two methods are akin to those of PrivetDeviceLister::Delegate. The
  // user of PrivetNotificationListener should create a PrivetDeviceLister and
  // forward device notifications to the PrivetNotificationLister.
  void DeviceChanged(bool added,
                     const std::string& name,
                     const DeviceDescription& description);
  void DeviceRemoved(const std::string& name);
  virtual void DeviceCacheFlushed();

 private:
  struct DeviceContext {
    DeviceContext();
    ~DeviceContext();

    bool notification_may_be_active;
    bool registered;
    scoped_ptr<PrivetJSONOperation> info_operation;
    scoped_ptr<PrivetHTTPResolution> privet_http_resolution;
    scoped_ptr<PrivetHTTPClient> privet_http;
  };

  typedef std::map<std::string, linked_ptr<DeviceContext> > DeviceContextMap;

  void CreateInfoOperation(scoped_ptr<PrivetHTTPClient> http_client);
  void OnPrivetInfoDone(DeviceContext* device,
                        const base::DictionaryValue* json_value);


  void NotifyDeviceRemoved();

  Delegate* delegate_;
  scoped_ptr<PrivetDeviceLister> device_lister_;
  scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_;
  DeviceContextMap devices_seen_;
  int devices_active_;
};

class PrivetNotificationService
    : public BrowserContextKeyedService,
      public PrivetDeviceLister::Delegate,
      public PrivetNotificationsListener::Delegate,
      public base::SupportsWeakPtr<PrivetNotificationService> {
 public:
  explicit PrivetNotificationService(content::BrowserContext* profile);
  virtual ~PrivetNotificationService();

  // PrivetDeviceLister::Delegate implementation:
  virtual void DeviceChanged(bool added, const std::string& name,
                             const DeviceDescription& description) OVERRIDE;
  virtual void DeviceRemoved(const std::string& name) OVERRIDE;

  // PrivetNotificationListener::Delegate implementation:
  virtual void PrivetNotify(bool has_multiple, bool added) OVERRIDE;

  virtual void PrivetRemoveNotification() OVERRIDE;
  virtual void DeviceCacheFlushed() OVERRIDE;

  static bool IsEnabled();
  static bool IsForced();

 private:
  void Start();
  void OnNotificationsEnabledChanged();
  void StartLister();

  content::BrowserContext* profile_;
  scoped_ptr<PrivetDeviceLister> device_lister_;
  scoped_refptr<ServiceDiscoverySharedClient> service_discovery_client_;
  scoped_refptr<PrivetTrafficDetector> traffic_detector_;
  scoped_ptr<PrivetNotificationsListener> privet_notifications_listener_;
  BooleanPrefMember enable_privet_notification_member_;
};

class PrivetNotificationDelegate : public NotificationDelegate {
 public:
  explicit PrivetNotificationDelegate(content::BrowserContext* profile);

  // NotificationDelegate implementation.
  virtual std::string id() const OVERRIDE;
  virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE;
  virtual void Display() OVERRIDE;
  virtual void Error() OVERRIDE;
  virtual void Close(bool by_user) OVERRIDE;
  virtual void Click() OVERRIDE;
  virtual void ButtonClick(int button_index) OVERRIDE;

 private:
  void OpenTab(const GURL& url);
  void DisableNotifications();

  virtual ~PrivetNotificationDelegate();

  content::BrowserContext* profile_;
};

}  // namespace local_discovery

#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_NOTIFICATIONS_H_