Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: chrome/browser/printing/print_preview_tab_controller.cc

Issue 6221005: Print Preview: Store preview data in the print preview controller.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/browser/ui/tab_contents/tab_contents_wrapper.h"
14 #include "chrome/common/notification_details.h"
15 #include "chrome/common/notification_source.h"
16 #include "chrome/common/url_constants.h"
17
18 namespace printing {
19
20 PrintPreviewTabController::PrintPreviewTabController()
21 : waiting_for_new_preview_page_(false) {
22 }
23
24 PrintPreviewTabController::~PrintPreviewTabController() {}
25
26 // static
27 PrintPreviewTabController* PrintPreviewTabController::GetInstance() {
28 if (!g_browser_process)
29 return NULL;
30 return g_browser_process->print_preview_tab_controller();
31 }
32
33 TabContents* PrintPreviewTabController::GetOrCreatePreviewTab(
34 TabContents* initiator_tab, int browser_window_id ) {
35 DCHECK(initiator_tab);
36
37 // Get the print preview tab for |initiator_tab|.
38 TabContents* preview_tab = GetPrintPreviewForTab(initiator_tab);
39 if (preview_tab) {
40 // Show current preview tab.
41 preview_tab->Activate();
42 return preview_tab;
43 }
44 return CreatePrintPreviewTab(initiator_tab, browser_window_id);
45 }
46
47 void PrintPreviewTabController::Observe(NotificationType type,
48 const NotificationSource& source,
49 const NotificationDetails& details) {
50 TabContents* initiator_tab = NULL;
51 TabContents* preview_tab = NULL;
52 TabContents* source_tab = NULL;
53 NavigationController::LoadCommittedDetails* detail_info = NULL;
54
55 switch (type.value) {
56 case NotificationType::TAB_CONTENTS_DESTROYED: {
57 source_tab = Source<TabContents>(source).ptr();
58 break;
59 }
60 case NotificationType::NAV_ENTRY_COMMITTED: {
61 NavigationController* controller =
62 Source<NavigationController>(source).ptr();
63 source_tab = controller->tab_contents();
64 detail_info =
65 Details<NavigationController::LoadCommittedDetails>(details).ptr();
66 break;
67 }
68 default: {
69 NOTREACHED();
70 return;
71 }
72 }
73
74 DCHECK(source_tab);
75 preview_tab = GetPrintPreviewForTab(source_tab);
76
77 // |source_tab| is preview tab.
78 if (preview_tab == source_tab)
79 initiator_tab = GetInitiatorTab(source_tab);
80 else
81 initiator_tab = source_tab;
82
83 if (detail_info) {
84 PageTransition::Type transition_type =
85 detail_info->entry->transition_type();
86 NavigationType::Type nav_type = detail_info->type;
87
88 // Don't update/erase the map entry if the page has not changed.
89 if (transition_type == PageTransition::RELOAD ||
90 nav_type == NavigationType::SAME_PAGE) {
91 return;
92 }
93
94 // New |preview_tab| is created. Don't update/erase map entry.
95 if (waiting_for_new_preview_page_ &&
96 transition_type == PageTransition::LINK &&
97 nav_type == NavigationType::NEW_PAGE &&
98 source_tab == preview_tab) {
99 waiting_for_new_preview_page_ = false;
100 return;
101 }
102
103 // User navigated to a preview tab using forward/back button.
104 if (IsPrintPreviewTab(source_tab) &&
105 transition_type == PageTransition::FORWARD_BACK &&
106 nav_type == NavigationType::EXISTING_PAGE) {
107 return;
108 }
109 }
110
111 // If |source_tab| is |initiator_tab|, update the map entry.
112 if (source_tab == initiator_tab) {
113 preview_tab_map_[preview_tab] = NULL;
114 }
115
116 // If |source_tab| is |preview_tab|, erase the map entry.
117 if (source_tab == preview_tab) {
118 preview_tab_map_.erase(preview_tab);
119 RemoveObservers(preview_tab);
120 }
121
122 if (initiator_tab)
123 RemoveObservers(initiator_tab);
124 }
125
126 // static
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);
136 if (it != preview_tab_map_.end())
137 return preview_tab_map_[preview_tab];
138 return NULL;
139 }
140
141 TabContents* PrintPreviewTabController::GetPrintPreviewForTab(
142 TabContents* tab) {
143 PrintPreviewTabMap::iterator it = preview_tab_map_.find(tab);
144 if (it != preview_tab_map_.end())
145 return tab;
146
147 for (it = preview_tab_map_.begin(); it != preview_tab_map_.end(); ++it) {
148 if (it->second == tab)
149 return it->first;
150 }
151 return NULL;
152 }
153
154 TabContents* PrintPreviewTabController::CreatePrintPreviewTab(
155 TabContents* initiator_tab, int browser_window_id) {
156 Browser* current_browser = BrowserList::FindBrowserWithID(browser_window_id);
157 // Add a new tab next to initiator tab.
158 browser::NavigateParams params(current_browser,
159 GURL(chrome::kChromeUIPrintURL),
160 PageTransition::LINK);
161 params.disposition = NEW_FOREGROUND_TAB;
162 params.tabstrip_index = current_browser->tabstrip_model()->
163 GetWrapperIndex(initiator_tab) + 1;
164 browser::Navigate(&params);
165 TabContentsWrapper* preview_tab = params.target_contents;
166 preview_tab->tab_contents()->Activate();
167
168 // Add an entry to the map.
169 preview_tab_map_[preview_tab->tab_contents()] = initiator_tab;
170 waiting_for_new_preview_page_ = true;
171
172 AddObservers(initiator_tab);
173 AddObservers(preview_tab->tab_contents());
174
175 return preview_tab->tab_contents();
176 }
177
178 void PrintPreviewTabController::AddObservers(TabContents* tab) {
179 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED,
180 Source<TabContents>(tab));
181 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
182 Source<NavigationController>(&tab->controller()));
183 }
184
185 void PrintPreviewTabController::RemoveObservers(TabContents* tab) {
186 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED,
187 Source<TabContents>(tab));
188 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
189 Source<NavigationController>(&tab->controller()));
190 }
191
192 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698