1 // Copyright 2018 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 UI_BASE_ACCELERATORS_MEDIA_KEYS_LISTENER_H_ 6 #define UI_BASE_ACCELERATORS_MEDIA_KEYS_LISTENER_H_ 7 8 #include <memory> 9 10 #include "base/callback.h" 11 #include "base/observer_list_types.h" 12 #include "ui/base/ui_base_export.h" 13 #include "ui/events/keycodes/keyboard_codes.h" 14 15 namespace ui { 16 17 class Accelerator; 18 19 // Create MediaKeyListener to receive accelerators on media keys. 20 class UI_BASE_EXPORT MediaKeysListener { 21 public: 22 enum class Scope { 23 kGlobal, // Listener works whenever application in focus or not. 24 kFocused, // Listener only works whan application has focus. 25 }; 26 27 // Media keys accelerators receiver. 28 class UI_BASE_EXPORT Delegate : public base::CheckedObserver { 29 public: 30 ~Delegate() override; 31 32 // Called on media key event. 33 virtual void OnMediaKeysAccelerator(const Accelerator& accelerator) = 0; 34 }; 35 36 // Can return nullptr if media keys listening is not implemented. 37 // Currently implemented only on mac. 38 static std::unique_ptr<MediaKeysListener> Create(Delegate* delegate, 39 Scope scope); 40 41 static bool IsMediaKeycode(KeyboardCode key_code); 42 43 virtual ~MediaKeysListener(); 44 45 // Start listening for a given media key. Returns true if the listener 46 // successfully started listening for the key. Some implementations may not be 47 // able to register if another application is already listening to the media 48 // key. 49 virtual bool StartWatchingMediaKey(KeyboardCode key_code) = 0; 50 // Stop listening for a given media key. 51 virtual void StopWatchingMediaKey(KeyboardCode key_code) = 0; 52 53 // Informs the listener whether or not media is currently playing. In some 54 // implementations this will prevent us from calling PlayPause unnecessarily. 55 // TODO(https://crbug.com/974035): Once the MediaKeysListenerManager has been 56 // refactored to work with system media controls this should no longer be 57 // needed and should be deleted. 58 virtual void SetIsMediaPlaying(bool is_playing) = 0; 59 }; 60 61 } // namespace ui 62 63 #endif // UI_BASE_ACCELERATORS_MEDIA_KEYS_LISTENER_H_ 64