1 // Copyright (c) 2011 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_frame/infobars/infobar_manager.h" 6 7 #include "base/logging.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "chrome_frame/infobars/internal/host_window_manager.h" 10 #include "chrome_frame/infobars/internal/infobar_window.h" 11 12 // Connects InfobarWindow to HostWindowManager, and exposes the result as an 13 // InfobarManager. 14 class InfobarManagerImpl 15 : public InfobarManager, 16 public InfobarWindow::Host, 17 public HostWindowManager::Delegate { 18 public: 19 explicit InfobarManagerImpl(HostWindowManager* manager); 20 21 // Implementation of InfobarManager 22 virtual bool Show(InfobarContent* content, InfobarType type); 23 virtual void Hide(InfobarType type); 24 virtual void HideAll(); 25 26 // Implementation of HostWindowManager::Delegate 27 virtual void AdjustDisplacedWindowDimensions(RECT* rect); 28 29 // Implementation of InfobarWindow::Host 30 virtual HWND GetContainerWindow(); 31 virtual void UpdateLayout(); 32 33 private: 34 // Not owned by this instance. 35 HostWindowManager* manager_; 36 // Infobar windows. 37 scoped_ptr<InfobarWindow> infobars_[END_OF_INFOBAR_TYPE]; 38 DISALLOW_COPY_AND_ASSIGN(InfobarManagerImpl); 39 }; // class InfobarManagerImpl 40 41 InfobarManagerImpl::InfobarManagerImpl(HostWindowManager* manager) 42 : manager_(manager) { 43 for (int index = 0; index < END_OF_INFOBAR_TYPE; ++index) { 44 infobars_[index].reset( 45 new InfobarWindow(static_cast<InfobarType>(index))); 46 infobars_[index]->SetHost(this); 47 } 48 } 49 50 bool InfobarManagerImpl::Show(InfobarContent* content, InfobarType type) { 51 DCHECK(type >= FIRST_INFOBAR_TYPE && type < END_OF_INFOBAR_TYPE); 52 return infobars_[type]->Show(content); 53 } 54 55 void InfobarManagerImpl::Hide(InfobarType type) { 56 DCHECK(type >= FIRST_INFOBAR_TYPE && type < END_OF_INFOBAR_TYPE); 57 infobars_[type]->Hide(); 58 } 59 60 void InfobarManagerImpl::HideAll() { 61 for (int index = 0; index < END_OF_INFOBAR_TYPE; ++index) 62 Hide(static_cast<InfobarType>(index)); 63 } 64 65 void InfobarManagerImpl::AdjustDisplacedWindowDimensions(RECT* rect) { 66 for (int index = 0; index < END_OF_INFOBAR_TYPE; ++index) { 67 if (infobars_[index] != NULL) 68 infobars_[index]->ReserveSpace(rect); 69 } 70 } 71 72 HWND InfobarManagerImpl::GetContainerWindow() { 73 return *manager_; 74 } 75 76 void InfobarManagerImpl::UpdateLayout() { 77 manager_->UpdateLayout(); 78 } 79 80 InfobarManager::~InfobarManager() { 81 } 82 83 InfobarManager* InfobarManager::Get(HWND tab_window) { 84 HostWindowManager::Delegate* delegate = 85 HostWindowManager::GetDelegateForHwnd(tab_window); 86 87 if (delegate != NULL) 88 return static_cast<InfobarManagerImpl*>(delegate); 89 90 scoped_ptr<HostWindowManager> host_manager(new HostWindowManager()); 91 92 // Transferred to host_manager in call to Initialize. 93 InfobarManagerImpl* infobar_manager = new InfobarManagerImpl( 94 host_manager.get()); 95 96 if (host_manager->Initialize(tab_window, infobar_manager)) { 97 host_manager.release(); // takes ownership of itself 98 return infobar_manager; 99 } 100 101 return NULL; 102 } 103