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 host a scoped_refptr must inherit from the Observer class. | |
mark a. foltz
2017/03/20 23:47:03
Did you mean hold instead of host?
takumif
2017/03/21 04:00:07
Yes, changed.
| |
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 // The pointer returned by this getter should not be stored, as it can be | |
mark a. foltz
2017/03/20 23:47:03
Returns a reference to the observed MediaRouteCont
takumif
2017/03/21 04:00:07
Done.
| |
48 // invalidated at any time. | |
49 MediaRouteController* controller() { return controller_.get(); } | |
mark a. foltz
2017/03/20 23:47:03
Adding a comment here is not sufficient. There's
takumif
2017/03/21 04:00:07
Done.
| |
50 | |
51 private: | |
52 friend class MediaRouteController; | |
53 | |
54 // Disposes the reference to the controller. Must be called by methods | |
55 // overriding it. | |
mark a. foltz
2017/03/20 23:47:03
Didn't follow the second sentence - this method is
takumif
2017/03/21 04:00:07
Sorry, it's out of date. Removed.
| |
56 void OnControllerInvalidated(); | |
57 | |
58 // Called by OnControllerInvalidated() after the reference to the controller | |
59 // is disposed. Overridden by subclasses to do custom cleanup. | |
mark a. foltz
2017/03/20 23:47:03
Perhaps note that this is called immediately befor
takumif
2017/03/21 04:00:07
This is called after |controller_| is reset, so th
| |
60 virtual void OnControllerDisposed(); | |
61 | |
62 scoped_refptr<MediaRouteController> controller_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(Observer); | |
65 }; | |
66 | |
67 // |this| will forward media commands to |media_controller|. | |
mark a. foltz
2017/03/20 23:47:03
Constructs a MediaRouteController that forwards...
takumif
2017/03/21 04:00:07
Done.
| |
68 // |media_controller| must be bound to a message pipe. |destructor_callback| | |
mark a. foltz
2017/03/20 23:47:03
Remove |destructor_callback| comment
takumif
2017/03/21 04:00:07
Done.
| |
69 // will be called in the dtor of |this|. | |
70 MediaRouteController(const MediaRoute::Id& route_id, | |
71 mojom::MediaControllerPtr media_controller); | |
72 | |
73 // Media controller methods for forwarding commands to a | |
74 // mojom::MediaControllerPtr held in |media_controller_|. | |
75 void Play(); | |
76 void Pause(); | |
77 void Seek(base::TimeDelta time); | |
78 void SetMute(bool mute); | |
79 void SetVolume(float volume); | |
80 | |
81 // mojom::MediaStatusObserver: | |
82 // Notifies |observers_| of a status update. | |
83 void OnMediaStatusUpdated(const MediaStatus& status) override; | |
84 | |
85 // Called when the connection between |this| and |media_controller_| is no | |
86 // longer valid. Notifies |observers_| to dispose their references to |this|. | |
87 // |this| gets destroyed when all the references are disposed. | |
88 void OnControllerInvalidated(); | |
89 | |
90 MediaRoute::Id route_id() const { return route_id_; } | |
91 | |
92 private: | |
93 friend class base::RefCounted<MediaRouteController>; | |
94 | |
95 ~MediaRouteController() override; | |
96 | |
97 void AddObserver(Observer* observer); | |
98 void RemoveObserver(Observer* observer); | |
99 | |
100 // The ID of the Media Route that |this| controls. | |
101 const MediaRoute::Id route_id_; | |
102 | |
103 // The out-of-process controller that |this| forwards commands to. | |
mark a. foltz
2017/03/20 23:47:03
Technically, mojo services can be bound in-process
takumif
2017/03/21 04:00:07
Done.
| |
104 mojom::MediaControllerPtr media_controller_; | |
105 | |
106 // Observers that |this| notifies of status updates. The observers share the | |
107 // ownership of |this| through scoped_refptr. | |
108 base::ObserverList<Observer> observers_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(MediaRouteController); | |
111 }; | |
112 | |
113 } // namespace media_router | |
114 | |
115 #endif // CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTE_CONTROLLER_H_ | |
OLD | NEW |