1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved. 2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be 3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file. 4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/web_modal/popup_manager.h" 6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/web_modal/web_contents_modal_dialog_host.h" 8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/web_modal/web_contents_modal_dialog_manager.h" 9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/browser/web_contents.h" 10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/browser/web_contents_user_data.h" 11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/gfx/geometry/size.h" 12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 13116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing content::WebContents; 14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 15116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace web_modal { 16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 17116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace { 18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 19116680a4aac90f2aa7413d9095a592090648e557Ben Murdochconst char kPopupManagerUserDataKey[] = "PopupManager"; 20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This class provides a hook to get a PopupManager from a WebContents. 22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// The PopupManager is browser-scoped, but will use a FromWebContents API 23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// to attach to each WebContents in that browser. 24116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass PopupManagerRelay : public content::WebContentsUserData<PopupManager> { 25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public: 26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch explicit PopupManagerRelay(base::WeakPtr<PopupManager> manager) 27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch : manager_(manager) {} 28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual ~PopupManagerRelay() {} 30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::WeakPtr<PopupManager> manager_; 32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}; 33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} // namespace 35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 36116680a4aac90f2aa7413d9095a592090648e557Ben MurdochPopupManager::PopupManager(WebContentsModalDialogHost* host) 37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch : host_(host), 38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch weak_factory_(this) {} 39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 40116680a4aac90f2aa7413d9095a592090648e557Ben MurdochPopupManager::~PopupManager() { 41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 43116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PopupManager::ShowPopup(scoped_ptr<SinglePopupManager> manager) { 44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch content::WebContents* web_contents = manager->GetBoundWebContents(); 45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // TODO(gbillock): get rid of this when we handle bubbles 46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DCHECK(web_contents); 47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // TODO(gbillock): remove when we port the popup management logic to this 49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // class. 50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch NativeWebContentsModalDialog dialog = 51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch static_cast<NativeWebContentsModalDialog>(manager->popup()); 52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WebContentsModalDialogManager* wm_manager = 54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WebContentsModalDialogManager::FromWebContents(web_contents); 55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DCHECK(wm_manager); 56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch wm_manager->ShowModalDialog(dialog); 57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 59116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PopupManager::ShowModalDialog(NativePopup popup, 60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch content::WebContents* web_contents) { 61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // TODO make a new native popup manager and call ShowPopup. 62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // For now just lay off to WCMDM. 63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WebContentsModalDialogManager* manager = 64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WebContentsModalDialogManager::FromWebContents(web_contents); 65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch manager->ShowModalDialog(popup); 66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 68116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool PopupManager::IsWebModalDialogActive( 69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const content::WebContents* web_contents) const { 70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if (web_contents == NULL) 71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return false; 72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const WebContentsModalDialogManager* manager = 74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WebContentsModalDialogManager::FromWebContents(web_contents); 75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return manager && manager->IsDialogActive(); 76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 78116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PopupManager::WasFocused(const content::WebContents* web_contents) { 79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if (!IsWebModalDialogActive(web_contents)) 80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return; 81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const WebContentsModalDialogManager* manager = 83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WebContentsModalDialogManager::FromWebContents(web_contents); 84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if (manager) 85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch manager->FocusTopmostDialog(); 86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 88116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PopupManager::WillClose(NativePopup popup) { 89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 91116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PopupManager::RegisterWith(content::WebContents* web_contents) { 92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch web_contents->SetUserData(kPopupManagerUserDataKey, 93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch new PopupManagerRelay(weak_factory_.GetWeakPtr())); 94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // TODO(gbillock): Need to do something more extreme here to manage changing 95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // popup managers with popups in-flight? 96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 98116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PopupManager::UnregisterWith(content::WebContents* web_contents) { 99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch web_contents->RemoveUserData(kPopupManagerUserDataKey); 100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // TODO(gbillock): Need to do something more extreme here to manage changing 101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // popup managers with popups in-flight? 102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 104116680a4aac90f2aa7413d9095a592090648e557Ben MurdochPopupManager* PopupManager::FromWebContents( 105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch content::WebContents* web_contents) { 106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch PopupManagerRelay* relay = static_cast<PopupManagerRelay*>( 107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch web_contents->GetUserData(kPopupManagerUserDataKey)); 108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if (!relay) 109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return NULL; 110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return relay->manager_.get(); 112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 114116680a4aac90f2aa7413d9095a592090648e557Ben Murdochgfx::NativeView PopupManager::GetHostView() const { 115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // TODO(gbillock): replace this with a PopupManagerHost or something. 116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DCHECK(host_); 117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return host_->GetHostView(); 118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 120116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PopupManager::CloseAllDialogsForTesting( 121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch content::WebContents* web_contents) { 122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // TODO: re-implement, probably in terms of something in the host_, 123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // or of owned WCMDMs. 124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WebContentsModalDialogManager* manager = 125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch WebContentsModalDialogManager::FromWebContents(web_contents); 126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch manager->CloseAllDialogs(); 127116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 128116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} // namespace web_modal 130