Commit 5f1263bb authored by andresantoso's avatar andresantoso Committed by Commit bot

MacViews: Unify web contents modal dialog types

The #if around NativeWebContentsModalDialog is a problem for building
the MacViews browser. Unify them to be gfx::NativeWindow on all platforms.
Refactor the cocoa side to move code from ConstrainedWindowMac into
SingleWebContentsDialogManagerCocoa, while keeping
ConstrainedWindowMac's interface the same as before.
The Views side only needs simple changes from NativeView to NativeWindow.

BUG=425229

Review URL: https://codereview.chromium.org/866263008

Cr-Commit-Position: refs/heads/master@{#318813}
parent 62a1ccd2
......@@ -197,6 +197,10 @@ void ShowCertificateViewer(content::WebContents* web_contents,
// NOOP
}
- (NSWindow*)sheetWindow {
return panel_;
}
- (void)onConstrainedWindowClosed {
panel_.reset();
constrainedWindow_.reset();
......
......@@ -72,9 +72,7 @@ class ConstrainedWebDialogDelegateViewMac :
void ReleaseWebContentsOnDialogClose() override {
return impl_->ReleaseWebContentsOnDialogClose();
}
NativeWebContentsModalDialog GetNativeDialog() override {
return constrained_window_->GetNativeDialog();
}
NativeWebContentsModalDialog GetNativeDialog() override { return window_; }
WebContents* GetWebContents() override { return impl_->GetWebContents(); }
gfx::Size GetMinimumSize() const override {
NOTIMPLEMENTED();
......
......@@ -73,4 +73,8 @@
[customWindow_ setFrameOrigin:origin];
}
- (NSWindow*)sheetWindow {
return customWindow_;
}
@end
......@@ -7,13 +7,11 @@
#import <Cocoa/Cocoa.h>
#include "base/mac/scoped_nsobject.h"
#include "components/web_modal/native_web_contents_modal_dialog.h"
namespace content {
class WebContents;
}
class ConstrainedWindowMac;
class SingleWebContentsDialogManagerCocoa;
@protocol ConstrainedWindowSheet;
// A delegate for a constrained window. The delegate is notified when the
......@@ -28,29 +26,25 @@ class ConstrainedWindowMacDelegate {
// should delete the instance when the window is closed.
class ConstrainedWindowMac {
public:
ConstrainedWindowMac(
ConstrainedWindowMacDelegate* delegate,
content::WebContents* web_contents,
id<ConstrainedWindowSheet> sheet);
virtual ~ConstrainedWindowMac();
void ShowWebContentsModalDialog();
// Closes the constrained window and deletes this instance.
void CloseWebContentsModalDialog();
void FocusWebContentsModalDialog();
void PulseWebContentsModalDialog();
web_modal::NativeWebContentsModalDialog GetNativeDialog();
ConstrainedWindowMac(ConstrainedWindowMacDelegate* delegate,
content::WebContents* web_contents,
id<ConstrainedWindowSheet> sheet);
~ConstrainedWindowMac();
private:
ConstrainedWindowMacDelegate* delegate_; // weak, owns us.
// Closes the constrained window.
void CloseWebContentsModalDialog();
// The WebContents that owns and constrains this ConstrainedWindowMac. Weak.
content::WebContents* web_contents_;
SingleWebContentsDialogManagerCocoa* manager() const { return manager_; }
void set_manager(SingleWebContentsDialogManagerCocoa* manager) {
manager_ = manager;
}
base::scoped_nsprotocol<id<ConstrainedWindowSheet>> sheet_;
// Called by |manager_| when the dialog is closing.
void OnDialogClosing();
// This is true if the constrained window has been shown.
bool shown_;
private:
ConstrainedWindowMacDelegate* delegate_; // weak, owns us.
SingleWebContentsDialogManagerCocoa* manager_; // weak, owned by WCMDM.
};
#endif // CHROME_BROWSER_UI_COCOA_CONSTRAINED_WINDOW_CONSTRAINED_WINDOW_MAC_
......@@ -4,89 +4,46 @@
#include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
#include "base/memory/scoped_ptr.h"
#include "base/logging.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h"
#import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
#include "components/web_modal/popup_manager.h"
#import "chrome/browser/ui/cocoa/single_web_contents_dialog_manager_cocoa.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/guest_view/guest_view_base.h"
using web_modal::WebContentsModalDialogManager;
using web_modal::NativeWebContentsModalDialog;
ConstrainedWindowMac::ConstrainedWindowMac(
ConstrainedWindowMacDelegate* delegate,
content::WebContents* web_contents,
id<ConstrainedWindowSheet> sheet)
: delegate_(delegate),
web_contents_(NULL),
sheet_([sheet retain]),
shown_(false) {
DCHECK(web_contents);
: delegate_(delegate) {
DCHECK(sheet);
extensions::GuestViewBase* guest_view =
extensions::GuestViewBase::FromWebContents(web_contents);
// For embedded WebContents, use the embedder's WebContents for constrained
// window.
web_contents_ = guest_view && guest_view->embedder_web_contents() ?
guest_view->embedder_web_contents() : web_contents;
DCHECK(sheet_.get());
web_modal::PopupManager* popup_manager =
web_modal::PopupManager::FromWebContents(web_contents_);
if (popup_manager)
popup_manager->ShowModalDialog(this, web_contents_);
web_contents = guest_view && guest_view->embedder_web_contents() ?
guest_view->embedder_web_contents() : web_contents;
auto manager = WebContentsModalDialogManager::FromWebContents(web_contents);
scoped_ptr<SingleWebContentsDialogManagerCocoa> native_manager(
new SingleWebContentsDialogManagerCocoa(this, sheet, manager));
manager->ShowDialogWithManager([sheet sheetWindow], native_manager.Pass());
}
ConstrainedWindowMac::~ConstrainedWindowMac() {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
}
void ConstrainedWindowMac::ShowWebContentsModalDialog() {
if (shown_)
return;
NSWindow* parent_window = web_contents_->GetTopLevelNativeWindow();
NSView* parent_view = GetSheetParentViewForWebContents(web_contents_);
if (!parent_window || !parent_view)
return;
shown_ = true;
ConstrainedWindowSheetController* controller =
[ConstrainedWindowSheetController
controllerForParentWindow:parent_window];
[controller showSheet:sheet_ forParentView:parent_view];
DCHECK(!manager_);
}
void ConstrainedWindowMac::CloseWebContentsModalDialog() {
[[ConstrainedWindowSheetController controllerForSheet:sheet_]
closeSheet:sheet_];
// TODO(gbillock): get this object in config, not from a global.
WebContentsModalDialogManager* web_contents_modal_dialog_manager =
WebContentsModalDialogManager::FromWebContents(web_contents_);
if (manager_)
manager_->Close();
}
// Will result in the delegate being deleted.
void ConstrainedWindowMac::OnDialogClosing() {
if (delegate_)
delegate_->OnConstrainedWindowClosed(this);
// Will cause this object to be deleted.
web_contents_modal_dialog_manager->WillClose(this);
}
void ConstrainedWindowMac::FocusWebContentsModalDialog() {
}
void ConstrainedWindowMac::PulseWebContentsModalDialog() {
[[ConstrainedWindowSheetController controllerForSheet:sheet_]
pulseSheet:sheet_];
}
NativeWebContentsModalDialog ConstrainedWindowMac::GetNativeDialog() {
// TODO(wittman): Ultimately this should be changed to the
// ConstrainedWindowSheet pointer, in conjunction with the corresponding
// changes to NativeWebContentsModalDialogManagerCocoa.
return this;
}
......@@ -24,6 +24,8 @@
- (void)updateSheetPosition;
@property(readonly, nonatomic) NSWindow* sheetWindow;
@end
#endif // CHROME_BROWSER_UI_COCOA_CONSTRAINED_WINDOW_CONSTRAINED_WINDOW_SHEET_H_
......@@ -56,6 +56,10 @@ const int kSystemSheetReturnCode = 77;
- (void)updateSheetPosition {
}
- (NSWindow*)sheetWindow {
return [alert_ window];
}
- (void)alertDidEnd:(NSAlert *)alert
returnCode:(NSInteger)returnCode
ctxInfo:(void *)contextInfo {
......
// Copyright 2015 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_COCOA_SINGLE_WEB_CONTENTS_DIALOG_MANAGER_COCOA_H_
#define CHROME_BROWSER_UI_COCOA_SINGLE_WEB_CONTENTS_DIALOG_MANAGER_COCOA_H_
#import "base/mac/scoped_nsobject.h"
#include "components/web_modal/single_web_contents_dialog_manager.h"
class ConstrainedWindowMac;
@protocol ConstrainedWindowSheet;
// Cocoa implementation of web_modal::SingleWebContentsDialogManager.
class SingleWebContentsDialogManagerCocoa
: public web_modal::SingleWebContentsDialogManager {
public:
SingleWebContentsDialogManagerCocoa(
ConstrainedWindowMac* client,
id<ConstrainedWindowSheet> sheet,
web_modal::SingleWebContentsDialogManagerDelegate* delegate);
~SingleWebContentsDialogManagerCocoa() override;
// SingleWebContentsDialogManager overrides.
void Show() override;
void Hide() override;
void Close() override;
void Focus() override;
void Pulse() override;
void HostChanged(web_modal::WebContentsModalDialogHost* new_host) override;
web_modal::NativeWebContentsModalDialog dialog() override;
private:
ConstrainedWindowMac* client_; // Weak. Can be null.
base::scoped_nsprotocol<id<ConstrainedWindowSheet>> sheet_;
// Weak. Owns this.
web_modal::SingleWebContentsDialogManagerDelegate* delegate_;
bool shown_;
DISALLOW_COPY_AND_ASSIGN(SingleWebContentsDialogManagerCocoa);
};
#endif // CHROME_BROWSER_UI_COCOA_SINGLE_WEB_CONTENTS_DIALOG_MANAGER_COCOA_H_
// Copyright 2015 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.
#import "chrome/browser/ui/cocoa/single_web_contents_dialog_manager_cocoa.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h"
#import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
using web_modal::NativeWebContentsModalDialog;
using web_modal::SingleWebContentsDialogManagerDelegate;
SingleWebContentsDialogManagerCocoa::SingleWebContentsDialogManagerCocoa(
ConstrainedWindowMac* client,
id<ConstrainedWindowSheet> sheet,
web_modal::SingleWebContentsDialogManagerDelegate* delegate)
: client_(client),
sheet_([sheet retain]),
delegate_(delegate),
shown_(false) {
if (client)
client->set_manager(this);
}
SingleWebContentsDialogManagerCocoa::~SingleWebContentsDialogManagerCocoa() {
}
void SingleWebContentsDialogManagerCocoa::Show() {
if (shown_)
return;
content::WebContents* web_contents = delegate_->GetWebContents();
NSWindow* parent_window = web_contents->GetTopLevelNativeWindow();
NSView* parent_view = GetSheetParentViewForWebContents(web_contents);
if (!parent_window || !parent_view)
return;
shown_ = true;
[[ConstrainedWindowSheetController controllerForParentWindow:parent_window]
showSheet:sheet_ forParentView:parent_view];
}
void SingleWebContentsDialogManagerCocoa::Hide() {
}
void SingleWebContentsDialogManagerCocoa::Close() {
[[ConstrainedWindowSheetController controllerForSheet:sheet_]
closeSheet:sheet_];
if (client_) {
client_->set_manager(nullptr);
client_->OnDialogClosing(); // |client_| might delete itself here.
client_ = nullptr;
}
delegate_->WillClose(dialog());
}
void SingleWebContentsDialogManagerCocoa::Focus() {
}
void SingleWebContentsDialogManagerCocoa::Pulse() {
[[ConstrainedWindowSheetController controllerForSheet:sheet_]
pulseSheet:sheet_];
}
void SingleWebContentsDialogManagerCocoa::HostChanged(
web_modal::WebContentsModalDialogHost* new_host) {
}
NativeWebContentsModalDialog SingleWebContentsDialogManagerCocoa::dialog() {
return [sheet_ sheetWindow];
}
namespace web_modal {
SingleWebContentsDialogManager*
WebContentsModalDialogManager::CreateNativeWebModalManager(
NativeWebContentsModalDialog dialog,
SingleWebContentsDialogManagerDelegate* delegate) {
base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
[[CustomConstrainedWindowSheet alloc] initWithCustomWindow:dialog]);
return new SingleWebContentsDialogManagerCocoa(nullptr, sheet, delegate);
}
} // namespace web_modal
......@@ -227,6 +227,10 @@ void ShowSSLClientCertificateSelector(
// NOOP
}
- (NSWindow*)sheetWindow {
return panel_;
}
- (void)onConstrainedWindowClosed {
observer_->StopObserving();
panel_.reset();
......
......@@ -7,6 +7,7 @@
#import <Cocoa/Cocoa.h>
#import "base/mac/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
#include "chrome/browser/ui/tab_modal_confirm_dialog.h"
......
// Copyright (c) 2013 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 "components/web_modal/web_contents_modal_dialog_manager.h"
#include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
#include "components/web_modal/single_web_contents_dialog_manager.h"
using web_modal::NativeWebContentsModalDialog;
namespace {
class NativeWebContentsModalDialogManagerCocoa
: public web_modal::SingleWebContentsDialogManager {
public:
NativeWebContentsModalDialogManagerCocoa(
NativeWebContentsModalDialog dialog)
: dialog_(dialog) {
}
~NativeWebContentsModalDialogManagerCocoa() override {}
// SingleWebContentsDialogManager overrides
void Show() override {
GetConstrainedWindowMac(dialog())->ShowWebContentsModalDialog();
}
void Hide() override {}
void Close() override {
GetConstrainedWindowMac(dialog())->CloseWebContentsModalDialog();
}
void Focus() override {
GetConstrainedWindowMac(dialog())->FocusWebContentsModalDialog();
}
void Pulse() override {
GetConstrainedWindowMac(dialog())->PulseWebContentsModalDialog();
}
void HostChanged(web_modal::WebContentsModalDialogHost* new_host) override {}
NativeWebContentsModalDialog dialog() override { return dialog_; }
private:
static ConstrainedWindowMac* GetConstrainedWindowMac(
NativeWebContentsModalDialog dialog) {
return static_cast<ConstrainedWindowMac*>(dialog);
}
// In mac this is a pointer to a ConstrainedWindowMac.
// TODO(gbillock): Replace this casting system with a more typesafe call path.
NativeWebContentsModalDialog dialog_;
DISALLOW_COPY_AND_ASSIGN(NativeWebContentsModalDialogManagerCocoa);
};
} // namespace
namespace web_modal {
SingleWebContentsDialogManager*
WebContentsModalDialogManager::CreateNativeWebModalManager(
NativeWebContentsModalDialog dialog,
SingleWebContentsDialogManagerDelegate* native_delegate) {
return new NativeWebContentsModalDialogManagerCocoa(dialog);
}
} // namespace web_modal
......@@ -125,7 +125,7 @@ class ConstrainedWebDialogDelegateViews
// ConstrainedWebDialogDelegate:
web_modal::NativeWebContentsModalDialog GetNativeDialog() override {
return view_->GetWidget()->GetNativeView();
return view_->GetWidget()->GetNativeWindow();
}
private:
......@@ -246,7 +246,7 @@ class ConstrainedWebDialogDelegateViewViews
web_modal::PopupManager* popup_manager =
web_modal::PopupManager::FromWebContents(
initiator_observer_.web_contents());
popup_manager->ShowModalDialog(GetWidget()->GetNativeView(),
popup_manager->ShowModalDialog(GetWidget()->GetNativeWindow(),
initiator_observer_.web_contents());
}
}
......
......@@ -220,7 +220,7 @@ class NativeWebContentsModalDialogManagerViews
#endif
// Will cause this object to be deleted.
native_delegate_->WillClose(widget->GetNativeView());
native_delegate_->WillClose(widget->GetNativeWindow());
}
SingleWebContentsDialogManagerDelegate* native_delegate_;
......
......@@ -651,6 +651,8 @@
'browser/ui/cocoa/screen_capture_notification_ui_cocoa.h',
'browser/ui/cocoa/screen_capture_notification_ui_cocoa.mm',
'browser/ui/cocoa/simple_message_box_mac.mm',
'browser/ui/cocoa/single_web_contents_dialog_manager_cocoa.h',
'browser/ui/cocoa/single_web_contents_dialog_manager_cocoa.mm',
'browser/ui/cocoa/sprite_view.h',
'browser/ui/cocoa/sprite_view.mm',
'browser/ui/cocoa/ssl_client_certificate_selector_cocoa.h',
......@@ -731,7 +733,6 @@
'browser/ui/cocoa/view_id_util.h',
'browser/ui/cocoa/view_id_util.mm',
'browser/ui/cocoa/view_resizer.h',
'browser/ui/cocoa/web_contents_modal_dialog_manager_cocoa.mm',
'browser/ui/cocoa/web_dialog_window_controller.h',
'browser/ui/cocoa/web_dialog_window_controller.mm',
'browser/ui/cocoa/website_settings/permission_bubble_cocoa.h',
......
......@@ -11,21 +11,8 @@ namespace web_modal {
// TODO(gbillock): rename this file
#if defined(OS_MACOSX)
// Use a void* since none of the gfx::Native* types are suitable for
// representing the web contents modal dialog under Cocoa.
typedef void* NativeWebContentsModalDialog;
#else
typedef gfx::NativeView NativeWebContentsModalDialog;
#endif
#if defined(OS_MACOSX)
// Use a void* since none of the gfx::Native* types are suitable for
// representing a popup window under Cocoa.
typedef void* NativePopup;
#else
typedef gfx::NativeView NativePopup;
#endif
using NativeWebContentsModalDialog = gfx::NativeWindow;
using NativePopup = gfx::NativeWindow;
} // namespace web_modal
......
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