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