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