summaryrefslogtreecommitdiffstats
path: root/extensions/browser/api/display_source/display_source_event_router.h
blob: 97e2e6f20b55b9520719a4658f9bff4e804eca5a (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
// Copyright 2015 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 EXTENSIONS_BROWSER_API_DISPLAY_SOURCE_DISPLAY_SOURCE_EVENT_ROUTER_H_
#define EXTENSIONS_BROWSER_API_DISPLAY_SOURCE_DISPLAY_SOURCE_EVENT_ROUTER_H_

#include "base/macros.h"
#include "components/keyed_service/core/keyed_service.h"
#include "extensions/browser/api/display_source/display_source_connection_delegate.h"
#include "extensions/browser/event_router.h"

namespace content {
class BrowserContext;
}

namespace extensions {

// Observe listeners to 'onSinksUpdated' events.
// This class initiates/stops listening for the available sinks
// by the DisplaySourceConnectionDelegate, depending on whether
// there are existing listeners to 'onSinksUpdated' event.
class DisplaySourceEventRouter
    : public KeyedService,
      public EventRouter::Observer,
      public DisplaySourceConnectionDelegate::Observer {
 public:
  static DisplaySourceEventRouter* Create(
      content::BrowserContext* browser_context);

  ~DisplaySourceEventRouter() override;

 private:
  explicit DisplaySourceEventRouter(content::BrowserContext* browser_context);

  // KeyedService overrides:
  void Shutdown() override;

  // EventRouter::Observer overrides:
  void OnListenerAdded(const EventListenerInfo& details) override;
  void OnListenerRemoved(const EventListenerInfo& details) override;

  // DisplaySourceConnectionDelegate::Observer overrides:
  void OnSinksUpdated(const DisplaySourceSinkInfoList& sinks) override;
  void OnConnectionError(int sink_id,
                         DisplaySourceErrorType type,
                         const std::string& description) override {}

 private:
  void StartOrStopListeningForSinksChanges();

  content::BrowserContext* browser_context_;
  bool listening_;

  DISALLOW_COPY_AND_ASSIGN(DisplaySourceEventRouter);
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_DISPLAY_SOURCE_DISPLAY_SOURCE_EVENT_ROUTER_H_