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