forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notify clients when the web engine wants to create a new Window
Certain navigations (like target=_blank links) require the web engine to create a new Window. In those cases Chromium creates a new WebContents object and notifies the client (Wolvic in this case) about that. However the current architecture of Wolvic does not allow us to properly use that new WebContents object to create a new tab. To circumvent that, we can use the fact that Chromium cancels the creation of a new window if the ownership of the WebContents object is not taken when AddNewContents() is called on the delegate. We precisely do that (we let the new WebContents die) but we use the target_url (the URL of the new window) to notify the client (Wolvic) via a new method called onCreateNewWindow() that was added to a new object called WolvicWebContentsDelegate which directly inherits from WebContentsDelegateAndroid (the one we were using previously). The client just needs to override that onCreateWindow() method and trigger the creation of a new window there using the passed in URL.
- Loading branch information
Showing
8 changed files
with
119 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "wolvic/browser/wolvic_web_contents_delegate.h" | ||
|
||
#include "base/android/jni_android.h" | ||
#include "base/android/scoped_java_ref.h" | ||
#include "content/public/browser/web_contents.h" | ||
#include "wolvic/browser/wolvic_contents.h" | ||
#include "wolvic/jni_headers/WolvicWebContentsDelegate_jni.h" | ||
#include "url/android/gurl_android.h" | ||
|
||
namespace wolvic { | ||
|
||
WolvicWebContentsDelegate::WolvicWebContentsDelegate(JNIEnv* env, jobject obj) | ||
: WebContentsDelegateAndroid(env, obj) {} | ||
|
||
WolvicWebContentsDelegate::~WolvicWebContentsDelegate() = default; | ||
|
||
using base::android::ScopedJavaLocalRef; | ||
|
||
// Called by web_contents_impl.cc whenever a navigation | ||
// requires the creation of a new window (for example | ||
// a link with target=_blank | ||
void WolvicWebContentsDelegate::AddNewContents( | ||
content::WebContents* source, | ||
std::unique_ptr<content::WebContents> new_contents, | ||
const GURL& target_url, | ||
WindowOpenDisposition disposition, | ||
const blink::mojom::WindowFeatures& window_features, | ||
bool user_gesture, | ||
bool* was_blocked) { | ||
|
||
JNIEnv* env = base::android::AttachCurrentThread(); | ||
ScopedJavaLocalRef<jobject> java_delegate = GetJavaDelegate(env); | ||
DCHECK(java_delegate.obj()); | ||
|
||
// We let new_contents die on purpouse (by not assigning the | ||
// unique_ptr to any local variable/attribute because that way | ||
// we are telling the web engine that we do not want a new | ||
// window to be created. We'll in any case use the target_urlformatter | ||
// to notify the client (Wolvic) so that it can create the window | ||
// for the new URL on its own. | ||
ScopedJavaLocalRef<jobject> java_gurl = | ||
url::GURLAndroid::FromNativeGURL(env, target_url); | ||
Java_WolvicWebContentsDelegate_onCreateNewWindow(env, java_delegate, java_gurl); | ||
} | ||
|
||
} // namespace wolvic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef WOLVIC_BROWSER_WOLVIC_WEB_CONTENTS_DELEGATE_H_ | ||
#define WOLVIC_BROWSER_WOLVIC_WEB_CONTENTS_DELEGATE_H_ | ||
|
||
#include "components/embedder_support/android/delegate/web_contents_delegate_android.h" | ||
|
||
namespace wolvic { | ||
|
||
class WolvicWebContentsDelegate | ||
: public web_contents_delegate_android::WebContentsDelegateAndroid { | ||
public: | ||
WolvicWebContentsDelegate(JNIEnv* env, jobject obj); | ||
~WolvicWebContentsDelegate() override; | ||
|
||
// See //android_webview/docs/how-does-on-create-window-work.md for more | ||
// details. | ||
void AddNewContents(content::WebContents* source, | ||
std::unique_ptr<content::WebContents> new_contents, | ||
const GURL& target_url, | ||
WindowOpenDisposition disposition, | ||
const blink::mojom::WindowFeatures& window_features, | ||
bool user_gesture, | ||
bool* was_blocked) final; | ||
|
||
private: | ||
std::unique_ptr<content::WebContents> new_contents_; | ||
}; | ||
|
||
}// namespace wolvic | ||
|
||
#endif // WOLVIC_BROWSER_WOLVIC_WEB_CONTENTS_DELEGATE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
wolvic/java/org/chromium/wolvic/WolvicWebContentsDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.wolvic; | ||
|
||
import androidx.annotation.VisibleForTesting; | ||
|
||
import org.chromium.url.GURL; | ||
import org.chromium.base.annotations.CalledByNative; | ||
import org.chromium.base.annotations.JNINamespace; | ||
import org.chromium.components.embedder_support.delegate.WebContentsDelegateAndroid; | ||
|
||
@JNINamespace("wolvic") | ||
public abstract class WolvicWebContentsDelegate extends WebContentsDelegateAndroid { | ||
@CalledByNative | ||
public abstract void onCreateNewWindow(GURL url); | ||
} |