Commit 4525e6a9 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Split embedder-specific behavior off from JavaScriptDialogTabHelper

This will allow us to componentize JavaScriptDialogTabHelper and reuse
it for WebLayer.

Bug: 1045499,1025256
Change-Id: I07d6b2123a00898a8aa03400b4b60c203b694252
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2023397Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#736922}
parent 44c4433a
......@@ -112,6 +112,9 @@ jumbo_static_library("ui") {
"javascript_dialogs/javascript_dialog.h",
"javascript_dialogs/javascript_dialog_tab_helper.cc",
"javascript_dialogs/javascript_dialog_tab_helper.h",
"javascript_dialogs/javascript_dialog_tab_helper_delegate.h",
"javascript_dialogs/javascript_dialog_tab_helper_delegate_android.cc",
"javascript_dialogs/javascript_dialog_tab_helper_delegate_android.h",
"login/login_handler.cc",
"login/login_handler.h",
"login/login_interstitial_delegate.cc",
......@@ -1003,6 +1006,8 @@ jumbo_static_library("ui") {
"in_product_help/reopen_tab_in_product_help_trigger.h",
"intent_picker_tab_helper.cc",
"intent_picker_tab_helper.h",
"javascript_dialogs/javascript_dialog_tab_helper_delegate_desktop.cc",
"javascript_dialogs/javascript_dialog_tab_helper_delegate_desktop.h",
"layout_constants.cc",
"layout_constants.h",
"location_bar/location_bar.h",
......
......@@ -11,6 +11,7 @@
#include "base/metrics/histogram_macros.h"
#include "chrome/android/chrome_jni_headers/JavascriptTabModalDialog_jni.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper_delegate_android.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
......@@ -33,25 +34,6 @@ JavaScriptDialogAndroid::~JavaScriptDialogAndroid() {
}
}
// static
base::WeakPtr<JavaScriptDialog> JavaScriptDialog::CreateNewDialog(
content::WebContents* parent_web_contents,
content::WebContents* alerting_web_contents,
const base::string16& title,
content::JavaScriptDialogType dialog_type,
const base::string16& message_text,
const base::string16& default_prompt_text,
content::JavaScriptDialogManager::DialogClosedCallback
callback_on_button_clicked,
base::OnceClosure callback_on_cancelled) {
return (new JavaScriptDialogAndroid(parent_web_contents,
alerting_web_contents, title, dialog_type,
message_text, default_prompt_text,
std::move(callback_on_button_clicked),
std::move(callback_on_cancelled)))
->weak_factory_.GetWeakPtr();
}
void JavaScriptDialogAndroid::CloseDialogWithoutCallback() {
delete this;
}
......@@ -143,3 +125,25 @@ JavaScriptDialogAndroid::JavaScriptDialogAndroid(
Java_JavascriptTabModalDialog_showDialog(env, dialog_object, jwindow,
reinterpret_cast<intptr_t>(this));
}
// Note on the two callbacks: |dialog_callback_on_button_clicked| is for the
// case where user responds to the dialog. |dialog_callback_on_cancelled| is
// for the case where user cancels the dialog without interacting with the
// dialog (e.g. clicks the navigate back button on Android).
base::WeakPtr<JavaScriptDialog>
JavaScriptDialogTabHelperDelegateAndroid::CreateNewDialog(
content::WebContents* alerting_web_contents,
const base::string16& title,
content::JavaScriptDialogType dialog_type,
const base::string16& message_text,
const base::string16& default_prompt_text,
content::JavaScriptDialogManager::DialogClosedCallback
callback_on_button_clicked,
base::OnceClosure callback_on_cancelled) {
return (new JavaScriptDialogAndroid(web_contents_, alerting_web_contents,
title, dialog_type, message_text,
default_prompt_text,
std::move(callback_on_button_clicked),
std::move(callback_on_cancelled)))
->weak_factory_.GetWeakPtr();
}
......@@ -11,6 +11,7 @@
#include "base/android/scoped_java_ref.h"
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog.h"
#include "content/public/browser/javascript_dialog_manager.h"
......@@ -23,21 +24,6 @@ class JavaScriptDialogAndroid : public JavaScriptDialog {
public:
~JavaScriptDialogAndroid() override;
// Note on the two callbacks: |dialog_callback_on_button_clicked| is for the
// case where user responds to the dialog. |dialog_callback_on_cancelled| is
// for the case where user cancels the dialog without interacting with the
// dialog (e.g. clicks the navigate back button on Android).
static base::WeakPtr<JavaScriptDialogAndroid> Create(
content::WebContents* parent_web_contents,
content::WebContents* alerting_web_contents,
const base::string16& title,
content::JavaScriptDialogType dialog_type,
const base::string16& message_text,
const base::string16& default_prompt_text,
content::JavaScriptDialogManager::DialogClosedCallback
callback_on_button_clicked,
base::OnceClosure callback_on_cancelled);
// JavaScriptDialog:
void CloseDialogWithoutCallback() override;
base::string16 GetUserInput() override;
......@@ -51,6 +37,7 @@ class JavaScriptDialogAndroid : public JavaScriptDialog {
private:
friend class JavaScriptDialog;
friend class JavaScriptDialogTabHelperDelegateAndroid;
JavaScriptDialogAndroid(content::WebContents* parent_web_contents,
content::WebContents* alerting_web_contents,
......
......@@ -5,33 +5,12 @@
#ifndef CHROME_BROWSER_UI_JAVASCRIPT_DIALOGS_JAVASCRIPT_DIALOG_H_
#define CHROME_BROWSER_UI_JAVASCRIPT_DIALOGS_JAVASCRIPT_DIALOG_H_
#include <memory>
#include "base/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string16.h"
#include "build/build_config.h"
#include "content/public/browser/javascript_dialog_manager.h"
namespace content {
class WebContents;
}
class JavaScriptDialog {
public:
virtual ~JavaScriptDialog() {}
// Factory function for creating a tab-modal Javascript dialog.
static base::WeakPtr<JavaScriptDialog> CreateNewDialog(
content::WebContents* parent_web_contents,
content::WebContents* alerting_web_contents,
const base::string16& title,
content::JavaScriptDialogType dialog_type,
const base::string16& message_text,
const base::string16& default_prompt_text,
content::JavaScriptDialogManager::DialogClosedCallback dialog_callback,
base::OnceClosure dialog_closed_callback);
// Closes the dialog without sending a callback. This is useful when the
// JavaScriptDialogTabHelper needs to make this dialog go away so that it can
// respond to a call that requires it to make no callback or make a customized
......
......@@ -13,15 +13,11 @@
#include "base/optional.h"
#include "build/build_config.h"
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog.h"
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper_delegate.h"
#include "content/public/browser/javascript_dialog_manager.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#if !defined(OS_ANDROID)
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#endif
// A class, attached to WebContentses in browser windows, that is the
// JavaScriptDialogManager for them and handles displaying their dialogs.
//
......@@ -43,10 +39,6 @@
class JavaScriptDialogTabHelper
: public content::JavaScriptDialogManager,
public content::WebContentsObserver,
#if !defined(OS_ANDROID)
public BrowserListObserver,
public TabStripModelObserver,
#endif
public content::WebContentsUserData<JavaScriptDialogTabHelper> {
public:
enum class DismissalCause {
......@@ -97,9 +89,15 @@ class JavaScriptDialogTabHelper
kMaxValue = kDialogClosed,
};
explicit JavaScriptDialogTabHelper(content::WebContents* web_contents);
static void CreateForWebContents(
content::WebContents* web_contents,
std::unique_ptr<JavaScriptDialogTabHelperDelegate> delegate);
~JavaScriptDialogTabHelper() override;
void BrowserActiveStateChanged();
void CloseDialogWithReason(DismissalCause reason);
void SetDialogShownCallbackForTesting(base::OnceClosure callback);
bool IsShowingDialogForTesting() const;
void ClickDialogButtonForTesting(bool accept,
......@@ -130,20 +128,17 @@ class JavaScriptDialogTabHelper
void DidStartNavigation(
content::NavigationHandle* navigation_handle) override;
#if !defined(OS_ANDROID)
// BrowserListObserver:
void OnBrowserSetLastActive(Browser* browser) override;
// TabStripModelObserver:
void OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) override;
#endif
private:
friend class content::WebContentsUserData<JavaScriptDialogTabHelper>;
// This is here to hide the normal WebContentsUserData factory function in
// favor of that which takes a delegate.
static void CreateForWebContents(content::WebContents* web_contents);
JavaScriptDialogTabHelper(
content::WebContents* web_contents,
std::unique_ptr<JavaScriptDialogTabHelperDelegate> delegate);
// Logs the cause of a dialog dismissal in UMA.
void LogDialogDismissalCause(DismissalCause cause);
......@@ -156,17 +151,6 @@ class JavaScriptDialogTabHelper
bool success,
const base::string16& user_input);
// Marks the tab as needing attention. The WebContents must be in a browser
// window.
void SetTabNeedsAttention(bool attention);
#if !defined(OS_ANDROID)
// Marks the tab as needing attention.
void SetTabNeedsAttentionImpl(bool attention,
TabStripModel* tab_strip_model,
int index);
#endif
// There can be at most one dialog (pending or not) being shown at any given
// time on a tab. Depending on the type of the dialog, the variables
// |dialog_|, |pending_dialog_|, and |dialog_callback_| can be present in
......@@ -212,21 +196,7 @@ class JavaScriptDialogTabHelper
// A closure to be fired when a dialog is dismissed. For testing only.
DialogDismissedCallback dialog_dismissed_;
#if !defined(OS_ANDROID)
// If this instance is observing a TabStripModel, then this member is not
// nullptr.
//
// This raw pointer is safe to use because the lifetime of this instance is
// tied to the corresponding WebContents, which is owned by the TabStripModel.
// Any time TabStripModel would give up ownership of the WebContents, it would
// first send either a TabStripModelChange::kRemoved or kReplaced
// via OnTabStripModelChanged() callback, which gives this instance the
// opportunity to stop observing the TabStripModel.
//
// A TabStripModel cannot be destroyed without first detaching all of its
// WebContents.
TabStripModel* tab_strip_model_being_observed_ = nullptr;
#endif
std::unique_ptr<JavaScriptDialogTabHelperDelegate> delegate_;
WEB_CONTENTS_USER_DATA_KEY_DECL();
......
// Copyright 2020 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.
#ifndef CHROME_BROWSER_UI_JAVASCRIPT_DIALOGS_JAVASCRIPT_DIALOG_TAB_HELPER_DELEGATE_H_
#define CHROME_BROWSER_UI_JAVASCRIPT_DIALOGS_JAVASCRIPT_DIALOG_TAB_HELPER_DELEGATE_H_
#include "base/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string16.h"
#include "content/public/browser/javascript_dialog_manager.h"
class JavaScriptDialog;
// This interface provides platform-specific controller functionality to
// JavaScriptDialogTabHelper.
class JavaScriptDialogTabHelperDelegate {
public:
virtual ~JavaScriptDialogTabHelperDelegate() = default;
// Factory function for creating a tab modal view.
virtual base::WeakPtr<JavaScriptDialog> CreateNewDialog(
content::WebContents* alerting_web_contents,
const base::string16& title,
content::JavaScriptDialogType dialog_type,
const base::string16& message_text,
const base::string16& default_prompt_text,
content::JavaScriptDialogManager::DialogClosedCallback dialog_callback,
base::OnceClosure dialog_closed_callback) = 0;
// Called when a dialog is about to be shown.
virtual void WillRunDialog() = 0;
// Called when a dialog has been hidden.
virtual void DidCloseDialog() = 0;
// Called when a tab should indicate to the user that it needs attention, such
// as when an alert fires from a background tab.
virtual void SetTabNeedsAttention(bool attention) = 0;
// Should return true if the web contents is foremost (i.e. the active tab in
// the active browser window).
virtual bool IsWebContentsForemost() = 0;
// Should return true if this web contents is an app window, such as a PWA.
virtual bool IsApp() = 0;
};
#endif // CHROME_BROWSER_UI_JAVASCRIPT_DIALOGS_JAVASCRIPT_DIALOG_TAB_HELPER_DELEGATE_H_
// Copyright 2020 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 "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper_delegate_android.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
JavaScriptDialogTabHelperDelegateAndroid::
JavaScriptDialogTabHelperDelegateAndroid(content::WebContents* web_contents)
: web_contents_(web_contents) {}
JavaScriptDialogTabHelperDelegateAndroid::
~JavaScriptDialogTabHelperDelegateAndroid() {}
void JavaScriptDialogTabHelperDelegateAndroid::WillRunDialog() {}
void JavaScriptDialogTabHelperDelegateAndroid::DidCloseDialog() {}
void JavaScriptDialogTabHelperDelegateAndroid::SetTabNeedsAttention(
bool attention) {}
bool JavaScriptDialogTabHelperDelegateAndroid::IsWebContentsForemost() {
TabModel* tab_model = TabModelList::GetTabModelForWebContents(web_contents_);
if (tab_model) {
return tab_model->IsCurrentModel() &&
tab_model->GetActiveWebContents() == web_contents_;
} else {
// If tab model is not found (e.g. single tab model), fall back to check
// whether the tab for this web content is interactive.
TabAndroid* tab = TabAndroid::FromWebContents(web_contents_);
return tab && tab->IsUserInteractable();
}
}
bool JavaScriptDialogTabHelperDelegateAndroid::IsApp() {
return false;
}
// Copyright 2020 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.
#ifndef CHROME_BROWSER_UI_JAVASCRIPT_DIALOGS_JAVASCRIPT_DIALOG_TAB_HELPER_DELEGATE_ANDROID_H_
#define CHROME_BROWSER_UI_JAVASCRIPT_DIALOGS_JAVASCRIPT_DIALOG_TAB_HELPER_DELEGATE_ANDROID_H_
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper_delegate.h"
namespace content {
class WebContents;
}
class JavaScriptDialogTabHelperDelegateAndroid
: public JavaScriptDialogTabHelperDelegate {
public:
explicit JavaScriptDialogTabHelperDelegateAndroid(
content::WebContents* web_contents);
~JavaScriptDialogTabHelperDelegateAndroid() override;
JavaScriptDialogTabHelperDelegateAndroid(
const JavaScriptDialogTabHelperDelegateAndroid& other) = delete;
JavaScriptDialogTabHelperDelegateAndroid& operator=(
const JavaScriptDialogTabHelperDelegateAndroid& other) = delete;
// JavaScriptDialogTabHelperDelegate
base::WeakPtr<JavaScriptDialog> CreateNewDialog(
content::WebContents* alerting_web_contents,
const base::string16& title,
content::JavaScriptDialogType dialog_type,
const base::string16& message_text,
const base::string16& default_prompt_text,
content::JavaScriptDialogManager::DialogClosedCallback dialog_callback,
base::OnceClosure dialog_closed_callback) override;
void WillRunDialog() override;
void DidCloseDialog() override;
void SetTabNeedsAttention(bool attention) override;
bool IsWebContentsForemost() override;
bool IsApp() override;
private:
content::WebContents* web_contents_;
};
#endif // CHROME_BROWSER_UI_JAVASCRIPT_DIALOGS_JAVASCRIPT_DIALOG_TAB_HELPER_DELEGATE_ANDROID_H_
// Copyright 2020 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 "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper_delegate_desktop.h"
#include <utility>
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog.h"
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h"
#include "chrome/browser/ui/tab_modal_confirm_dialog.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "components/app_modal/javascript_dialog_manager.h"
#include "components/navigation_metrics/navigation_metrics.h"
#include "components/ukm/content/source_url_recorder.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "ui/gfx/text_elider.h"
JavaScriptDialogTabHelperDelegateDesktop::
JavaScriptDialogTabHelperDelegateDesktop(content::WebContents* web_contents)
: web_contents_(web_contents) {}
JavaScriptDialogTabHelperDelegateDesktop::
~JavaScriptDialogTabHelperDelegateDesktop() {
DCHECK(!tab_strip_model_being_observed_);
}
void JavaScriptDialogTabHelperDelegateDesktop::WillRunDialog() {
BrowserList::AddObserver(this);
}
void JavaScriptDialogTabHelperDelegateDesktop::DidCloseDialog() {
BrowserList::RemoveObserver(this);
}
void JavaScriptDialogTabHelperDelegateDesktop::SetTabNeedsAttention(
bool attention) {
Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
if (!browser) {
// It's possible that the WebContents is no longer in the tab strip. If so,
// just give up. https://crbug.com/786178#c7.
return;
}
TabStripModel* tab_strip_model = browser->tab_strip_model();
SetTabNeedsAttentionImpl(
attention, tab_strip_model,
tab_strip_model->GetIndexOfWebContents(web_contents_));
}
bool JavaScriptDialogTabHelperDelegateDesktop::IsWebContentsForemost() {
Browser* browser = BrowserList::GetInstance()->GetLastActive();
DCHECK(browser);
return browser->tab_strip_model()->GetActiveWebContents() == web_contents_;
}
bool JavaScriptDialogTabHelperDelegateDesktop::IsApp() {
Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
return browser && browser->deprecated_is_app();
}
void JavaScriptDialogTabHelperDelegateDesktop::OnBrowserSetLastActive(
Browser* browser) {
JavaScriptDialogTabHelper::FromWebContents(web_contents_)
->BrowserActiveStateChanged();
}
void JavaScriptDialogTabHelperDelegateDesktop::OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) {
if (change.type() == TabStripModelChange::kReplaced) {
auto* replace = change.GetReplace();
if (replace->old_contents == web_contents_) {
// At this point, this WebContents is no longer in the tabstrip. The usual
// teardown will not be able to turn off the attention indicator, so that
// must be done here.
SetTabNeedsAttentionImpl(false, tab_strip_model, replace->index);
JavaScriptDialogTabHelper::FromWebContents(web_contents_)
->CloseDialogWithReason(
JavaScriptDialogTabHelper::DismissalCause::kTabSwitchedOut);
}
} else if (change.type() == TabStripModelChange::kRemoved) {
for (const auto& contents : change.GetRemove()->contents) {
if (contents.contents == web_contents_) {
// We don't call TabStripModel::SetTabNeedsAttention because it causes
// re-entrancy into TabStripModel and correctness of the |index|
// parameter is dependent on observer ordering. This is okay in the
// short term because the tab in question is being removed.
// TODO(erikchen): Clean up TabStripModel observer API so that this
// doesn't require re-entrancy and/or works correctly
// https://crbug.com/842194.
DCHECK(tab_strip_model_being_observed_);
tab_strip_model_being_observed_->RemoveObserver(this);
tab_strip_model_being_observed_ = nullptr;
JavaScriptDialogTabHelper::FromWebContents(web_contents_)
->CloseDialogWithReason(
JavaScriptDialogTabHelper::DismissalCause::kTabHelperDestroyed);
break;
}
}
}
}
void JavaScriptDialogTabHelperDelegateDesktop::SetTabNeedsAttentionImpl(
bool attention,
TabStripModel* tab_strip_model,
int index) {
tab_strip_model->SetTabNeedsAttentionAt(index, attention);
if (attention) {
tab_strip_model->AddObserver(this);
tab_strip_model_being_observed_ = tab_strip_model;
} else {
DCHECK_EQ(tab_strip_model_being_observed_, tab_strip_model);
tab_strip_model_being_observed_->RemoveObserver(this);
tab_strip_model_being_observed_ = nullptr;
}
}
// Copyright 2020 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.
#ifndef CHROME_BROWSER_UI_JAVASCRIPT_DIALOGS_JAVASCRIPT_DIALOG_TAB_HELPER_DELEGATE_DESKTOP_H_
#define CHROME_BROWSER_UI_JAVASCRIPT_DIALOGS_JAVASCRIPT_DIALOG_TAB_HELPER_DELEGATE_DESKTOP_H_
#include <memory>
#include "base/callback_forward.h"
#include "base/macros.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
class JavaScriptDialogTabHelperDelegateDesktop
: public JavaScriptDialogTabHelperDelegate,
public BrowserListObserver,
public TabStripModelObserver {
public:
explicit JavaScriptDialogTabHelperDelegateDesktop(
content::WebContents* web_contents);
~JavaScriptDialogTabHelperDelegateDesktop() override;
// JavaScriptDialogTabHelperDelegate
base::WeakPtr<JavaScriptDialog> CreateNewDialog(
content::WebContents* alerting_web_contents,
const base::string16& title,
content::JavaScriptDialogType dialog_type,
const base::string16& message_text,
const base::string16& default_prompt_text,
content::JavaScriptDialogManager::DialogClosedCallback dialog_callback,
base::OnceClosure dialog_closed_callback) override;
void WillRunDialog() override;
void DidCloseDialog() override;
void SetTabNeedsAttention(bool attention) override;
bool IsWebContentsForemost() override;
bool IsApp() override;
// BrowserListObserver:
void OnBrowserSetLastActive(Browser* browser) override;
// TabStripModelObserver:
void OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) override;
private:
// Marks the tab as needing attention.
void SetTabNeedsAttentionImpl(bool attention,
TabStripModel* tab_strip_model,
int index);
// If this instance is observing a TabStripModel, then this member is not
// nullptr.
//
// This raw pointer is safe to use because the lifetime of this instance is
// tied to the corresponding WebContents, which is owned by the TabStripModel.
// Any time TabStripModel would give up ownership of the WebContents, it would
// first send either a TabStripModelChange::kRemoved or kReplaced
// via OnTabStripModelChanged() callback, which gives this instance the
// opportunity to stop observing the TabStripModel.
//
// A TabStripModel cannot be destroyed without first detaching all of its
// WebContents.
TabStripModel* tab_strip_model_being_observed_ = nullptr;
// The WebContents for the tab over which the dialog will be modal. This may
// be different from the WebContents that requested the dialog, such as with
// Chrome app <webview>s.
content::WebContents* web_contents_;
DISALLOW_COPY_AND_ASSIGN(JavaScriptDialogTabHelperDelegateDesktop);
};
#endif // CHROME_BROWSER_UI_JAVASCRIPT_DIALOGS_JAVASCRIPT_DIALOG_TAB_HELPER_DELEGATE_DESKTOP_H_
......@@ -115,12 +115,14 @@
#include "chrome/browser/flags/android/chrome_feature_list.h"
#include "chrome/browser/ui/android/context_menu_helper.h"
#include "chrome/browser/ui/android/view_android_helper.h"
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper_delegate_android.h"
#else
#include "chrome/browser/banners/app_banner_manager_desktop.h"
#include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
#include "chrome/browser/tab_contents/form_interaction_tab_helper.h"
#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
#include "chrome/browser/ui/intent_picker_tab_helper.h"
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper_delegate_desktop.h"
#include "chrome/browser/ui/sad_tab_helper.h"
#include "chrome/browser/ui/search/search_tab_helper.h"
#include "chrome/browser/ui/sync/browser_synced_tab_delegate.h"
......@@ -305,7 +307,9 @@ void TabHelpers::AttachTabHelpers(WebContents* web_contents) {
banners::AppBannerManagerAndroid::CreateForWebContents(web_contents);
}
ContextMenuHelper::CreateForWebContents(web_contents);
JavaScriptDialogTabHelper::CreateForWebContents(web_contents);
JavaScriptDialogTabHelper::CreateForWebContents(
web_contents,
std::make_unique<JavaScriptDialogTabHelperDelegateAndroid>(web_contents));
if (OomInterventionTabHelper::IsEnabled()) {
OomInterventionTabHelper::CreateForWebContents(web_contents);
}
......@@ -322,7 +326,9 @@ void TabHelpers::AttachTabHelpers(WebContents* web_contents) {
FormInteractionTabHelper::CreateForWebContents(web_contents);
FramebustBlockTabHelper::CreateForWebContents(web_contents);
IntentPickerTabHelper::CreateForWebContents(web_contents);
JavaScriptDialogTabHelper::CreateForWebContents(web_contents);
JavaScriptDialogTabHelper::CreateForWebContents(
web_contents,
std::make_unique<JavaScriptDialogTabHelperDelegateDesktop>(web_contents));
ManagePasswordsUIController::CreateForWebContents(web_contents);
pdf::PDFWebContentsHelper::CreateForWebContentsWithClient(
web_contents, std::make_unique<ChromePDFWebContentsHelperClient>());
......
......@@ -5,6 +5,7 @@
#include "chrome/browser/ui/views/javascript_dialog_views.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper_delegate_desktop.h"
#include "chrome/browser/ui/views/title_origin_label.h"
#include "components/constrained_window/constrained_window_views.h"
#include "content/public/browser/javascript_dialog_manager.h"
......@@ -16,23 +17,6 @@
JavaScriptDialogViews::~JavaScriptDialogViews() = default;
// static
base::WeakPtr<JavaScriptDialog> JavaScriptDialog::CreateNewDialog(
content::WebContents* parent_web_contents,
content::WebContents* alerting_web_contents,
const base::string16& title,
content::JavaScriptDialogType dialog_type,
const base::string16& message_text,
const base::string16& default_prompt_text,
content::JavaScriptDialogManager::DialogClosedCallback dialog_callback,
base::OnceClosure dialog_force_closed_callback) {
return (new JavaScriptDialogViews(
parent_web_contents, alerting_web_contents, title, dialog_type,
message_text, default_prompt_text, std::move(dialog_callback),
std::move(dialog_force_closed_callback)))
->weak_factory_.GetWeakPtr();
}
void JavaScriptDialogViews::CloseDialogWithoutCallback() {
dialog_callback_.Reset();
dialog_force_closed_callback_.Reset();
......@@ -122,3 +106,22 @@ JavaScriptDialogViews::JavaScriptDialogViews(
constrained_window::ShowWebModalDialogViews(this, parent_web_contents);
chrome::RecordDialogCreation(chrome::DialogIdentifier::JAVA_SCRIPT);
}
// Creates a new JS dialog. Note the two callbacks; |dialog_callback| is for
// user responses, while |dialog_force_closed_callback| is for when Views
// forces the dialog closed without a user reply.
base::WeakPtr<JavaScriptDialog>
JavaScriptDialogTabHelperDelegateDesktop::CreateNewDialog(
content::WebContents* alerting_web_contents,
const base::string16& title,
content::JavaScriptDialogType dialog_type,
const base::string16& message_text,
const base::string16& default_prompt_text,
content::JavaScriptDialogManager::DialogClosedCallback dialog_callback,
base::OnceClosure dialog_force_closed_callback) {
return (new JavaScriptDialogViews(
web_contents_, alerting_web_contents, title, dialog_type,
message_text, default_prompt_text, std::move(dialog_callback),
std::move(dialog_force_closed_callback)))
->weak_factory_.GetWeakPtr();
}
......@@ -24,19 +24,6 @@ class JavaScriptDialogViews : public JavaScriptDialog,
public:
~JavaScriptDialogViews() override;
// Creates a new JS dialog. Note the two callbacks; |dialog_callback| is for
// user responses, while |dialog_force_closed_callback| is for when Views
// forces the dialog closed without a user reply.
static base::WeakPtr<JavaScriptDialogViews> Create(
content::WebContents* parent_web_contents,
content::WebContents* alerting_web_contents,
const base::string16& title,
content::JavaScriptDialogType dialog_type,
const base::string16& message_text,
const base::string16& default_prompt_text,
content::JavaScriptDialogManager::DialogClosedCallback dialog_callback,
base::OnceClosure dialog_force_closed_callback);
// JavaScriptDialog:
void CloseDialogWithoutCallback() override;
base::string16 GetUserInput() override;
......@@ -57,6 +44,7 @@ class JavaScriptDialogViews : public JavaScriptDialog,
private:
friend class JavaScriptDialog;
friend class JavaScriptDialogTabHelperDelegateDesktop;
JavaScriptDialogViews(
content::WebContents* parent_web_contents,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment