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 gfx::Image* favicon = NULL; | |
351 switch (device_type) { | |
352 case browser_sync::SyncedSession::TYPE_PHONE: | |
353 if ((favicon = &phone_favicon_)->IsEmpty()) | |
354 favicon_id = IDR_PHONE_FAVICON; | |
355 break; | |
356 | |
357 case browser_sync::SyncedSession::TYPE_TABLET: | |
358 if ((favicon = &tablet_favicon_)->IsEmpty()) | |
359 favicon_id = IDR_TABLET_FAVICON; | |
360 break; | |
361 | |
362 case browser_sync::SyncedSession::TYPE_CHROMEOS: | |
363 case browser_sync::SyncedSession::TYPE_WIN: | |
364 case browser_sync::SyncedSession::TYPE_MACOSX: | |
365 case browser_sync::SyncedSession::TYPE_LINUX: | |
366 case browser_sync::SyncedSession::TYPE_OTHER: | |
367 case browser_sync::SyncedSession::TYPE_UNSET: | |
368 if ((favicon = &laptop_favicon_)->IsEmpty()) | |
369 favicon_id = IDR_LAPTOP_FAVICON; | |
370 break; | |
371 }; | |
372 DCHECK(favicon && (!favicon->IsEmpty() || favicon_id > -1)); | |
373 | |
374 if (favicon->IsEmpty()) { | |
375 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
376 *favicon = rb.GetNativeImageNamed(favicon_id); | |
377 } | |
378 SetIcon(index_in_menu, *favicon); | |
379 } | |
380 | |
381 void RecentTabsSubMenuModel::AddTabFavicon(int model_index, int command_id, | |
sky
2012/11/13 17:41:49
nit: each param on its own line.
kuan
2012/11/13 18:52:52
Done.
| |
382 const GURL& url) { | |
383 int index_in_menu = GetIndexOfCommandId(command_id); | |
384 | |
385 // If tab has synced favicon, use it. | |
386 browser_sync::SessionModelAssociator* associator = GetModelAssociator(); | |
387 std::string favicon_png; | |
388 if (associator && | |
389 associator->GetSyncedFaviconForPageURL(url.spec(), &favicon_png)) { | |
390 SetIcon(index_in_menu, gfx::Image(reinterpret_cast<const unsigned char*>( | |
sky
2012/11/13 17:41:49
You sure you need reinterpret cast here?
kuan
2012/11/13 18:52:52
unfortunately yes, it won't build 'cos std::string
| |
391 favicon_png.data()), favicon_png.size())); | |
392 return; | |
393 } | |
394 | |
395 // Otherwise, start to fetch the favicon from local history asynchronously. | |
344 // Set default icon first. | 396 // Set default icon first. |
345 SetIcon(GetIndexOfCommandId(command_id), default_favicon_); | 397 SetIcon(index_in_menu, default_favicon_); |
346 // Start request to fetch actual icon if possible. | 398 // Start request to fetch actual icon if possible. |
347 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( | 399 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( |
348 browser_->profile(), Profile::EXPLICIT_ACCESS); | 400 browser_->profile(), Profile::EXPLICIT_ACCESS); |
349 if (!favicon_service) | 401 if (!favicon_service) |
350 return; | 402 return; |
351 FaviconService::Handle handle = favicon_service->GetFaviconImageForURL( | 403 FaviconService::Handle handle = favicon_service->GetFaviconImageForURL( |
352 FaviconService::FaviconForURLParams(browser_->profile(), url, | 404 FaviconService::FaviconForURLParams(browser_->profile(), url, |
353 history::FAVICON, gfx::kFaviconSize, &favicon_consumer_), | 405 history::FAVICON, gfx::kFaviconSize, &favicon_consumer_), |
354 base::Bind(&RecentTabsSubMenuModel::OnFaviconDataAvailable, | 406 base::Bind(&RecentTabsSubMenuModel::OnFaviconDataAvailable, |
355 weak_ptr_factory_.GetWeakPtr())); | 407 weak_ptr_factory_.GetWeakPtr())); |
(...skipping 21 matching lines...) Expand all Loading... | |
377 RecentTabsSubMenuModel::GetModelAssociator() { | 429 RecentTabsSubMenuModel::GetModelAssociator() { |
378 if (!associator_) { | 430 if (!associator_) { |
379 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 431 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
380 GetForProfile(browser_->profile()); | 432 GetForProfile(browser_->profile()); |
381 // Only return the associator if it exists and it is done syncing sessions. | 433 // Only return the associator if it exists and it is done syncing sessions. |
382 if (service && service->ShouldPushChanges()) | 434 if (service && service->ShouldPushChanges()) |
383 associator_ = service->GetSessionModelAssociator(); | 435 associator_ = service->GetSessionModelAssociator(); |
384 } | 436 } |
385 return associator_; | 437 return associator_; |
386 } | 438 } |
OLD | NEW |