OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_MEDIA_ROUTER_MOJO_MEDIA_ROUTE_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTE_CONTROLLER_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/observer_list.h" |
| 12 #include "chrome/browser/media/router/media_route.h" |
| 13 #include "chrome/browser/media/router/mojo/media_controller.mojom.h" |
| 14 #include "mojo/public/cpp/bindings/binding.h" |
| 15 |
| 16 namespace media_router { |
| 17 |
| 18 // A controller for a MediaRoute. Forwards commands for controlling the route to |
| 19 // an out-of-process controller. Notifies its observers whenever there is a |
| 20 // change in the route's MediaStatus. |
| 21 // |
| 22 // It is owned by its observers, each of which holds a scoped_refptr to it. All |
| 23 // the classes that hold a scoped_refptr must inherit from the Observer class. |
| 24 // An observer should be instantiated with a scoped_refptr obtained through |
| 25 // MediaRouter::GetRouteController(). |
| 26 // |
| 27 // A MediaRouteController instance is destroyed when all its observers dispose |
| 28 // their references to it. When the Mojo connection with the out-of-process |
| 29 // controller is terminated or has an error, OnControllerInvalidated() will be |
| 30 // called by the MediaRouter or a Mojo error handler to make observers dispose |
| 31 // their refptrs. |
| 32 class MediaRouteController : public mojom::MediaStatusObserver, |
| 33 public base::RefCounted<MediaRouteController> { |
| 34 public: |
| 35 // Observes MediaRouteController for MediaStatus updates. The ownership of a |
| 36 // MediaRouteController is shared by its observers. |
| 37 class Observer { |
| 38 public: |
| 39 // Adds itself as an observer to |controller|. |
| 40 explicit Observer(scoped_refptr<MediaRouteController> controller); |
| 41 |
| 42 // Removes itself as an observer if |controller_| is still valid. |
| 43 virtual ~Observer(); |
| 44 |
| 45 virtual void OnMediaStatusUpdated(const MediaStatus& status) = 0; |
| 46 |
| 47 // Returns a reference to the observed MediaRouteController. The reference |
| 48 // should not be stored by an object of a class that does not inherit from |
| 49 // Observer. |
| 50 scoped_refptr<MediaRouteController> controller() const { |
| 51 return controller_; |
| 52 } |
| 53 |
| 54 private: |
| 55 friend class MediaRouteController; |
| 56 |
| 57 // Disposes the reference to the controller. |
| 58 void InvalidateController(); |
| 59 |
| 60 // Called by InvalidateController() after the reference to the controller is |
| 61 // disposed. Overridden by subclasses to do custom cleanup. |
| 62 virtual void OnControllerInvalidated(); |
| 63 |
| 64 scoped_refptr<MediaRouteController> controller_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(Observer); |
| 67 }; |
| 68 |
| 69 // Constructs a MediaRouteController that forwards media commands to |
| 70 // |media_controller|. |media_controller| must be bound to a message pipe. |
| 71 MediaRouteController(const MediaRoute::Id& route_id, |
| 72 mojom::MediaControllerPtr media_controller); |
| 73 |
| 74 // Media controller methods for forwarding commands to a |
| 75 // mojom::MediaControllerPtr held in |media_controller_|. |
| 76 void Play(); |
| 77 void Pause(); |
| 78 void Seek(base::TimeDelta time); |
| 79 void SetMute(bool mute); |
| 80 void SetVolume(float volume); |
| 81 |
| 82 // mojom::MediaStatusObserver: |
| 83 // Notifies |observers_| of a status update. |
| 84 void OnMediaStatusUpdated(const MediaStatus& status) override; |
| 85 |
| 86 // Called when the connection between |this| and |media_controller_| is no |
| 87 // longer valid. Notifies |observers_| to dispose their references to |this|. |
| 88 // |this| gets destroyed when all the references are disposed. |
| 89 void Invalidate(); |
| 90 |
| 91 MediaRoute::Id route_id() const { return route_id_; } |
| 92 |
| 93 private: |
| 94 friend class base::RefCounted<MediaRouteController>; |
| 95 |
| 96 ~MediaRouteController() override; |
| 97 |
| 98 void AddObserver(Observer* observer); |
| 99 void RemoveObserver(Observer* observer); |
| 100 |
| 101 // The ID of the Media Route that |this| controls. |
| 102 const MediaRoute::Id route_id_; |
| 103 |
| 104 // Handle to the mojom::MediaController that receives media commands. |
| 105 mojom::MediaControllerPtr media_controller_; |
| 106 |
| 107 // Observers that |this| notifies of status updates. The observers share the |
| 108 // ownership of |this| through scoped_refptr. |
| 109 base::ObserverList<Observer> observers_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(MediaRouteController); |
| 112 }; |
| 113 |
| 114 } // namespace media_router |
| 115 |
| 116 #endif // CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTE_CONTROLLER_H_ |
OLD | NEW |