summaryrefslogtreecommitdiff
path: root/chromium/ui/base/accelerators/system_media_controls_media_keys_listener.h
blob: 847669a515e5f6782f65c356ed57d5aaf1ee7acd (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
// Copyright 2019 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 UI_BASE_ACCELERATORS_SYSTEM_MEDIA_CONTROLS_MEDIA_KEYS_LISTENER_H_
#define UI_BASE_ACCELERATORS_SYSTEM_MEDIA_CONTROLS_MEDIA_KEYS_LISTENER_H_

#include "base/component_export.h"
#include "base/containers/flat_set.h"
#include "components/system_media_controls/system_media_controls_observer.h"
#include "ui/base/accelerators/media_keys_listener.h"
#include "ui/events/keycodes/keyboard_codes.h"

namespace system_media_controls {
class SystemMediaControls;
}  // namespace system_media_controls

namespace ui {

// Implementation of MediaKeysListener that uses the SystemMediaControls API to
// listen for media key presses. It only allows for a single instance to be
// created in order to prevent conflicts from multiple listeners.
class COMPONENT_EXPORT(UI_BASE) SystemMediaControlsMediaKeysListener
    : public MediaKeysListener,
      public system_media_controls::SystemMediaControlsObserver {
 public:
  explicit SystemMediaControlsMediaKeysListener(
      MediaKeysListener::Delegate* delegate);
  ~SystemMediaControlsMediaKeysListener() override;

  static bool has_instance() { return has_instance_; }

  bool Initialize();

  // MediaKeysListener implementation.
  bool StartWatchingMediaKey(KeyboardCode key_code) override;
  void StopWatchingMediaKey(KeyboardCode key_code) override;
  void SetIsMediaPlaying(bool is_playing) override;

  // system_media_controls::SystemMediaControlsObserver implementation.
  void OnServiceReady() override {}
  void OnNext() override;
  void OnPrevious() override;
  void OnPlay() override;
  void OnPause() override;
  void OnPlayPause() override;
  void OnStop() override;

  void SetSystemMediaControlsForTesting(
      system_media_controls::SystemMediaControls* service) {
    service_ = service;
  }

 private:
  static bool has_instance_;

  // Sends the key code to the delegate if the delegate has asked for it.
  void MaybeSendKeyCode(KeyboardCode key_code);

  MediaKeysListener::Delegate* delegate_;

  // Set of keys codes that we're currently listening for.
  base::flat_set<KeyboardCode> key_codes_;

  system_media_controls::SystemMediaControls* service_ = nullptr;

  bool is_media_playing_ = false;

  DISALLOW_COPY_AND_ASSIGN(SystemMediaControlsMediaKeysListener);
};

}  // namespace ui

#endif  // UI_BASE_ACCELERATORS_SYSTEM_MEDIA_CONTROLS_MEDIA_KEYS_LISTENER_H_