OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/toolbar/recent_tabs_sub_menu_model.h" | 5 #include "chrome/browser/ui/toolbar/recent_tabs_sub_menu_model.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
11 #include "chrome/browser/favicon/favicon_service_factory.h" | 11 #include "chrome/browser/favicon/favicon_service_factory.h" |
12 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 12 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/sessions/session_restore.h" | 14 #include "chrome/browser/sessions/session_restore.h" |
15 #include "chrome/browser/sessions/tab_restore_service_delegate.h" | 15 #include "chrome/browser/sessions/tab_restore_service_delegate.h" |
16 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 16 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
17 #include "chrome/browser/sync/glue/session_model_associator.h" | 17 #include "chrome/browser/sync/glue/session_model_associator.h" |
18 #include "chrome/browser/sync/glue/synced_session.h" | 18 #include "chrome/browser/sync/glue/synced_session.h" |
19 #include "chrome/browser/sync/profile_sync_service.h" | 19 #include "chrome/browser/sync/profile_sync_service.h" |
20 #include "chrome/browser/sync/profile_sync_service_factory.h" | 20 #include "chrome/browser/sync/profile_sync_service_factory.h" |
21 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
22 #include "chrome/browser/ui/browser_commands.h" | 22 #include "chrome/browser/ui/browser_commands.h" |
23 #include "chrome/browser/ui/browser_tabstrip.h" | 23 #include "chrome/browser/ui/browser_tabstrip.h" |
24 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
26 #include "chrome/common/time_format.h" | 26 #include "chrome/common/time_format.h" |
27 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.h" |
28 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
29 #include "grit/theme_resources.h" | |
29 #include "grit/ui_resources.h" | 30 #include "grit/ui_resources.h" |
30 #include "ui/base/accelerators/accelerator.h" | 31 #include "ui/base/accelerators/accelerator.h" |
31 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
32 #include "ui/base/resource/resource_bundle.h" | 33 #include "ui/base/resource/resource_bundle.h" |
33 #include "ui/base/text/text_elider.h" | |
34 #include "ui/gfx/favicon_size.h" | 34 #include "ui/gfx/favicon_size.h" |
35 | 35 |
36 #if defined(USE_ASH) | 36 #if defined(USE_ASH) |
37 #include "ash/accelerators/accelerator_table.h" | 37 #include "ash/accelerators/accelerator_table.h" |
38 #endif // defined(USE_ASH) | 38 #endif // defined(USE_ASH) |
39 | 39 |
40 namespace { | 40 namespace { |
41 | 41 |
42 // First comamnd id for navigatable (and hence executable) tab menu item. | 42 // First comamnd id for navigatable (and hence executable) tab menu item. |
43 // The model and menu are not 1-1: | 43 // The model and menu are not 1-1: |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
302 std::sort(tabs_in_session.begin(), tabs_in_session.end(), | 302 std::sort(tabs_in_session.begin(), tabs_in_session.end(), |
303 SortTabsByRecency); | 303 SortTabsByRecency); |
304 | 304 |
305 // Build tab menu items from sorted session tabs. | 305 // Build tab menu items from sorted session tabs. |
306 const size_t kMaxTabsPerSessionToShow = 4; | 306 const size_t kMaxTabsPerSessionToShow = 4; |
307 for (size_t k = 0; | 307 for (size_t k = 0; |
308 k < std::min(tabs_in_session.size(), kMaxTabsPerSessionToShow); | 308 k < std::min(tabs_in_session.size(), kMaxTabsPerSessionToShow); |
309 ++k) { | 309 ++k) { |
310 BuildForeignTabItem(session_tag, *tabs_in_session[k], | 310 BuildForeignTabItem(session_tag, *tabs_in_session[k], |
311 // Only need |session_name| for the first tab of the session. | 311 // Only need |session_name| for the first tab of the session. |
312 !k ? session->session_name : std::string(), need_separator); | 312 !k ? session->session_name : std::string(), session->device_type, |
313 need_separator); | |
313 need_separator = false; | 314 need_separator = false; |
314 } // for all tabs in one session | 315 } // for all tabs in one session |
315 | 316 |
316 ++num_sessions_added; | 317 ++num_sessions_added; |
317 need_separator = true; | 318 need_separator = true; |
318 } // for all sessions | 319 } // for all sessions |
319 } | 320 } |
320 | 321 |
321 void RecentTabsSubMenuModel::BuildForeignTabItem( | 322 void RecentTabsSubMenuModel::BuildForeignTabItem( |
322 const std::string& session_tag, | 323 const std::string& session_tag, |
323 const SessionTab& tab, | 324 const SessionTab& tab, |
324 const std::string& session_name, | 325 const std::string& session_name, |
326 browser_sync::SyncedSession::DeviceType device_type, | |
325 bool need_separator) { | 327 bool need_separator) { |
326 if (need_separator) | 328 if (need_separator) |
327 AddSeparator(ui::NORMAL_SEPARATOR); | 329 AddSeparator(ui::NORMAL_SEPARATOR); |
328 | 330 |
329 if (!session_name.empty()) | 331 if (!session_name.empty()) { |
330 AddItem(kDisabledCommandId, UTF8ToUTF16(session_name)); | 332 AddItem(kDisabledCommandId, UTF8ToUTF16(session_name)); |
333 AddDeviceFavicon(GetItemCount() - 1, device_type); | |
334 } | |
331 | 335 |
332 const TabNavigation& current_navigation = | 336 const TabNavigation& current_navigation = |
333 tab.navigations.at(tab.normalized_navigation_index()); | 337 tab.navigations.at(tab.normalized_navigation_index()); |
334 NavigationItem item(session_tag, tab.tab_id.id(), | 338 NavigationItem item(session_tag, tab.tab_id.id(), |
335 current_navigation.virtual_url()); | 339 current_navigation.virtual_url()); |
336 int command_id = ModelIndexToCommandId(model_.size()); | 340 int command_id = ModelIndexToCommandId(model_.size()); |
337 AddItem(command_id, current_navigation.title()); | 341 AddItem(command_id, current_navigation.title()); |
338 AddFavicon(model_.size(), command_id, item.url); | 342 AddTabFavicon(model_.size(), command_id, item.url); |
339 model_.push_back(item); | 343 model_.push_back(item); |
340 } | 344 } |
341 | 345 |
342 void RecentTabsSubMenuModel::AddFavicon(int model_index, int command_id, | 346 void RecentTabsSubMenuModel::AddDeviceFavicon( |
343 const GURL& url) { | 347 int index_in_menu, |
348 browser_sync::SyncedSession::DeviceType device_type) { | |
349 int favicon_id = -1; | |
350 switch (device_type) { | |
351 case browser_sync::SyncedSession::TYPE_PHONE: | |
352 favicon_id = IDR_PHONE_FAVICON; | |
353 break; | |
354 | |
355 case browser_sync::SyncedSession::TYPE_TABLET: | |
356 favicon_id = IDR_TABLET_FAVICON; | |
357 break; | |
358 | |
359 case browser_sync::SyncedSession::TYPE_CHROMEOS: | |
360 case browser_sync::SyncedSession::TYPE_WIN: | |
361 case browser_sync::SyncedSession::TYPE_MACOSX: | |
362 case browser_sync::SyncedSession::TYPE_LINUX: | |
363 case browser_sync::SyncedSession::TYPE_OTHER: | |
364 case browser_sync::SyncedSession::TYPE_UNSET: | |
365 favicon_id = IDR_LAPTOP_FAVICON; | |
366 break; | |
367 }; | |
368 | |
369 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
370 SetIcon(index_in_menu, rb.GetNativeImageNamed(favicon_id)); | |
371 } | |
372 | |
373 void RecentTabsSubMenuModel::AddTabFavicon(int model_index, | |
374 int command_id, | |
375 const GURL& url) { | |
376 int index_in_menu = GetIndexOfCommandId(command_id); | |
377 | |
378 // If tab has synced favicon, use it. | |
379 browser_sync::SessionModelAssociator* associator = GetModelAssociator(); | |
380 std::string favicon_png; | |
381 if (associator && | |
382 associator->GetSyncedFaviconForPageURL(url.spec(), &favicon_png)) { | |
383 LOG(ERROR) << "kk got synced favicon for " << url.spec(); | |
dhollowa
2012/11/13 20:14:20
Remove LOG.
kuan
2012/11/13 20:49:10
Done.
| |
384 SetIcon(index_in_menu, gfx::Image(reinterpret_cast<const unsigned char*>( | |
385 favicon_png.data()), favicon_png.size())); | |
386 return; | |
387 } | |
388 | |
389 // Otherwise, start to fetch the favicon from local history asynchronously. | |
344 // Set default icon first. | 390 // Set default icon first. |
345 SetIcon(GetIndexOfCommandId(command_id), default_favicon_); | 391 SetIcon(index_in_menu, default_favicon_); |
346 // Start request to fetch actual icon if possible. | 392 // Start request to fetch actual icon if possible. |
347 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( | 393 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( |
348 browser_->profile(), Profile::EXPLICIT_ACCESS); | 394 browser_->profile(), Profile::EXPLICIT_ACCESS); |
349 if (!favicon_service) | 395 if (!favicon_service) |
350 return; | 396 return; |
351 FaviconService::Handle handle = favicon_service->GetFaviconImageForURL( | 397 FaviconService::Handle handle = favicon_service->GetFaviconImageForURL( |
352 FaviconService::FaviconForURLParams(browser_->profile(), url, | 398 FaviconService::FaviconForURLParams(browser_->profile(), url, |
353 history::FAVICON, gfx::kFaviconSize, &favicon_consumer_), | 399 history::FAVICON, gfx::kFaviconSize, &favicon_consumer_), |
354 base::Bind(&RecentTabsSubMenuModel::OnFaviconDataAvailable, | 400 base::Bind(&RecentTabsSubMenuModel::OnFaviconDataAvailable, |
355 weak_ptr_factory_.GetWeakPtr())); | 401 weak_ptr_factory_.GetWeakPtr())); |
(...skipping 21 matching lines...) Expand all Loading... | |
377 RecentTabsSubMenuModel::GetModelAssociator() { | 423 RecentTabsSubMenuModel::GetModelAssociator() { |
378 if (!associator_) { | 424 if (!associator_) { |
379 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 425 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
380 GetForProfile(browser_->profile()); | 426 GetForProfile(browser_->profile()); |
381 // Only return the associator if it exists and it is done syncing sessions. | 427 // Only return the associator if it exists and it is done syncing sessions. |
382 if (service && service->ShouldPushChanges()) | 428 if (service && service->ShouldPushChanges()) |
383 associator_ = service->GetSessionModelAssociator(); | 429 associator_ = service->GetSessionModelAssociator(); |
384 } | 430 } |
385 return associator_; | 431 return associator_; |
386 } | 432 } |
OLD | NEW |