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 #include "content/browser/android/dialog_overlay_operations_impl.h" | |
6 | |
7 #include "content/public/browser/render_frame_host.h" | |
8 #include "content/public/browser/web_contents.h" | |
9 #include "gpu/ipc/common/gpu_surface_tracker.h" | |
10 #include "jni/DialogOverlayOperationsImpl_jni.h" | |
11 #include "ui/android/window_android.h" | |
12 | |
13 using base::android::AttachCurrentThread; | |
14 using base::android::JavaParamRef; | |
15 using base::android::ScopedJavaLocalRef; | |
16 | |
17 namespace content { | |
18 | |
19 // static | |
20 bool DialogOverlayOperationsImpl::RegisterDialogOverlayOperationsImpl( | |
21 JNIEnv* env) { | |
22 return RegisterNativesImpl(env); | |
23 } | |
24 | |
25 static jlong RegisterForTokens(JNIEnv* env, | |
26 const JavaParamRef<jobject>& obj, | |
27 jlong high, | |
28 jlong low) { | |
29 return reinterpret_cast<jlong>(new DialogOverlayOperationsImpl( | |
30 obj, base::UnguessableToken::Deserialize(high, low))); | |
31 } | |
32 | |
33 static void UnregisterForTokens(JNIEnv* env, | |
34 const JavaParamRef<jobject>& obj, | |
35 jlong handle) { | |
36 delete reinterpret_cast<DialogOverlayOperationsImpl*>(handle); | |
37 } | |
38 | |
39 DialogOverlayOperationsImpl::DialogOverlayOperationsImpl( | |
40 const JavaParamRef<jobject>& receiver, | |
41 const base::UnguessableToken& token) | |
42 : receiver_(receiver), token_(token), cvc_(nullptr) { | |
43 cvc_ = GetContentViewCore(); | |
boliu
2017/03/30 22:42:02
DCHECK everything here is on UI thread
liberato (no reviews please)
2017/04/04 17:49:28
Done.
| |
44 | |
45 // If there's no CVC, then just post a null token immediately. | |
46 if (!cvc_) { | |
47 JNIEnv* env = AttachCurrentThread(); | |
48 Java_DialogOverlayOperationsImpl_onDismissed(env, receiver_.obj()); | |
49 // |this| might be destroyed. | |
50 return; | |
51 } | |
52 | |
53 cvc_->AddObserver(this); | |
54 | |
55 // Also send the initial token, since we'll only get changes. | |
56 if (ui::WindowAndroid* window = cvc_->GetWindowAndroid()) { | |
57 ScopedJavaLocalRef<jobject> token = window->GetWindowToken(); | |
58 if (!token.is_null()) { | |
59 JNIEnv* env = AttachCurrentThread(); | |
60 Java_DialogOverlayOperationsImpl_onWindowToken(env, receiver_.obj(), | |
61 token); | |
62 } | |
63 } | |
64 } | |
65 | |
66 DialogOverlayOperationsImpl::~DialogOverlayOperationsImpl() { | |
67 if (cvc_) | |
68 cvc_->RemoveObserver(this); | |
69 } | |
70 | |
71 void DialogOverlayOperationsImpl::OnContentViewCoreDestroyed() { | |
72 cvc_ = nullptr; | |
73 | |
74 JNIEnv* env = AttachCurrentThread(); | |
75 Java_DialogOverlayOperationsImpl_onDismissed(env, receiver_.obj()); | |
76 // |this| might be destroyed. | |
boliu
2017/03/30 22:42:02
what if you use DeleteSoon in UnregisterForTokens
liberato (no reviews please)
2017/04/04 17:49:28
good point. i'll explicitly remove the CVC observ
| |
77 } | |
78 | |
79 void DialogOverlayOperationsImpl::OnAttachedToWindow() { | |
80 JNIEnv* env = AttachCurrentThread(); | |
81 | |
82 ScopedJavaLocalRef<jobject> token; | |
83 | |
84 if (ui::WindowAndroid* window = cvc_->GetWindowAndroid()) | |
85 token = window->GetWindowToken(); | |
86 | |
87 Java_DialogOverlayOperationsImpl_onWindowToken(env, receiver_.obj(), token); | |
88 } | |
89 | |
90 void DialogOverlayOperationsImpl::OnDetachedFromWindow() { | |
91 JNIEnv* env = AttachCurrentThread(); | |
92 Java_DialogOverlayOperationsImpl_onWindowToken(env, receiver_.obj(), nullptr); | |
93 } | |
94 | |
95 ContentViewCoreImpl* DialogOverlayOperationsImpl::GetContentViewCore() { | |
96 // Get the frame from the token. | |
97 content::RenderFrameHost* frame = | |
98 content::RenderFrameHost::FromOverlayRoutingToken(token_); | |
99 if (!frame) { | |
100 DVLOG(1) << "Cannot find frame host for token " << token_; | |
101 return nullptr; | |
102 } | |
103 | |
104 content::WebContents* web_contents = | |
105 content::WebContents::FromRenderFrameHost(frame); | |
106 if (!web_contents) { | |
boliu
2017/03/30 22:42:02
there's a chromium-dev thread by dcheng that this
liberato (no reviews please)
2017/04/04 17:49:28
DCHECK it is then!
| |
107 DVLOG(1) << "Cannot find web_contents for token " << token_; | |
108 return nullptr; | |
109 } | |
110 | |
111 content::ContentViewCoreImpl* cvc = | |
112 content::ContentViewCoreImpl::FromWebContents(web_contents); | |
113 if (!cvc) { | |
114 DVLOG(1) << "Cannot find cvc for token " << token_; | |
115 return nullptr; | |
116 } | |
117 | |
118 return cvc; | |
119 } | |
120 | |
121 static jint RegisterSurface(JNIEnv* env, | |
122 const base::android::JavaParamRef<jobject>& jcaller, | |
123 const JavaParamRef<jobject>& surface) { | |
124 return gpu::GpuSurfaceTracker::Get()->RegisterViewSurface(surface.obj()); | |
125 } | |
126 | |
127 static void UnregisterSurface( | |
128 JNIEnv* env, | |
129 const base::android::JavaParamRef<jobject>& jcaller, | |
130 jint surface_id) { | |
131 gpu::GpuSurfaceTracker::Get()->UnregisterViewSurface(surface_id); | |
132 } | |
133 | |
134 } // namespace content | |
OLD | NEW |