[Presentation API, Android] Implement basic messaging
[chromium-blink-merge.git] / chrome / browser / media / android / router / media_router_android.h
blobb6d46dcd013d6c32cfca9d333ae00ee1a44e3d5d
1 // Copyright 2015 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.
5 #ifndef CHROME_BROWSER_MEDIA_ANDROID_ROUTER_MEDIA_ROUTER_ANDROID_H_
6 #define CHROME_BROWSER_MEDIA_ANDROID_ROUTER_MEDIA_ROUTER_ANDROID_H_
8 #include <jni.h>
10 #include "base/android/scoped_java_ref.h"
11 #include "base/containers/scoped_ptr_hash_map.h"
12 #include "base/id_map.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "chrome/browser/media/router/media_router.h"
17 namespace content {
18 class BrowserContext;
21 namespace media_router {
23 // A stub implementation of MediaRouter interface on Android.
24 class MediaRouterAndroid : public MediaRouter {
25 public:
26 ~MediaRouterAndroid() override;
28 static bool Register(JNIEnv* env);
30 // MediaRouter implementation.
31 void CreateRoute(
32 const MediaSource::Id& source_id,
33 const MediaSink::Id& sink_id,
34 const GURL& origin,
35 int tab_id,
36 const std::vector<MediaRouteResponseCallback>& callbacks) override;
37 void JoinRoute(
38 const MediaSource::Id& source,
39 const std::string& presentation_id,
40 const GURL& origin,
41 int tab_id,
42 const std::vector<MediaRouteResponseCallback>& callbacks) override;
43 void CloseRoute(const MediaRoute::Id& route_id) override;
44 void SendRouteMessage(const MediaRoute::Id& route_id,
45 const std::string& message,
46 const SendRouteMessageCallback& callback) override;
47 void SendRouteBinaryMessage(
48 const MediaRoute::Id& route_id,
49 scoped_ptr<std::vector<uint8>> data,
50 const SendRouteMessageCallback& callback) override;
51 void ClearIssue(const Issue::Id& issue_id) override;
52 void OnPresentationSessionDetached(const MediaRoute::Id& route_id) override;
54 // The methods called by the Java counterpart.
56 // Notifies the media router that information about sinks is received for
57 // a specific source URN.
58 void OnSinksReceived(
59 JNIEnv* env, jobject obj, jstring jsource_urn, jint jcount);
61 // Notifies the media router about a successful route creation.
62 void OnRouteCreated(
63 JNIEnv* env,
64 jobject obj,
65 jstring jmedia_route_id,
66 jint jcreate_route_request_id,
67 jboolean jis_local);
69 // Notifies the media router that route creation failed.
70 void OnRouteCreationError(
71 JNIEnv* env,
72 jobject obj,
73 jstring jerror_text,
74 jint jcreate_route_request_id);
76 // Notifies the media router when the route was closed.
77 void OnRouteClosed(JNIEnv* env, jobject obj, jstring jmedia_route_id);
79 // Notifies the media router about the result of sending a message.
80 void OnMessageSentResult(
81 JNIEnv* env, jobject obj, jboolean jsuccess, jint jcallback_id);
83 // Notifies the media router about a message received from the media route.
84 void OnMessage(
85 JNIEnv* env, jobject obj, jstring jmedia_route_id, jstring jmessage);
87 private:
88 friend class MediaRouterFactory;
90 explicit MediaRouterAndroid(content::BrowserContext*);
92 // MediaRouter implementation.
93 void RegisterMediaSinksObserver(MediaSinksObserver* observer) override;
94 void UnregisterMediaSinksObserver(MediaSinksObserver* observer) override;
95 void RegisterMediaRoutesObserver(MediaRoutesObserver* observer) override;
96 void UnregisterMediaRoutesObserver(MediaRoutesObserver* observer) override;
97 void RegisterIssuesObserver(IssuesObserver* observer) override;
98 void UnregisterIssuesObserver(IssuesObserver* observer) override;
99 void RegisterPresentationSessionMessagesObserver(
100 PresentationSessionMessagesObserver* observer) override;
101 void UnregisterPresentationSessionMessagesObserver(
102 PresentationSessionMessagesObserver* observer) override;
104 base::android::ScopedJavaGlobalRef<jobject> java_media_router_;
106 using MediaSinkObservers = base::ScopedPtrHashMap<
107 MediaSource::Id,
108 scoped_ptr<base::ObserverList<MediaSinksObserver>>>;
109 MediaSinkObservers sinks_observers_;
111 base::ObserverList<MediaRoutesObserver> routes_observers_;
113 struct CreateMediaRouteRequest {
114 CreateMediaRouteRequest(
115 const MediaSource& source,
116 const MediaSink& sink,
117 const std::string& presentation_id,
118 const std::vector<MediaRouteResponseCallback>& callbacks);
119 ~CreateMediaRouteRequest();
121 MediaSource media_source;
122 MediaSink media_sink;
123 std::string presentation_id;
124 std::vector<MediaRouteResponseCallback> callbacks;
127 using CreateMediaRouteRequests =
128 IDMap<CreateMediaRouteRequest, IDMapOwnPointer>;
129 CreateMediaRouteRequests create_route_requests_;
131 using MediaRoutes = std::vector<MediaRoute>;
132 MediaRoutes active_routes_;
134 using SendMessageCallbacks = IDMap<SendRouteMessageCallback, IDMapOwnPointer>;
135 SendMessageCallbacks message_callbacks_;
137 using MessagesObservers = base::ScopedPtrHashMap<
138 MediaRoute::Id,
139 scoped_ptr<base::ObserverList<PresentationSessionMessagesObserver>>>;
140 MessagesObservers messages_observers_;
142 DISALLOW_COPY_AND_ASSIGN(MediaRouterAndroid);
145 } // namespace media_router
147 #endif // CHROME_BROWSER_MEDIA_ANDROID_ROUTER_MEDIA_ROUTER_ANDROID_H_