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

Unified Diff: content/browser/android/dialog_android_overlay.cc

Issue 2178973004: DialogSurfaceManager implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cleanup Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/android/dialog_android_overlay.cc
diff --git a/content/browser/android/dialog_android_overlay.cc b/content/browser/android/dialog_android_overlay.cc
new file mode 100644
index 0000000000000000000000000000000000000000..312fecc70a05b24fb09172c5d92b7b0b84d3b8d4
--- /dev/null
+++ b/content/browser/android/dialog_android_overlay.cc
@@ -0,0 +1,154 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/android/dialog_android_overlay.h"
+
+#include "content/browser/frame_host/render_frame_host_impl.h"
+#include "content/public/browser/render_process_host.h"
+#include "content/public/browser/web_contents.h"
+#include "jni/DialogAndroidOverlay_jni.h"
+#include "ui/android/window_android.h"
+
+using base::android::AttachCurrentThread;
+using base::android::JavaParamRef;
+using base::android::ScopedJavaLocalRef;
+
+namespace content {
+
+// static
+bool DialogAndroidOverlay::RegisterDialogAndroidOverlay(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+static jlong Init(JNIEnv* env,
+ const JavaParamRef<jobject>& obj,
+ jint renderer_pid,
+ jint render_frame_id) {
+ return reinterpret_cast<jlong>(
+ new DialogAndroidOverlay(obj, renderer_pid, render_frame_id));
+}
+
+static void Shutdown(JNIEnv* env,
+ const JavaParamRef<jobject>& obj,
+ jlong handle) {
+ content::BrowserThread::DeleteSoon(
+ content::BrowserThread::UI, FROM_HERE,
+ reinterpret_cast<DialogAndroidOverlay*>(handle));
+}
+
+DialogAndroidOverlay::DialogAndroidOverlay(
+ const JavaParamRef<jobject>& receiver,
+ int renderer_pid,
+ int render_frame_id)
+ : receiver_(receiver),
+ renderer_pid_(renderer_pid),
+ render_frame_id_(render_frame_id),
+ cvc_(nullptr) {
+ // This is called from a random thread, so post to the right one.
+ // Note that it's safe for |this| to be unretained, since java can't destroy
+ // us until we return, and that will post too.
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&DialogAndroidOverlay::CompleteInitialization,
+ base::Unretained(this)));
+}
+
+DialogAndroidOverlay::~DialogAndroidOverlay() {
+ if (cvc_)
+ cvc_->RemoveObserver(this);
+}
+
+void DialogAndroidOverlay::OnContentViewCoreDestroyed() {
+ cvc_ = nullptr;
+
+ JNIEnv* env = AttachCurrentThread();
+ Java_DialogAndroidOverlay_onDismissed(env, receiver_.obj());
+ // |this| might be destroyed.
+}
+
+void DialogAndroidOverlay::OnAttachedToWindow() {
+ JNIEnv* env = AttachCurrentThread();
+
+ ScopedJavaLocalRef<jobject> token;
+
+ if (ui::WindowAndroid* window = cvc_->GetWindowAndroid())
+ token = window->GetWindowToken();
+
+ Java_DialogAndroidOverlay_onWindowToken(env, receiver_.obj(), token);
+}
+
+void DialogAndroidOverlay::OnDetachedFromWindow() {
+ JNIEnv* env = AttachCurrentThread();
+ Java_DialogAndroidOverlay_onWindowToken(env, receiver_.obj(), nullptr);
+}
+
+void DialogAndroidOverlay::CompleteInitialization() {
+ cvc_ = GetContentViewCore();
+
+ // If there's no CVC, then just post a null token immediately.
+ if (!cvc_) {
+ JNIEnv* env = AttachCurrentThread();
+ Java_DialogAndroidOverlay_onDismissed(env, receiver_.obj());
+ // |this| might be destroyed.
+ return;
+ }
+
+ cvc_->AddObserver(this);
+
+ // Also send the initial token, since we'll only get changes.
+ if (ui::WindowAndroid* window = cvc_->GetWindowAndroid()) {
+ ScopedJavaLocalRef<jobject> token = window->GetWindowToken();
+ if (!token.is_null()) {
+ JNIEnv* env = AttachCurrentThread();
+ Java_DialogAndroidOverlay_onWindowToken(env, receiver_.obj(), token);
+ }
+ }
+}
+
+ContentViewCoreImpl* DialogAndroidOverlay::GetContentViewCore() {
+ // Get the id from the handle. On android, the handle is the pid.
+ content::RenderProcessHost::iterator it =
+ content::RenderProcessHost::AllHostsIterator();
+ int render_process_id = 0;
+ while (!it.IsAtEnd()) {
+ if (it.GetCurrentValue()->GetHandle() == renderer_pid_) {
+ render_process_id = it.GetCurrentValue()->GetID();
+ break;
+ }
+ it.Advance();
+ }
+ if (!render_process_id) {
+ DVLOG(1) << "Cannot find render process for renderer_pid " << renderer_pid_;
+ return nullptr;
+ }
+
+ // Get the frame from the id.
+ content::RenderFrameHostImpl* frame =
+ content::RenderFrameHostImpl::FromID(render_process_id, render_frame_id_);
+ if (!frame) {
+ DVLOG(1) << "Cannot find frame for renderer_pid " << renderer_pid_
+ << " render_frame_id " << render_frame_id_;
+ return nullptr;
+ }
+
+ content::WebContents* web_contents =
+ content::WebContents::FromRenderFrameHost(frame);
+ if (!web_contents) {
+ DVLOG(1) << "Cannot find web_contents for renderer_pid " << renderer_pid_
+ << " render_frame_id " << render_frame_id_;
+ return nullptr;
+ }
+
+ content::ContentViewCoreImpl* cvc =
+ content::ContentViewCoreImpl::FromWebContents(web_contents);
+ if (!cvc) {
+ DVLOG(1) << "Cannot find cvc for renderer_pid " << renderer_pid_
+ << " render_frame_id " << render_frame_id_;
+ return nullptr;
+ }
+
+ return cvc;
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698