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/observer_list.h" | |
mark a. foltz
2017/03/17 23:55:04
#include base/memory/ref_counted.h
takumif
2017/03/20 22:10:45
Done.
| |
11 #include "chrome/browser/media/router/media_route.h" | |
12 #include "chrome/browser/media/router/mojo/media_controller.mojom.h" | |
13 #include "mojo/public/cpp/bindings/binding.h" | |
14 | |
15 namespace media_router { | |
16 | |
17 // A controller for a MediaRoute. Forwards commands for controlling the route to | |
18 // an out-of-process controller. Notifies its observers whenever there is a | |
19 // change in the route's MediaStatus. | |
20 // | |
21 // It is owned by its observers, each of which holds a scoped_refptr. All the | |
mark a. foltz
2017/03/17 23:55:04
...scoped_refptr to it.
takumif
2017/03/20 22:10:45
Done.
| |
22 // classes that share the ownership must inherit from the Observer class. A | |
mark a. foltz
2017/03/17 23:55:04
that hold a scoped_refptr must
takumif
2017/03/20 22:10:45
Done.
| |
23 // MediaRouteController instance is destroyed when all its observers dispose | |
24 // their references to it. | |
25 // | |
26 // Example usage: | |
27 // | |
28 // Instantiate MediaRouteController and pass it into the ctor of an observer. | |
29 // An observer will subscribe itself in its ctor, and unsubscribe in its dtor. | |
30 // | |
31 // // Let ObserverImpl extend MediaRouteController::Observer. | |
imcheng
2017/03/18 00:11:32
If we are calling this MediaRouteController and ad
takumif
2017/03/20 22:10:45
Removing the example code, and mentioning that an
| |
32 // // Let |media_controller| be bound to a message pipe. | |
33 // // Let |destructor_callback| be a closure. | |
34 // ObserverImpl observer1(new MediaRouteController( | |
35 // "some route id", media_controller, destructor_callback)); | |
36 // ObserverImpl observer2(observer1.controller()); | |
mark a. foltz
2017/03/17 23:55:04
How about:
MRC::Observer* observer;
{
scoped_refp
takumif
2017/03/20 22:10:45
I think it's better if MRController didn't know ab
mark a. foltz
2017/03/21 00:05:21
Ok, if the Observer is implemented separately from
| |
37 // | |
38 // Destroy it by destroying all the observers, or making the observers dispose | |
39 // their refptrs. | |
40 // | |
41 // // This call will notify the observers to dispose their refptrs. | |
42 // observer1.controller()->OnControllerInvalidated(); | |
imcheng
2017/03/18 00:11:32
Hmm... I don't think we actually do this in any sc
takumif
2017/03/20 22:10:45
Right, OnControllerInvalidated() should be called
| |
43 // | |
44 class MediaRouteController : public mojom::MediaStatusObserver, | |
45 public base::RefCounted<MediaRouteController> { | |
46 public: | |
47 // Observes MediaRouteController for MediaStatus updates. The ownership of a | |
48 // MediaRouteController is shared by its observers. | |
49 class Observer { | |
50 public: | |
51 // Adds itself as an observer to |controller|. | |
52 explicit Observer(scoped_refptr<MediaRouteController> controller); | |
53 | |
54 // Removes itself as an observer if |controller_| is still valid. | |
55 virtual ~Observer(); | |
56 | |
57 virtual void OnMediaStatusUpdated(const MediaStatus& status) = 0; | |
58 | |
59 // Disposes the reference to the controller. Must be called by methods | |
60 // overriding it. | |
61 virtual void OnControllerInvalidated(); | |
mark a. foltz
2017/03/17 23:55:04
Shouldn't this be called only by the MediaControll
takumif
2017/03/20 22:10:45
Done.
| |
62 | |
63 MediaRouteController* controller() { return controller_.get(); } | |
mark a. foltz
2017/03/17 23:55:04
Shouldn't this return controller_ as a scoped_refp
takumif
2017/03/20 22:10:45
The point of this getter is for MediaRouterUI to b
| |
64 | |
65 private: | |
66 scoped_refptr<MediaRouteController> controller_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(Observer); | |
69 }; | |
70 | |
71 // |this| will forward media commands to |media_controller|. | |
72 // |media_controller| must be bound to a message pipe. |destructor_callback| | |
73 // will be called in the dtor of |this|. | |
74 MediaRouteController(const MediaRoute::Id& route_id, | |
75 mojom::MediaControllerPtr media_controller, | |
76 const base::Closure& destructor_callback); | |
77 | |
78 // Media controller methods for forwarding commands to |media_controller_|. | |
mark a. foltz
2017/03/17 23:55:04
...forwarding commands to a mojom::MediaController
takumif
2017/03/20 22:10:45
Done.
| |
79 void Play(); | |
80 void Pause(); | |
81 void Seek(base::TimeDelta time); | |
82 void SetMute(bool mute); | |
83 void SetVolume(float volume); | |
84 | |
85 // mojom::MediaStatusObserver: | |
86 // Notifies |observers_| of a status update. | |
87 void OnMediaStatusUpdated(const MediaStatus& status) override; | |
88 | |
89 // Called when the connection between |this| and |media_controller_| is no | |
90 // longer valid. Notifies |observers_| to dispose their references to |this|. | |
91 // |this| gets destroyed when all the references are disposed. | |
92 void OnControllerInvalidated(); | |
93 | |
94 // Called when |media_controller_| is bound. | |
95 void OnControllerBound(); | |
imcheng
2017/03/18 00:11:32
This is not needed anymore.
takumif
2017/03/20 22:10:45
Removed.
| |
96 | |
97 MediaRoute::Id route_id() const { return route_id_; } | |
98 | |
99 private: | |
100 friend class base::RefCounted<MediaRouteController>; | |
101 | |
102 ~MediaRouteController() override; | |
103 | |
104 void AddObserver(Observer* observer); | |
105 void RemoveObserver(Observer* observer); | |
106 | |
107 // The ID of the Media Route that |this| controls. | |
108 const MediaRoute::Id route_id_; | |
109 | |
110 // The out-of-process controller that |this| forwards commands to. | |
111 mojom::MediaControllerPtr media_controller_; | |
112 | |
113 // Gets called in the dtor. | |
114 base::Closure destructor_callback_; | |
115 | |
116 // Observers that |this| notifies of status updates. The observers share the | |
117 // ownership of |this| through scoped_refptr. | |
118 base::ObserverList<Observer> observers_; | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(MediaRouteController); | |
121 }; | |
122 | |
123 } // namespace media_router | |
124 | |
125 #endif // CHROME_BROWSER_MEDIA_ROUTER_MOJO_MEDIA_ROUTE_CONTROLLER_H_ | |
OLD | NEW |