Chromium Code Reviews| Index: chrome/browser/printing/print_preview_tab_controller.cc |
| diff --git a/chrome/browser/printing/print_preview_tab_controller.cc b/chrome/browser/printing/print_preview_tab_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8f90e2cdacbd5c4d12a9aeb39cd144a7dae61002 |
| --- /dev/null |
| +++ b/chrome/browser/printing/print_preview_tab_controller.cc |
| @@ -0,0 +1,207 @@ |
| +// Copyright (c) 2010 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 "chrome/browser/browser.h" |
|
Lei Zhang
2010/11/12 02:10:55
Plese use:
chrome/browser/ui/browser.h
chrome/bro
kmadhusu
2010/11/12 22:21:48
Done.
|
| +#include "chrome/browser/browser_list.h" |
| +#include "chrome/browser/browser_navigator.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/printing/print_preview_tab_controller.h" |
|
Lei Zhang
2010/11/12 02:10:55
nit: this goes on the top of the include list.
kmadhusu
2010/11/12 22:21:48
Done.
|
| +#include "chrome/browser/tab_contents/tab_contents.h" |
| +#include "chrome/browser/tabs/tab_strip_model.h" |
| +#include "chrome/common/url_constants.h" |
| + |
| +namespace printing { |
| + |
| +// static |
| +PrintPreviewTabController* PrintPreviewTabController::GetInstance() { |
| + if (!g_browser_process) |
| + return NULL; |
| + return g_browser_process->print_preview_tab_controller(); |
| +} |
| + |
| +PrintPreviewTabController::PrintPreviewTabController() |
| + : waiting_for_new_preview_page_(false) { |
| +} |
| + |
| +PrintPreviewTabController::~PrintPreviewTabController() { |
| + registrar_.RemoveAll(); |
|
Lei Zhang
2010/11/12 02:10:55
~NotificationRegistrar() does this already.
kmadhusu
2010/11/12 22:21:48
Done.
|
| + preview_tab_map_.clear(); |
| +} |
| + |
| +TabContents* PrintPreviewTabController::get_print_preview_tab( |
| + TabContents* current_tab, int browser_window_id ) { |
| + // Initiator tab is a preview tab. |
| + if (IsPrintPreviewTab(current_tab)) |
| + return current_tab; |
| + |
| + // Get the preview tab for initiator tab. |
| + TabContents* preview_tab = GetPreviewTab(current_tab); |
| + if (preview_tab != NULL) { |
| + // Show current preview tab. |
| + preview_tab->Activate(); |
| + return preview_tab; |
| + } |
| + return CreatePrintPreviewTab(current_tab, browser_window_id); |
| +} |
| + |
| +bool PrintPreviewTabController::IsPrintPreviewTab(TabContents* tab) { |
| + const GURL& url = tab->GetURL(); |
| + if (url.SchemeIs(chrome::kChromeUIScheme) && |
| + url.host() == chrome::kChromeUIPrintHost) |
| + return true; |
| + |
| + TabContents* preview_tab = GetPreviewTab(tab); |
| + return ((preview_tab != NULL)? (preview_tab == tab) : false); |
|
Lei Zhang
2010/11/12 02:10:55
You don't need the != NULL part here. Same goes fo
kmadhusu
2010/11/12 22:21:48
Done.
|
| +} |
| + |
| +Browser* PrintPreviewTabController::GetBrowserWindow(int browser_window_id) { |
| + return BrowserList::FindBrowserWithID(browser_window_id); |
| +} |
| + |
| +TabContents* PrintPreviewTabController::GetInitiatorTab( |
| + TabContents* preview_tab) { |
| + PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); |
| + if (it != preview_tab_map_.end()) |
| + return preview_tab_map_[preview_tab]; |
| + return NULL; |
| +} |
| + |
| +TabContents* PrintPreviewTabController::GetPreviewTab( |
| + TabContents* current_tab) { |
| + for (PrintPreviewTabMap::iterator it = preview_tab_map_.begin(); |
| + it != preview_tab_map_.end(); ++it) { |
| + if ((it->second == current_tab) || (it->first == current_tab)) |
| + return it->first; |
| + } |
| + return NULL; |
| +} |
| + |
| +void PrintPreviewTabController::AddObservers(TabContents* tab) { |
| + // Add TAB_CONTENTS_DESTROYED notification. |
| + registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, |
| + Source<TabContents>(tab)); |
| + |
| + // Add NAV_ENTRY_COMMITTED notification. |
| + registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
| + Source<NavigationController>(&tab->controller())); |
| +} |
| + |
| +void PrintPreviewTabController::RemoveObservers(TabContents* tab) { |
| + // Remove TAB_CONTENTS_DESTROYED notification. |
| + registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, |
| + Source<TabContents>(tab)); |
| + |
| + // Remove NAV_ENTRY_COMMITTED notification. |
| + registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, |
| + Source<NavigationController>(&tab->controller())); |
| +} |
| + |
| +TabContents* PrintPreviewTabController::CreatePrintPreviewTab( |
| + TabContents* initiator_tab, int browser_window_id) { |
| + Browser* current_browser = GetBrowserWindow(browser_window_id); |
| + // Add a new tab next to initiator tab. |
| + browser::NavigateParams params(current_browser, |
| + GURL(chrome::kChromeUIPrintURL), |
| + PageTransition::LINK); |
| + params.disposition = NEW_FOREGROUND_TAB; |
| + params.tabstrip_index = current_browser->tabstrip_model()-> |
| + GetIndexOfTabContents(initiator_tab)+1; |
| + browser::Navigate(¶ms); |
| + TabContents* preview_tab = params.target_contents; |
| + preview_tab->Activate(); |
| + |
| + // Add an entry to the map. |
| + preview_tab_map_[preview_tab] = initiator_tab; |
| + waiting_for_new_preview_page_ = true; |
| + |
| + AddObservers(initiator_tab); |
| + AddObservers(preview_tab); |
| + |
| + return preview_tab; |
| +} |
| + |
| +void PrintPreviewTabController::Observe(NotificationType type, |
| + const NotificationSource& source, |
| + const NotificationDetails& details) { |
| + TabContents* initiator_tab = NULL; |
| + TabContents* preview_tab = NULL; |
| + TabContents* source_tab = NULL; |
| + NavigationController::LoadCommittedDetails* detail_info = NULL; |
| + |
| + switch (type.value) { |
| + case NotificationType::TAB_CONTENTS_DESTROYED: { |
| + source_tab = Source<TabContents>(source).ptr(); |
| + break; |
| + } |
| + case NotificationType::NAV_ENTRY_COMMITTED: { |
| + NavigationController* controller = |
| + Source<NavigationController>(source).ptr(); |
| + source_tab = controller->tab_contents(); |
| + detail_info = |
| + Details<NavigationController::LoadCommittedDetails>(details).ptr(); |
| + break; |
| + } |
| + default: { |
| + NOTREACHED(); |
| + return; |
| + } |
| + } |
| + |
| + if (IsPrintPreviewTab(source_tab)) { |
| + // |source_tab| is preview tab. |
| + initiator_tab = GetInitiatorTab(source_tab); |
| + preview_tab = source_tab; |
| + } else { |
| + initiator_tab = source_tab; |
| + preview_tab = GetPreviewTab(source_tab); |
| + } |
| + |
| + if (detail_info != NULL) { |
| + PageTransition::Type transition_type = |
| + detail_info->entry->transition_type(); |
| + NavigationType::Type nav_type = detail_info->type; |
| + |
| + // Don't update/erase the map entry if the page has not changed. |
| + bool reload = (transition_type == PageTransition::RELOAD || |
| + nav_type == NavigationType::SAME_PAGE); |
| + if (reload) |
| + return; |
| + |
| + // New |preview_tab| is created. Don't update/erase map entry. |
| + bool is_preview_tab_loaded = (transition_type == PageTransition::LINK && |
| + nav_type == NavigationType::NEW_PAGE && |
| + source_tab == preview_tab); |
| + if (waiting_for_new_preview_page_ && is_preview_tab_loaded) { |
| + waiting_for_new_preview_page_ = false; |
| + return; |
| + } |
| + |
| + // User navigated to existing |preview_tab| using forward/back button. |
| + bool nav_to_prev_tab = (IsPrintPreviewTab(source_tab) && |
| + transition_type == PageTransition::FORWARD_BACK && |
| + nav_type == NavigationType::EXISTING_PAGE); |
| + if (nav_to_prev_tab) |
| + return; |
| + } |
| + |
| + // If |source_tab| is |initiator_tab|, update the map entry. |
| + if (source_tab == initiator_tab) { |
| + preview_tab_map_[preview_tab] = NULL; |
| + } |
| + |
| + // If |source_tab| is |preview_tab|, erase the map entry. |
| + if (source_tab == preview_tab) { |
| + EraseMapEntry(preview_tab); |
| + RemoveObservers(preview_tab); |
| + } |
| + |
| + if (initiator_tab != NULL) |
| + RemoveObservers(initiator_tab); |
| +} |
| + |
| +void PrintPreviewTabController::EraseMapEntry(TabContents* initiator_tab) { |
| + preview_tab_map_.erase(initiator_tab); |
| +} |
| + |
| +} // namespace printing |