| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/cocoa/history_menu_bridge.h" | 5 #include "chrome/browser/ui/cocoa/history_menu_bridge.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "content/public/browser/notification_registrar.h" | 24 #include "content/public/browser/notification_registrar.h" |
| 25 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
| 26 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
| 27 #include "grit/theme_resources.h" | 27 #include "grit/theme_resources.h" |
| 28 #include "grit/ui_resources.h" | 28 #include "grit/ui_resources.h" |
| 29 #include "skia/ext/skia_utils_mac.h" | 29 #include "skia/ext/skia_utils_mac.h" |
| 30 #include "third_party/skia/include/core/SkBitmap.h" | 30 #include "third_party/skia/include/core/SkBitmap.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 32 #include "ui/base/resource/resource_bundle.h" | 32 #include "ui/base/resource/resource_bundle.h" |
| 33 #include "ui/gfx/codec/png_codec.h" | 33 #include "ui/gfx/codec/png_codec.h" |
| 34 #include "ui/gfx/favicon_size.h" |
| 34 #include "ui/gfx/image/image.h" | 35 #include "ui/gfx/image/image.h" |
| 35 #include "ui/gfx/mac/nsimage_cache.h" | 36 #include "ui/gfx/mac/nsimage_cache.h" |
| 36 | 37 |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 // Menus more than this many chars long will get trimmed. | 40 // Menus more than this many chars long will get trimmed. |
| 40 const NSUInteger kMaximumMenuWidthInChars = 50; | 41 const NSUInteger kMaximumMenuWidthInChars = 50; |
| 41 | 42 |
| 42 // When trimming, use this many chars from each side. | 43 // When trimming, use this many chars from each side. |
| 43 const NSUInteger kMenuTrimSizeInChars = 25; | 44 const NSUInteger kMenuTrimSizeInChars = 25; |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 | 448 |
| 448 // Tab navigations don't come with icons, so we always have to request them. | 449 // Tab navigations don't come with icons, so we always have to request them. |
| 449 GetFaviconForHistoryItem(item); | 450 GetFaviconForHistoryItem(item); |
| 450 | 451 |
| 451 return item; | 452 return item; |
| 452 } | 453 } |
| 453 | 454 |
| 454 void HistoryMenuBridge::GetFaviconForHistoryItem(HistoryItem* item) { | 455 void HistoryMenuBridge::GetFaviconForHistoryItem(HistoryItem* item) { |
| 455 FaviconService* service = | 456 FaviconService* service = |
| 456 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 457 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 457 FaviconService::Handle handle = service->GetFaviconForURL( | 458 FaviconService::Handle handle = service->GetFaviconImageForURL( |
| 458 profile_, item->url, history::FAVICON, &favicon_consumer_, | 459 profile_, item->url, history::FAVICON, gfx::kFaviconSize, |
| 460 &favicon_consumer_, |
| 459 base::Bind(&HistoryMenuBridge::GotFaviconData, base::Unretained(this))); | 461 base::Bind(&HistoryMenuBridge::GotFaviconData, base::Unretained(this))); |
| 460 favicon_consumer_.SetClientData(service, handle, item); | 462 favicon_consumer_.SetClientData(service, handle, item); |
| 461 item->icon_handle = handle; | 463 item->icon_handle = handle; |
| 462 item->icon_requested = true; | 464 item->icon_requested = true; |
| 463 } | 465 } |
| 464 | 466 |
| 465 void HistoryMenuBridge::GotFaviconData(FaviconService::Handle handle, | 467 void HistoryMenuBridge::GotFaviconData( |
| 466 history::FaviconData favicon) { | 468 FaviconService::Handle handle, |
| 469 const history::FaviconImageResult& image_result) { |
| 467 // Since we're going to do Cocoa-y things, make sure this is the main thread. | 470 // Since we're going to do Cocoa-y things, make sure this is the main thread. |
| 468 DCHECK([NSThread isMainThread]); | 471 DCHECK([NSThread isMainThread]); |
| 469 | 472 |
| 470 HistoryItem* item = | 473 HistoryItem* item = |
| 471 favicon_consumer_.GetClientData( | 474 favicon_consumer_.GetClientData( |
| 472 FaviconServiceFactory::GetForProfile( | 475 FaviconServiceFactory::GetForProfile( |
| 473 profile_, Profile::EXPLICIT_ACCESS), handle); | 476 profile_, Profile::EXPLICIT_ACCESS), handle); |
| 474 DCHECK(item); | 477 DCHECK(item); |
| 475 item->icon_requested = false; | 478 item->icon_requested = false; |
| 476 item->icon_handle = 0; | 479 item->icon_handle = 0; |
| 477 | 480 |
| 478 // Convert the raw data to Skia and then to a NSImage. | 481 NSImage* image = image_result.image.AsNSImage(); |
| 479 // TODO(rsesek): Is there an easier way to do this? | 482 if (image) { |
| 480 SkBitmap icon; | 483 item->icon.reset([image retain]); |
| 481 if (favicon.is_valid() && | 484 [item->menu_item setImage:item->icon.get()]; |
| 482 gfx::PNGCodec::Decode(favicon.image_data->front(), | |
| 483 favicon.image_data->size(), &icon)) { | |
| 484 NSImage* image = gfx::SkBitmapToNSImage(icon); | |
| 485 if (image) { | |
| 486 // The conversion was successful. | |
| 487 item->icon.reset([image retain]); | |
| 488 [item->menu_item setImage:item->icon.get()]; | |
| 489 } | |
| 490 } | 485 } |
| 491 } | 486 } |
| 492 | 487 |
| 493 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { | 488 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { |
| 494 DCHECK(item); | 489 DCHECK(item); |
| 495 if (item->icon_requested) { | 490 if (item->icon_requested) { |
| 496 FaviconService* service = FaviconServiceFactory::GetForProfile( | 491 FaviconService* service = FaviconServiceFactory::GetForProfile( |
| 497 profile_, Profile::EXPLICIT_ACCESS); | 492 profile_, Profile::EXPLICIT_ACCESS); |
| 498 service->CancelRequest(item->icon_handle); | 493 service->CancelRequest(item->icon_handle); |
| 499 item->icon_requested = false; | 494 item->icon_requested = false; |
| 500 item->icon_handle = 0; | 495 item->icon_handle = 0; |
| 501 } | 496 } |
| 502 } | 497 } |
| OLD | NEW |