Chromium Code Reviews| 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/background/background_mode_manager.h" | 5 #include "chrome/browser/background/background_mode_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 #include "chrome/browser/ui/extensions/app_launch_params.h" | 42 #include "chrome/browser/ui/extensions/app_launch_params.h" |
| 43 #include "chrome/browser/ui/extensions/application_launch.h" | 43 #include "chrome/browser/ui/extensions/application_launch.h" |
| 44 #include "chrome/browser/ui/host_desktop.h" | 44 #include "chrome/browser/ui/host_desktop.h" |
| 45 #include "chrome/browser/ui/user_manager.h" | 45 #include "chrome/browser/ui/user_manager.h" |
| 46 #include "chrome/common/chrome_constants.h" | 46 #include "chrome/common/chrome_constants.h" |
| 47 #include "chrome/common/chrome_switches.h" | 47 #include "chrome/common/chrome_switches.h" |
| 48 #include "chrome/common/extensions/extension_constants.h" | 48 #include "chrome/common/extensions/extension_constants.h" |
| 49 #include "chrome/common/pref_names.h" | 49 #include "chrome/common/pref_names.h" |
| 50 #include "chrome/grit/chromium_strings.h" | 50 #include "chrome/grit/chromium_strings.h" |
| 51 #include "chrome/grit/generated_resources.h" | 51 #include "chrome/grit/generated_resources.h" |
| 52 #include "content/public/browser/browser_thread.h" | |
| 52 #include "content/public/browser/notification_service.h" | 53 #include "content/public/browser/notification_service.h" |
| 53 #include "content/public/browser/user_metrics.h" | 54 #include "content/public/browser/user_metrics.h" |
| 54 #include "extensions/browser/extension_system.h" | 55 #include "extensions/browser/extension_system.h" |
| 55 #include "extensions/common/constants.h" | 56 #include "extensions/common/constants.h" |
| 56 #include "extensions/common/extension.h" | 57 #include "extensions/common/extension.h" |
| 57 #include "extensions/common/manifest_handlers/options_page_info.h" | 58 #include "extensions/common/manifest_handlers/options_page_info.h" |
| 58 #include "extensions/common/one_shot_event.h" | 59 #include "extensions/common/one_shot_event.h" |
| 59 #include "extensions/common/permissions/permission_set.h" | 60 #include "extensions/common/permissions/permission_set.h" |
| 60 #include "grit/chrome_unscaled_resources.h" | 61 #include "grit/chrome_unscaled_resources.h" |
| 61 #include "ui/base/l10n/l10n_util.h" | 62 #include "ui/base/l10n/l10n_util.h" |
| 62 #include "ui/base/resource/resource_bundle.h" | 63 #include "ui/base/resource/resource_bundle.h" |
| 63 | 64 |
| 65 #if defined(OS_WIN) | |
| 66 #include "chrome/browser/app_icon_win.h" | |
| 67 #endif | |
| 68 | |
| 64 using base::UserMetricsAction; | 69 using base::UserMetricsAction; |
| 65 using extensions::Extension; | 70 using extensions::Extension; |
| 66 | 71 |
| 67 namespace { | 72 namespace { |
| 68 | 73 |
| 69 // Enum for recording menu item clicks in UMA. | 74 // Enum for recording menu item clicks in UMA. |
| 70 // NOTE: Do not renumber these as that would confuse interpretation of | 75 // NOTE: Do not renumber these as that would confuse interpretation of |
| 71 // previously logged data. When making changes, also update histograms.xml. | 76 // previously logged data. When making changes, also update histograms.xml. |
| 72 enum MenuItem { | 77 enum MenuItem { |
| 73 MENU_ITEM_ABOUT = 0, | 78 MENU_ITEM_ABOUT = 0, |
| (...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 747 background_mode_suspended_ = false; | 752 background_mode_suspended_ = false; |
| 748 UpdateKeepAliveAndTrayIcon(); | 753 UpdateKeepAliveAndTrayIcon(); |
| 749 } | 754 } |
| 750 | 755 |
| 751 void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() { | 756 void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() { |
| 752 if (in_background_mode_ && !background_mode_suspended_) { | 757 if (in_background_mode_ && !background_mode_suspended_) { |
| 753 if (!keeping_alive_) { | 758 if (!keeping_alive_) { |
| 754 keeping_alive_ = true; | 759 keeping_alive_ = true; |
| 755 chrome::IncrementKeepAliveCount(); | 760 chrome::IncrementKeepAliveCount(); |
| 756 } | 761 } |
| 762 | |
| 757 CreateStatusTrayIcon(); | 763 CreateStatusTrayIcon(); |
| 758 return; | 764 return; |
| 759 } | 765 } |
| 760 | 766 |
| 761 RemoveStatusTrayIcon(); | 767 RemoveStatusTrayIcon(); |
| 762 if (keeping_alive_) { | 768 if (keeping_alive_) { |
| 763 keeping_alive_ = false; | 769 keeping_alive_ = false; |
| 764 chrome::DecrementKeepAliveCount(); | 770 chrome::DecrementKeepAliveCount(); |
| 765 } | 771 } |
| 766 } | 772 } |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 849 | 855 |
| 850 // Ensure we have a tray icon (needed so we can display the app-installed | 856 // Ensure we have a tray icon (needed so we can display the app-installed |
| 851 // notification below). | 857 // notification below). |
| 852 EnableBackgroundMode(); | 858 EnableBackgroundMode(); |
| 853 ResumeBackgroundMode(); | 859 ResumeBackgroundMode(); |
| 854 | 860 |
| 855 // Notify the user that a background client has been installed. | 861 // Notify the user that a background client has been installed. |
| 856 DisplayClientInstalledNotification(name); | 862 DisplayClientInstalledNotification(name); |
| 857 } | 863 } |
| 858 | 864 |
| 865 // Gets the image for the status tray icon, at the correct size for the current | |
| 866 // platform and display settings. | |
| 867 gfx::ImageSkia GetStatusTrayIcon() { | |
| 868 #if defined(OS_WIN) | |
| 869 // On Windows, use GetSmallAppIconSize to get the correct image size. The | |
| 870 // user's "text size" setting in Windows determines how large the system tray | |
| 871 // icon should be. | |
| 872 gfx::Size size = GetSmallAppIconSize(); | |
| 873 | |
| 874 // This loads all of the icon images, which is a bit wasteful because we're | |
| 875 // going to pick one and throw the rest away, but that is the price of using | |
| 876 // the ImageFamily abstraction. Note: We could just use the LoadImage function | |
| 877 // from the Windows API, but that does a *terrible* job scaling images. | |
| 878 // Therefore, we fetch the images and do our own high-quality scaling. | |
| 879 scoped_ptr<gfx::ImageFamily> family = GetAppIconImageFamily(); | |
| 880 DCHECK(family); | |
| 881 if (!family) | |
| 882 return gfx::ImageSkia(); | |
| 883 | |
| 884 return family->CreateExact(size).AsImageSkia(); | |
| 885 #else | |
| 886 // On other platforms, just get a static resource image. | |
| 887 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
| 888 IDR_STATUS_TRAY_ICON); | |
| 889 #endif | |
| 890 } | |
| 891 | |
| 859 void BackgroundModeManager::CreateStatusTrayIcon() { | 892 void BackgroundModeManager::CreateStatusTrayIcon() { |
| 893 // Creating the status tray icon can take time (as it has to load and | |
| 894 // potentially scale the icon resource), so this task should not block | |
| 895 // startup time. | |
| 896 content::BrowserThread::PostAfterStartupTask( | |
|
Andrew T Wilson (Slow)
2015/11/23 11:28:11
This seems incorrect, because it's possible that s
Matt Giuca
2015/11/24 07:07:46
That's a good point. But we can work around it by
| |
| 897 FROM_HERE, content::BrowserThread::GetMessageLoopProxyForThread( | |
| 898 content::BrowserThread::UI), | |
| 899 base::Bind(&BackgroundModeManager::CreateStatusTrayIconTask, | |
| 900 weak_factory_.GetWeakPtr())); | |
| 901 } | |
| 902 | |
| 903 void BackgroundModeManager::CreateStatusTrayIconTask() { | |
| 860 // Only need status icons on windows/linux. ChromeOS doesn't allow exiting | 904 // Only need status icons on windows/linux. ChromeOS doesn't allow exiting |
| 861 // Chrome and Mac can use the dock icon instead. | 905 // Chrome and Mac can use the dock icon instead. |
| 862 | 906 |
| 863 // Since there are multiple profiles which share the status tray, we now | 907 // Since there are multiple profiles which share the status tray, we now |
| 864 // use the browser process to keep track of it. | 908 // use the browser process to keep track of it. |
| 865 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) | 909 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) |
| 866 if (!status_tray_) | 910 if (!status_tray_) |
| 867 status_tray_ = g_browser_process->status_tray(); | 911 status_tray_ = g_browser_process->status_tray(); |
| 868 #endif | 912 #endif |
| 869 | 913 |
| 870 // If the platform doesn't support status icons, or we've already created | 914 // If the platform doesn't support status icons, or we've already created |
| 871 // our status icon, just return. | 915 // our status icon, just return. |
| 872 if (!status_tray_ || status_icon_) | 916 if (!status_tray_ || status_icon_) |
| 873 return; | 917 return; |
| 874 | 918 |
| 875 gfx::ImageSkia* image_skia = ui::ResourceBundle::GetSharedInstance(). | |
| 876 GetImageSkiaNamed(IDR_STATUS_TRAY_ICON); | |
| 877 | |
| 878 status_icon_ = status_tray_->CreateStatusIcon( | 919 status_icon_ = status_tray_->CreateStatusIcon( |
| 879 StatusTray::BACKGROUND_MODE_ICON, | 920 StatusTray::BACKGROUND_MODE_ICON, GetStatusTrayIcon(), |
| 880 *image_skia, | |
| 881 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); | 921 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); |
| 882 if (!status_icon_) | 922 if (!status_icon_) |
| 883 return; | 923 return; |
| 884 UpdateStatusTrayIconContextMenu(); | 924 UpdateStatusTrayIconContextMenu(); |
| 885 } | 925 } |
| 886 | 926 |
| 887 void BackgroundModeManager::UpdateStatusTrayIconContextMenu() { | 927 void BackgroundModeManager::UpdateStatusTrayIconContextMenu() { |
| 888 // Ensure we have a tray icon if appropriate. | 928 // Ensure we have a tray icon if appropriate. |
| 889 UpdateKeepAliveAndTrayIcon(); | 929 UpdateKeepAliveAndTrayIcon(); |
| 890 | 930 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 992 } | 1032 } |
| 993 } | 1033 } |
| 994 return profile_it; | 1034 return profile_it; |
| 995 } | 1035 } |
| 996 | 1036 |
| 997 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { | 1037 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { |
| 998 PrefService* service = g_browser_process->local_state(); | 1038 PrefService* service = g_browser_process->local_state(); |
| 999 DCHECK(service); | 1039 DCHECK(service); |
| 1000 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 1040 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
| 1001 } | 1041 } |
| OLD | NEW |