Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(126)

Side by Side Diff: content/browser/android/content_video_view.cc

Issue 13669003: Refactoring ContentVideoViewContextDelegate.java (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/android/content_video_view.h" 5 #include "content/browser/android/content_video_view.h"
6 6
7 #include "base/android/jni_android.h"
8 #include "base/command_line.h" 7 #include "base/command_line.h"
9 #include "base/logging.h" 8 #include "base/logging.h"
10 #include "content/browser/android/media_player_manager_android.h" 9 #include "content/browser/android/media_player_manager_android.h"
11 #include "content/common/android/surface_texture_peer.h" 10 #include "content/common/android/surface_texture_peer.h"
12 #include "content/public/common/content_switches.h" 11 #include "content/public/common/content_switches.h"
13 #include "jni/ContentVideoView_jni.h" 12 #include "jni/ContentVideoView_jni.h"
14 13
15 using base::android::AttachCurrentThread; 14 using base::android::AttachCurrentThread;
16 using base::android::CheckException; 15 using base::android::CheckException;
17 using base::android::ScopedJavaGlobalRef; 16 using base::android::ScopedJavaGlobalRef;
18 17
19 namespace content { 18 namespace content {
20 19
20 namespace {
21 // There can only be one content video view at a time, this holds onto that
22 // singleton instance.
23 ContentVideoView* g_content_video_view = NULL;
24
25 } // namespace
26
27 static jobject GetSingletonJavaContentVideoView(JNIEnv*env, jclass) {
28 if (g_content_video_view)
29 return g_content_video_view->GetJavaObject(env).Release();
30 else
31 return NULL;
32 }
33
21 bool ContentVideoView::RegisterContentVideoView(JNIEnv* env) { 34 bool ContentVideoView::RegisterContentVideoView(JNIEnv* env) {
22 return RegisterNativesImpl(env); 35 return RegisterNativesImpl(env);
23 } 36 }
24 37
25 ContentVideoView::ContentVideoView(MediaPlayerManagerAndroid* manager) 38 ContentVideoView::ContentVideoView(
39 const ScopedJavaLocalRef<jobject>& context,
40 const ScopedJavaLocalRef<jobject>& client,
41 MediaPlayerManagerAndroid* manager)
26 : manager_(manager) { 42 : manager_(manager) {
43 DCHECK(!g_content_video_view);
44 JNIEnv *env = AttachCurrentThread();
45 j_content_video_view_.reset(new JavaObjectWeakGlobalRef(env,
joth 2013/05/15 22:38:57 j_content_video_view_ = JavaObjectWeakGlobalRef(en
michaelbai 2013/05/22 18:08:39 Done.
46 Java_ContentVideoView_createContentVideoView(env, context.obj(),
47 reinterpret_cast<int>(this), client.obj()).obj()));
48 g_content_video_view = this;
joth 2013/05/15 22:38:57 problem: at this point no one has a strong-ref to
michaelbai 2013/05/22 18:08:39 In the ctor of ContentVideoView(java side), mClien
joth 2013/06/03 21:22:04 Yes, you're right - I missed a step there.
27 } 49 }
28 50
29 ContentVideoView::~ContentVideoView() { 51 ContentVideoView::~ContentVideoView() {
joth 2013/05/15 22:38:57 do we need to check that ExitFullscreen has been d
michaelbai 2013/05/22 18:08:39 Done.
30 DestroyContentVideoView(); 52 DCHECK(g_content_video_view);
53 g_content_video_view = NULL;
31 } 54 }
32 55
33 void ContentVideoView::CreateContentVideoView() { 56 void ContentVideoView::OpenVideo() {
34 if (j_content_video_view_.is_null()) { 57 JNIEnv *env = AttachCurrentThread();
35 JNIEnv* env = AttachCurrentThread(); 58 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
36 j_content_video_view_.Reset(Java_ContentVideoView_createContentVideoView( 59 if (!content_video_view.is_null())
37 env, reinterpret_cast<jint>(this))); 60 Java_ContentVideoView_openVideo(env, content_video_view.obj());
38 } else {
39 // Just ask video view to reopen the video.
40 Java_ContentVideoView_openVideo(AttachCurrentThread(),
41 j_content_video_view_.obj());
42 }
43 }
44
45 void ContentVideoView::DestroyContentVideoView() {
46 if (!j_content_video_view_.is_null()) {
47 Java_ContentVideoView_destroyContentVideoView(AttachCurrentThread());
48 j_content_video_view_.Reset();
49 }
50 } 61 }
51 62
52 void ContentVideoView::OnMediaPlayerError(int error_type) { 63 void ContentVideoView::OnMediaPlayerError(int error_type) {
53 if (!j_content_video_view_.is_null()) { 64 JNIEnv *env = AttachCurrentThread();
54 Java_ContentVideoView_onMediaPlayerError(AttachCurrentThread(), 65 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
55 j_content_video_view_.obj(), 66 if (!content_video_view.is_null()) {
56 error_type); 67 Java_ContentVideoView_onMediaPlayerError(env, content_video_view.obj(),
68 error_type);
57 } 69 }
58 } 70 }
59 71
60 void ContentVideoView::OnVideoSizeChanged(int width, int height) { 72 void ContentVideoView::OnVideoSizeChanged(int width, int height) {
61 if (!j_content_video_view_.is_null()) { 73 JNIEnv *env = AttachCurrentThread();
62 Java_ContentVideoView_onVideoSizeChanged(AttachCurrentThread(), 74 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
63 j_content_video_view_.obj(), 75 if (!content_video_view.is_null()) {
64 width, 76 Java_ContentVideoView_onVideoSizeChanged(env, content_video_view.obj(),
65 height); 77 width, height);
66 } 78 }
67 } 79 }
68 80
69 void ContentVideoView::OnBufferingUpdate(int percent) { 81 void ContentVideoView::OnBufferingUpdate(int percent) {
70 if (!j_content_video_view_.is_null()) { 82 JNIEnv *env = AttachCurrentThread();
71 Java_ContentVideoView_onBufferingUpdate(AttachCurrentThread(), 83 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
72 j_content_video_view_.obj(), 84 if (!content_video_view.is_null()) {
73 percent); 85 Java_ContentVideoView_onBufferingUpdate(env, content_video_view.obj(),
86 percent);
74 } 87 }
75 } 88 }
76 89
77 void ContentVideoView::OnPlaybackComplete() { 90 void ContentVideoView::OnPlaybackComplete() {
78 if (!j_content_video_view_.is_null()) { 91 JNIEnv *env = AttachCurrentThread();
79 Java_ContentVideoView_onPlaybackComplete(AttachCurrentThread(), 92 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
80 j_content_video_view_.obj()); 93 if (!content_video_view.is_null())
94 Java_ContentVideoView_onPlaybackComplete(env, content_video_view.obj());
95 }
96
97 void ContentVideoView::OnExitFullscreen() {
98 JNIEnv *env = AttachCurrentThread();
99 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
100 if (!content_video_view.is_null()) {
101 Java_ContentVideoView_destroyContentVideoView(env,
102 content_video_view.obj());
81 } 103 }
joth 2013/05/15 22:38:57 j_content_video_view_.reset() ?
michaelbai 2013/05/22 18:08:39 Done.
82 } 104 }
83 105
84 void ContentVideoView::UpdateMediaMetadata() { 106 void ContentVideoView::UpdateMediaMetadata() {
85 if (!j_content_video_view_.is_null()) 107 JNIEnv *env = AttachCurrentThread();
86 UpdateMediaMetadata(AttachCurrentThread(), j_content_video_view_.obj()); 108 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
109 if (!content_video_view.is_null())
110 UpdateMediaMetadata(env, content_video_view.obj());
87 } 111 }
88 112
89 int ContentVideoView::GetVideoWidth(JNIEnv*, jobject obj) const { 113 int ContentVideoView::GetVideoWidth(JNIEnv*, jobject obj) const {
90 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer(); 114 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer();
91 return player ? player->GetVideoWidth() : 0; 115 return player ? player->GetVideoWidth() : 0;
92 } 116 }
93 117
94 int ContentVideoView::GetVideoHeight(JNIEnv*, jobject obj) const { 118 int ContentVideoView::GetVideoHeight(JNIEnv*, jobject obj) const {
95 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer(); 119 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer();
96 return player ? player->GetVideoHeight() : 0; 120 return player ? player->GetVideoHeight() : 0;
(...skipping 22 matching lines...) Expand all
119 manager_->FullscreenPlayerPlay(); 143 manager_->FullscreenPlayerPlay();
120 } 144 }
121 145
122 void ContentVideoView::Pause(JNIEnv*, jobject obj) { 146 void ContentVideoView::Pause(JNIEnv*, jobject obj) {
123 manager_->FullscreenPlayerPause(); 147 manager_->FullscreenPlayerPause();
124 } 148 }
125 149
126 void ContentVideoView::ExitFullscreen( 150 void ContentVideoView::ExitFullscreen(
127 JNIEnv*, jobject, jboolean release_media_player) { 151 JNIEnv*, jobject, jboolean release_media_player) {
128 manager_->ExitFullscreen(release_media_player); 152 manager_->ExitFullscreen(release_media_player);
129 j_content_video_view_.Reset();
130 } 153 }
131 154
132 void ContentVideoView::SetSurface(JNIEnv* env, jobject obj, 155 void ContentVideoView::SetSurface(JNIEnv* env, jobject obj,
133 jobject surface) { 156 jobject surface) {
134 manager_->SetVideoSurface(surface); 157 manager_->SetVideoSurface(surface);
135 } 158 }
136 159
137 void ContentVideoView::UpdateMediaMetadata(JNIEnv* env, jobject obj) { 160 void ContentVideoView::UpdateMediaMetadata(JNIEnv* env, jobject obj) {
138 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer(); 161 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer();
139 if (player && player->prepared()) 162 if (player && player->prepared())
140 Java_ContentVideoView_updateMediaMetadata( 163 Java_ContentVideoView_onUpdateMediaMetadata(
141 env, obj, player->GetVideoWidth(), player->GetVideoHeight(), 164 env, obj, player->GetVideoWidth(), player->GetVideoHeight(),
142 player->GetDuration().InMilliseconds(), player->can_pause(), 165 player->GetDuration().InMilliseconds(), player->can_pause(),
143 player->can_seek_forward(), player->can_seek_backward()); 166 player->can_seek_forward(), player->can_seek_backward());
144 } 167 }
145 168
169 ScopedJavaLocalRef<jobject> ContentVideoView::GetJavaObject(JNIEnv* env) {
170 if (j_content_video_view_.get()) {
171 return j_content_video_view_->get(env);
172 }
173 return ScopedJavaLocalRef<jobject>();
174 }
175
146 } // namespace content 176 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698