Home | History | Annotate | Download | only in constrained_window
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
      6 
      7 #include "base/logging.h"
      8 #include "chrome/browser/guest_view/web_view/web_view_guest.h"
      9 #include "chrome/browser/ui/browser_finder.h"
     10 #include "chrome/browser/ui/browser_window.h"
     11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet.h"
     12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h"
     13 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
     14 #include "components/web_modal/web_contents_modal_dialog_manager.h"
     15 #include "content/public/browser/browser_thread.h"
     16 #include "content/public/browser/web_contents.h"
     17 
     18 using web_modal::WebContentsModalDialogManager;
     19 using web_modal::NativeWebContentsModalDialog;
     20 
     21 ConstrainedWindowMac::ConstrainedWindowMac(
     22     ConstrainedWindowMacDelegate* delegate,
     23     content::WebContents* web_contents,
     24     id<ConstrainedWindowSheet> sheet)
     25     : delegate_(delegate),
     26       web_contents_(NULL),
     27       sheet_([sheet retain]),
     28       shown_(false) {
     29   DCHECK(web_contents);
     30   WebViewGuest* web_view_guest = WebViewGuest::FromWebContents(web_contents);
     31   // For embedded WebContents, use the embedder's WebContents for constrained
     32   // window.
     33   web_contents_ = web_view_guest && web_view_guest->embedder_web_contents() ?
     34                       web_view_guest->embedder_web_contents() : web_contents;
     35   DCHECK(sheet_.get());
     36   WebContentsModalDialogManager* web_contents_modal_dialog_manager =
     37       WebContentsModalDialogManager::FromWebContents(web_contents_);
     38   web_contents_modal_dialog_manager->ShowModalDialog(this);
     39 }
     40 
     41 ConstrainedWindowMac::~ConstrainedWindowMac() {
     42   CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
     43 }
     44 
     45 void ConstrainedWindowMac::ShowWebContentsModalDialog() {
     46   if (shown_)
     47     return;
     48 
     49   NSWindow* parent_window = GetParentWindow();
     50   NSView* parent_view = GetSheetParentViewForWebContents(web_contents_);
     51   if (!parent_window || !parent_view)
     52     return;
     53 
     54   shown_ = true;
     55   ConstrainedWindowSheetController* controller =
     56       [ConstrainedWindowSheetController
     57           controllerForParentWindow:parent_window];
     58   [controller showSheet:sheet_ forParentView:parent_view];
     59 }
     60 
     61 void ConstrainedWindowMac::CloseWebContentsModalDialog() {
     62   [[ConstrainedWindowSheetController controllerForSheet:sheet_]
     63       closeSheet:sheet_];
     64   // TODO(gbillock): get this object in config, not from a global.
     65   WebContentsModalDialogManager* web_contents_modal_dialog_manager =
     66       WebContentsModalDialogManager::FromWebContents(web_contents_);
     67 
     68   // Will result in the delegate being deleted.
     69   if (delegate_)
     70     delegate_->OnConstrainedWindowClosed(this);
     71 
     72   // Will cause this object to be deleted.
     73   web_contents_modal_dialog_manager->WillClose(this);
     74 }
     75 
     76 void ConstrainedWindowMac::FocusWebContentsModalDialog() {
     77 }
     78 
     79 void ConstrainedWindowMac::PulseWebContentsModalDialog() {
     80   [[ConstrainedWindowSheetController controllerForSheet:sheet_]
     81       pulseSheet:sheet_];
     82 }
     83 
     84 NativeWebContentsModalDialog ConstrainedWindowMac::GetNativeDialog() {
     85   // TODO(wittman): Ultimately this should be changed to the
     86   // ConstrainedWindowSheet pointer, in conjunction with the corresponding
     87   // changes to NativeWebContentsModalDialogManagerCocoa.
     88   return this;
     89 }
     90 
     91 NSWindow* ConstrainedWindowMac::GetParentWindow() const {
     92   // Tab contents in a tabbed browser may not be inside a window. For this
     93   // reason use a browser window if possible.
     94   Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
     95   if (browser)
     96     return browser->window()->GetNativeWindow();
     97 
     98   return web_contents_->GetTopLevelNativeWindow();
     99 }
    100