OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/printing/print_preview_tab_controller.h" |
| 6 |
| 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/tab_contents/tab_contents.h" |
| 9 #include "chrome/browser/tabs/tab_strip_model.h" |
| 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_list.h" |
| 12 #include "chrome/browser/ui/browser_navigator.h" |
| 13 #include "chrome/common/notification_service.h" |
| 14 #include "chrome/common/url_constants.h" |
| 15 |
| 16 namespace printing { |
| 17 |
| 18 // static |
| 19 PrintPreviewTabController* PrintPreviewTabController::GetInstance() { |
| 20 if (!g_browser_process) |
| 21 return NULL; |
| 22 return g_browser_process->print_preview_tab_controller(); |
| 23 } |
| 24 |
| 25 PrintPreviewTabController::PrintPreviewTabController() |
| 26 : waiting_for_new_preview_page_(false) { |
| 27 } |
| 28 |
| 29 PrintPreviewTabController::~PrintPreviewTabController() { |
| 30 preview_tab_map_.clear(); |
| 31 } |
| 32 |
| 33 TabContents* PrintPreviewTabController::GetOrCreatePreviewTab( |
| 34 TabContents* initiator_tab, int browser_window_id ) { |
| 35 DCHECK(initiator_tab); |
| 36 |
| 37 if (IsPrintPreviewTab(initiator_tab)) |
| 38 return initiator_tab; |
| 39 |
| 40 // Get the print preview tab for |initiator_tab|. |
| 41 TabContents* preview_tab = GetPrintPreviewForTab(initiator_tab); |
| 42 if (preview_tab) { |
| 43 // Show current preview tab. |
| 44 preview_tab->Activate(); |
| 45 return preview_tab; |
| 46 } |
| 47 return CreatePrintPreviewTab(initiator_tab, browser_window_id); |
| 48 } |
| 49 |
| 50 bool PrintPreviewTabController::IsPrintPreviewTab(TabContents* tab) { |
| 51 const GURL& url = tab->GetURL(); |
| 52 return (url.SchemeIs(chrome::kChromeUIScheme) && |
| 53 url.host() == chrome::kChromeUIPrintHost); |
| 54 } |
| 55 |
| 56 TabContents* PrintPreviewTabController::GetInitiatorTab( |
| 57 TabContents* preview_tab) { |
| 58 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); |
| 59 if (it != preview_tab_map_.end()) |
| 60 return preview_tab_map_[preview_tab]; |
| 61 return NULL; |
| 62 } |
| 63 |
| 64 TabContents* PrintPreviewTabController::GetPrintPreviewForTab( |
| 65 TabContents* tab) { |
| 66 PrintPreviewTabMap::iterator it = preview_tab_map_.find(tab); |
| 67 if (it != preview_tab_map_.end()) |
| 68 return tab; |
| 69 |
| 70 for (it = preview_tab_map_.begin(); it != preview_tab_map_.end(); ++it) { |
| 71 if (it->second == tab) |
| 72 return it->first; |
| 73 } |
| 74 return NULL; |
| 75 } |
| 76 |
| 77 void PrintPreviewTabController::AddObservers(TabContents* tab) { |
| 78 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, |
| 79 Source<TabContents>(tab)); |
| 80 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
| 81 Source<NavigationController>(&tab->controller())); |
| 82 } |
| 83 |
| 84 void PrintPreviewTabController::RemoveObservers(TabContents* tab) { |
| 85 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, |
| 86 Source<TabContents>(tab)); |
| 87 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, |
| 88 Source<NavigationController>(&tab->controller())); |
| 89 } |
| 90 |
| 91 TabContents* PrintPreviewTabController::CreatePrintPreviewTab( |
| 92 TabContents* initiator_tab, int browser_window_id) { |
| 93 Browser* current_browser = BrowserList::FindBrowserWithID(browser_window_id); |
| 94 // Add a new tab next to initiator tab. |
| 95 browser::NavigateParams params(current_browser, |
| 96 GURL(chrome::kChromeUIPrintURL), |
| 97 PageTransition::LINK); |
| 98 params.disposition = NEW_FOREGROUND_TAB; |
| 99 params.tabstrip_index = current_browser->tabstrip_model()-> |
| 100 GetIndexOfTabContents(initiator_tab) + 1; |
| 101 browser::Navigate(¶ms); |
| 102 TabContents* preview_tab = params.target_contents; |
| 103 preview_tab->Activate(); |
| 104 |
| 105 // Add an entry to the map. |
| 106 preview_tab_map_[preview_tab] = initiator_tab; |
| 107 waiting_for_new_preview_page_ = true; |
| 108 |
| 109 AddObservers(initiator_tab); |
| 110 AddObservers(preview_tab); |
| 111 |
| 112 return preview_tab; |
| 113 } |
| 114 |
| 115 void PrintPreviewTabController::Observe(NotificationType type, |
| 116 const NotificationSource& source, |
| 117 const NotificationDetails& details) { |
| 118 TabContents* initiator_tab = NULL; |
| 119 TabContents* preview_tab = NULL; |
| 120 TabContents* source_tab = NULL; |
| 121 NavigationController::LoadCommittedDetails* detail_info = NULL; |
| 122 |
| 123 switch (type.value) { |
| 124 case NotificationType::TAB_CONTENTS_DESTROYED: { |
| 125 source_tab = Source<TabContents>(source).ptr(); |
| 126 break; |
| 127 } |
| 128 case NotificationType::NAV_ENTRY_COMMITTED: { |
| 129 NavigationController* controller = |
| 130 Source<NavigationController>(source).ptr(); |
| 131 source_tab = controller->tab_contents(); |
| 132 detail_info = |
| 133 Details<NavigationController::LoadCommittedDetails>(details).ptr(); |
| 134 break; |
| 135 } |
| 136 default: { |
| 137 NOTREACHED(); |
| 138 return; |
| 139 } |
| 140 } |
| 141 |
| 142 DCHECK(source_tab); |
| 143 preview_tab = GetPrintPreviewForTab(source_tab); |
| 144 |
| 145 // |source_tab| is preview tab. |
| 146 if (preview_tab == source_tab) |
| 147 initiator_tab = GetInitiatorTab(source_tab); |
| 148 else |
| 149 initiator_tab = source_tab; |
| 150 |
| 151 if (detail_info) { |
| 152 PageTransition::Type transition_type = |
| 153 detail_info->entry->transition_type(); |
| 154 NavigationType::Type nav_type = detail_info->type; |
| 155 |
| 156 // Don't update/erase the map entry if the page has not changed. |
| 157 if (transition_type == PageTransition::RELOAD || |
| 158 nav_type == NavigationType::SAME_PAGE) { |
| 159 return; |
| 160 } |
| 161 |
| 162 // New |preview_tab| is created. Don't update/erase map entry. |
| 163 if (waiting_for_new_preview_page_ && |
| 164 transition_type == PageTransition::LINK && |
| 165 nav_type == NavigationType::NEW_PAGE && |
| 166 source_tab == preview_tab) { |
| 167 waiting_for_new_preview_page_ = false; |
| 168 return; |
| 169 } |
| 170 |
| 171 // User navigated to a preview tab using forward/back button. |
| 172 if (IsPrintPreviewTab(source_tab) && |
| 173 transition_type == PageTransition::FORWARD_BACK && |
| 174 nav_type == NavigationType::EXISTING_PAGE) { |
| 175 return; |
| 176 } |
| 177 } |
| 178 |
| 179 // If |source_tab| is |initiator_tab|, update the map entry. |
| 180 if (source_tab == initiator_tab) { |
| 181 preview_tab_map_[preview_tab] = NULL; |
| 182 } |
| 183 |
| 184 // If |source_tab| is |preview_tab|, erase the map entry. |
| 185 if (source_tab == preview_tab) { |
| 186 preview_tab_map_.erase(preview_tab); |
| 187 RemoveObservers(preview_tab); |
| 188 } |
| 189 |
| 190 if (initiator_tab) |
| 191 RemoveObservers(initiator_tab); |
| 192 } |
| 193 |
| 194 } // namespace printing |
OLD | NEW |