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 |
| 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. |
| 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) { |
| 106 data->favicon = skia::ImageOperations::Resize(data->favicon, |
| 107 skia::ImageOperations::RESIZE_BEST, TouchTab::kTouchTargetIconSize, |
| 108 TouchTab::kTouchTargetIconSize); |
| 109 } |
| 110 |
| 111 // Check if we have an outstanding request for this tab. |
| 112 if (consumer_.HasPendingRequests()) |
| 113 consumer_.CancelAllRequestsForClientData(touch_tab); |
| 114 |
| 115 // Request touch icon. |
| 116 GURL page_url = GetURLWithoutFragment(contents->GetURL()); |
| 117 FaviconService* favicon_service = profile()->GetFaviconService( |
| 118 Profile::EXPLICIT_ACCESS); |
| 119 if (favicon_service) { |
| 120 CancelableRequestProvider::Handle h = |
| 121 favicon_service->GetFaviconForURL( |
| 122 page_url, |
| 123 history::TOUCH_ICON | history::TOUCH_PRECOMPOSED_ICON, |
| 124 &consumer_, |
| 125 NewCallback(this, &TouchTabStripController::OnTouchIconAvailable)); |
| 126 consumer_.SetClientData(favicon_service, h, touch_tab); |
| 127 } |
| 128 } |
| 129 |
| 130 const TouchTabStrip* TouchTabStripController::tabstrip() const { |
| 131 return static_cast<const TouchTabStrip*>( |
| 132 BrowserTabStripController::tabstrip()); |
| 133 } |
| 134 |
| 135 void TouchTabStripController::OnTouchIconAvailable( |
| 136 FaviconService::Handle h, |
| 137 history::FaviconData favicon) { |
| 138 // Abandon the request when there is no valid favicon. |
| 139 if (!favicon.is_valid()) |
| 140 return; |
| 141 |
| 142 // Retrieve the model_index from the TouchTab pointer received. |
| 143 TouchTab* touch_tab = consumer_.GetClientDataForCurrentRequest(); |
| 144 int model_index = tabstrip()->GetModelIndexOfBaseTab(touch_tab); |
| 145 if (!IsValidIndex(model_index)) |
| 146 return; |
| 147 |
| 148 // Try to decode the favicon, return on failure. |
| 149 SkBitmap bitmap; |
| 150 gfx::PNGCodec::Decode(favicon.image_data->front(), |
| 151 favicon.image_data->size(), |
| 152 &bitmap); |
| 153 if (bitmap.isNull()) |
| 154 return; |
| 155 |
| 156 // Rescale output, if needed, and assign to the TouchTab instance. |
| 157 int width = bitmap.width(); |
| 158 int height = bitmap.height(); |
| 159 if (width == TouchTab::kTouchTargetIconSize && |
| 160 height == TouchTab::kTouchTargetIconSize) { |
| 161 touch_tab->set_touch_icon(bitmap); |
| 162 } else { |
| 163 CalcTouchIconTargetSize(&width, &height); |
| 164 touch_tab->set_touch_icon(skia::ImageOperations::Resize(bitmap, |
| 165 skia::ImageOperations::RESIZE_BEST, width, height)); |
| 166 } |
| 167 |
| 168 // Refresh UI since favicon changed. |
| 169 browser()->GetTabContentsAt(model_index)->NotifyNavigationStateChanged( |
| 170 TabContents::INVALIDATE_TAB); |
| 171 } |
OLD | NEW |