OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" | 5 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet.h" | 11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet.h" |
12 #import "chrome/browser/ui/cocoa/single_web_contents_dialog_manager_cocoa.h" | 12 #import "chrome/browser/ui/cocoa/single_web_contents_dialog_manager_cocoa.h" |
13 #include "components/guest_view/browser/guest_view_base.h" | 13 #include "components/guest_view/browser/guest_view_base.h" |
14 #include "components/web_modal/web_contents_modal_dialog_manager.h" | |
15 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
16 | 15 |
17 using web_modal::WebContentsModalDialogManager; | 16 using web_modal::WebContentsModalDialogManager; |
18 | 17 |
| 18 std::unique_ptr<ConstrainedWindowMac> CreateAndShowWebModalDialogMac( |
| 19 ConstrainedWindowMacDelegate* delegate, |
| 20 content::WebContents* web_contents, |
| 21 id<ConstrainedWindowSheet> sheet) { |
| 22 ConstrainedWindowMac* window = |
| 23 new ConstrainedWindowMac(delegate, web_contents, sheet); |
| 24 window->ShowWebContentsModalDialog(); |
| 25 return std::unique_ptr<ConstrainedWindowMac>(window); |
| 26 } |
| 27 |
| 28 std::unique_ptr<ConstrainedWindowMac> CreateWebModalDialogMac( |
| 29 ConstrainedWindowMacDelegate* delegate, |
| 30 content::WebContents* web_contents, |
| 31 id<ConstrainedWindowSheet> sheet) { |
| 32 return std::unique_ptr<ConstrainedWindowMac>( |
| 33 new ConstrainedWindowMac(delegate, web_contents, sheet)); |
| 34 } |
| 35 |
19 ConstrainedWindowMac::ConstrainedWindowMac( | 36 ConstrainedWindowMac::ConstrainedWindowMac( |
20 ConstrainedWindowMacDelegate* delegate, | 37 ConstrainedWindowMacDelegate* delegate, |
21 content::WebContents* web_contents, | 38 content::WebContents* web_contents, |
22 id<ConstrainedWindowSheet> sheet) | 39 id<ConstrainedWindowSheet> sheet) |
23 : delegate_(delegate) { | 40 : delegate_(delegate), |
| 41 sheet_([sheet retain]) { |
24 DCHECK(sheet); | 42 DCHECK(sheet); |
25 | 43 |
26 // |web_contents| may be embedded within a chain of nested GuestViews. If it | 44 // |web_contents| may be embedded within a chain of nested GuestViews. If it |
27 // is, follow the chain of embedders to the outermost WebContents and use it. | 45 // is, follow the chain of embedders to the outermost WebContents and use it. |
28 web_contents = | 46 web_contents_ = |
29 guest_view::GuestViewBase::GetTopLevelWebContents(web_contents); | 47 guest_view::GuestViewBase::GetTopLevelWebContents(web_contents); |
30 | 48 |
31 auto manager = WebContentsModalDialogManager::FromWebContents(web_contents); | 49 native_manager_.reset( |
32 scoped_ptr<SingleWebContentsDialogManagerCocoa> native_manager( | 50 new SingleWebContentsDialogManagerCocoa(this, sheet_.get(), |
33 new SingleWebContentsDialogManagerCocoa(this, sheet, manager)); | 51 GetDialogManager())); |
34 manager->ShowDialogWithManager([sheet sheetWindow], | |
35 std::move(native_manager)); | |
36 } | 52 } |
37 | 53 |
38 ConstrainedWindowMac::~ConstrainedWindowMac() { | 54 ConstrainedWindowMac::~ConstrainedWindowMac() { |
39 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 55 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 56 native_manager_.reset(); |
40 DCHECK(!manager_); | 57 DCHECK(!manager_); |
41 } | 58 } |
42 | 59 |
| 60 void ConstrainedWindowMac::ShowWebContentsModalDialog() { |
| 61 scoped_ptr<SingleWebContentsDialogManagerCocoa> dialog_manager; |
| 62 dialog_manager.reset(native_manager_.release()); |
| 63 GetDialogManager()->ShowDialogWithManager( |
| 64 [sheet_.get() sheetWindow], std::move(dialog_manager)); |
| 65 } |
| 66 |
43 void ConstrainedWindowMac::CloseWebContentsModalDialog() { | 67 void ConstrainedWindowMac::CloseWebContentsModalDialog() { |
44 if (manager_) | 68 if (manager_) |
45 manager_->Close(); | 69 manager_->Close(); |
46 } | 70 } |
47 | 71 |
48 void ConstrainedWindowMac::OnDialogClosing() { | 72 void ConstrainedWindowMac::OnDialogClosing() { |
49 if (delegate_) | 73 if (delegate_) |
50 delegate_->OnConstrainedWindowClosed(this); | 74 delegate_->OnConstrainedWindowClosed(this); |
51 } | 75 } |
| 76 |
| 77 bool ConstrainedWindowMac::DialogWasShown() { |
| 78 // If the dialog was shown, |native_manager_| would have been released. |
| 79 return !native_manager_; |
| 80 } |
| 81 |
| 82 WebContentsModalDialogManager* ConstrainedWindowMac::GetDialogManager() { |
| 83 DCHECK(web_contents_); |
| 84 return WebContentsModalDialogManager::FromWebContents(web_contents_); |
| 85 } |
OLD | NEW |