OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/touch/tabs/touch_tab_strip_controller.h" | |
6 | |
7 #include "chrome/browser/extensions/extension_tab_helper.h" | |
8 #include "chrome/browser/favicon/favicon_tab_helper.h" | |
9 #include "chrome/browser/profiles/profile.h" | |
10 #include "chrome/browser/ui/browser.h" | |
11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
12 #include "chrome/browser/ui/touch/tabs/touch_tab.h" | |
13 #include "chrome/browser/ui/touch/tabs/touch_tab_strip.h" | |
14 #include "chrome/browser/ui/views/tabs/tab_renderer_data.h" | |
15 #include "skia/ext/image_operations.h" | |
16 #include "ui/gfx/codec/png_codec.h" | |
17 #include "ui/gfx/favicon_size.h" | |
18 | |
19 namespace { | |
20 | |
21 void CalcTouchIconTargetSize(int* width, int* height) { | |
22 if (*width > kTouchTargetIconSize || *height > kTouchTargetIconSize) { | |
23 // Too big, resize it maintaining the aspect ratio. | |
24 float aspect_ratio = static_cast<float>(*width) / | |
25 static_cast<float>(*height); | |
26 *height = kTouchTargetIconSize; | |
27 *width = static_cast<int>(aspect_ratio * *height); | |
28 if (*width > kTouchTargetIconSize) { | |
29 *width = kTouchTargetIconSize; | |
30 *height = static_cast<int>(*width / aspect_ratio); | |
31 } | |
32 } | |
33 } | |
34 | |
35 GURL GurlWithoutFragment(const GURL& gurl) { | |
tfarina
2011/06/17 00:06:41
would you rename this to GetURLWithoutFragment?
| |
36 url_canon::Replacements<char> replacements; | |
37 replacements.ClearUsername(); | |
38 replacements.ClearPassword(); | |
39 replacements.ClearQuery(); | |
40 replacements.ClearRef(); | |
41 return gurl.ReplaceComponents(replacements); | |
42 } | |
43 | |
44 } // namespace | |
45 | |
46 TouchTabStripController::TouchTabStripController(Browser* browser, | |
47 TabStripModel* model) | |
48 : BrowserTabStripController(browser, model) { | |
49 } | |
50 | |
51 TouchTabStripController::~TouchTabStripController() { | |
52 } | |
53 | |
54 void TouchTabStripController::TabDetachedAt(TabContentsWrapper* contents, | |
55 int model_index) { | |
56 if (consumer_.HasPendingRequests()) { | |
57 TouchTab* touch_tab = tabstrip()->GetTouchTabAtModelIndex(model_index); | |
58 consumer_.CancelAllRequestsForClientData(touch_tab); | |
59 } | |
60 } | |
61 | |
62 void TouchTabStripController::TabChangedAt(TabContentsWrapper* contents, | |
63 int model_index, | |
64 TabChangeType change_type) { | |
65 // Clear the large icon if we are loading a different URL in the same tab. | |
66 if (change_type == LOADING_ONLY) { | |
67 TouchTab* touch_tab = tabstrip()->GetTouchTabAtModelIndex(model_index); | |
68 if (!touch_tab->touch_icon().isNull()) { | |
69 GURL existing_tab_url = GurlWithoutFragment(touch_tab->data().url); | |
70 GURL page_url = GurlWithoutFragment(contents->tab_contents()->GetURL()); | |
71 // reset touch icon if the url are different | |
72 if (existing_tab_url != page_url) { | |
73 touch_tab->set_touch_icon(SkBitmap()); | |
74 consumer_.CancelAllRequestsForClientData(touch_tab); | |
75 } | |
76 } | |
77 } | |
78 | |
79 // Always call parent's method. | |
80 BrowserTabStripController::TabChangedAt(contents, model_index, change_type); | |
81 } | |
82 | |
83 void TouchTabStripController::SetTabRendererDataFromModel( | |
84 TabContents* contents, | |
85 int model_index, | |
86 TabRendererData* data, | |
87 TabStatus tab_status) { | |
88 // Call parent first. | |
89 BrowserTabStripController::SetTabRendererDataFromModel(contents, model_index, | |
90 data, tab_status); | |
91 if (tab_status == NEW_TAB) | |
92 return; | |
93 | |
94 // Use the touch icon if any. | |
95 TouchTab* touch_tab = tabstrip()->GetTouchTabAtModelIndex(model_index); | |
96 if (!touch_tab->touch_icon().isNull()) { | |
97 data->favicon = touch_tab->touch_icon(); | |
98 return; | |
99 } | |
100 | |
101 // In the case where we do not have a touch icon we scale up the small | |
102 // favicons (16x16) which originally are coming from NavigationEntry. | |
103 if (data->favicon.width() == kFaviconSize && | |
104 data->favicon.height() == kFaviconSize) | |
105 data->favicon = skia::ImageOperations::Resize(data->favicon, | |
106 skia::ImageOperations::RESIZE_BEST, kTouchTargetIconSize, | |
107 kTouchTargetIconSize); | |
108 | |
109 // Check if we have an outstanding request for this tab. | |
110 if (consumer_.HasPendingRequestsForClientData(touch_tab)) | |
111 consumer_.CancelAllRequestsForClientData(touch_tab); | |
112 | |
113 // Request touch icon. | |
114 GURL page_url = GurlWithoutFragment(contents->GetURL()); | |
115 FaviconService* favicon_service = profile()->GetFaviconService( | |
116 Profile::EXPLICIT_ACCESS); | |
117 if (favicon_service) { | |
118 CancelableRequestProvider::Handle h = | |
119 favicon_service->GetFaviconForURL( | |
120 page_url, | |
121 history::TOUCH_ICON | history::TOUCH_PRECOMPOSED_ICON, | |
122 &consumer_, | |
123 NewCallback(this, &TouchTabStripController::OnTouchIconAvailable)); | |
124 consumer_.SetClientData(favicon_service, h, touch_tab); | |
125 } | |
126 } | |
127 | |
128 const TouchTabStrip* TouchTabStripController::tabstrip() const { | |
129 return static_cast<const TouchTabStrip*>( | |
130 BrowserTabStripController::tabstrip()); | |
131 } | |
132 | |
133 void TouchTabStripController::OnTouchIconAvailable( | |
134 FaviconService::Handle h, | |
135 history::FaviconData favicon) { | |
136 // Abandon the request when there is no valid favicon. | |
137 if (!favicon.is_valid()) | |
138 return; | |
139 | |
140 // Retrieve the model_index from the TouchTab pointer received. | |
141 TouchTab* touch_tab = consumer_.GetClientDataForCurrentRequest(); | |
142 int model_index = tabstrip()->GetModelIndexOfBaseTab(touch_tab); | |
143 if (model_index == -1 || !IsValidIndex(model_index)) | |
144 return; | |
145 | |
146 // Try to decode the favicon, return on failure. | |
147 SkBitmap bitmap; | |
148 gfx::PNGCodec::Decode(favicon.image_data->front(), | |
149 favicon.image_data->size(), | |
150 &bitmap); | |
151 if (bitmap.isNull()) | |
152 return; | |
153 | |
154 // Rescale output, if needed, and assign to the TouchTab instance. | |
155 int width = bitmap.width(); | |
156 int height = bitmap.height(); | |
157 if (width == kTouchTargetIconSize && height == kTouchTargetIconSize) { | |
158 touch_tab->set_touch_icon(bitmap); | |
159 } else { | |
160 CalcTouchIconTargetSize(&width, &height); | |
161 touch_tab->set_touch_icon(skia::ImageOperations::Resize(bitmap, | |
162 skia::ImageOperations::RESIZE_BEST, width, height)); | |
163 } | |
164 | |
165 // Refresh UI since favicon changed | |
166 browser()->GetTabContentsAt(model_index)->NotifyNavigationStateChanged( | |
167 TabContents::INVALIDATE_TAB); | |
168 } | |
169 | |
tfarina
2011/06/17 00:06:41
remove this blank line.
| |
OLD | NEW |