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