| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/mac/mac_util.h" | 7 #include "base/mac/mac_util.h" |
| 8 #include "chrome/browser/background/background_mode_manager.h" | 8 #include "chrome/browser/background/background_mode_manager.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/common/chrome_switches.h" | 11 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 16 | 16 |
| 17 using content::BrowserThread; | 17 using content::BrowserThread; |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 #if !defined(NDEBUG) |
| 21 // The code to remove a login item has a potential race (because the code to |
| 22 // set and check the kUserRemovedLoginItem pref runs on the UI thread, while |
| 23 // the code that checks for a login item runs on the IO thread). We add this |
| 24 // flag which should always match the value of the pref to see if we ever hit |
| 25 // this race in practice. |
| 26 static bool login_item_removed = false; |
| 27 #endif |
| 28 |
| 29 void SetUserRemovedLoginItemPrefCallback() { |
| 30 PrefService* service = g_browser_process->local_state(); |
| 31 service->SetBoolean(prefs::kUserRemovedLoginItem, true); |
| 32 } |
| 20 | 33 |
| 21 void DisableLaunchOnStartupCallback() { | 34 void DisableLaunchOnStartupCallback() { |
| 22 // Check if Chrome is not a login Item, or is a Login Item but w/o 'hidden' | 35 // Check if Chrome is not a login Item, or is a Login Item but w/o 'hidden' |
| 23 // flag - most likely user has modified the setting, don't override it. | 36 // flag - most likely user has modified the setting, don't override it. |
| 24 bool is_hidden = false; | 37 bool is_hidden = false; |
| 25 if (!base::mac::CheckLoginItemStatus(&is_hidden) || !is_hidden) | 38 if (!base::mac::CheckLoginItemStatus(&is_hidden)) { |
| 39 // No login item - this means the user must have already removed it, so |
| 40 // call back to the UI thread to set a preference so we don't try to |
| 41 // recreate it the next time they enable/install a background app. |
| 42 #if !defined(NDEBUG) |
| 43 login_item_removed = true; |
| 44 #endif |
| 45 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 46 base::Bind(SetUserRemovedLoginItemPrefCallback)); |
| 47 return; |
| 48 } |
| 49 |
| 50 // If the login item does not have the "hidden" flag set, just leave it there |
| 51 // since it means the user must have created it. |
| 52 if (!is_hidden) |
| 26 return; | 53 return; |
| 27 | 54 |
| 55 // Remove the login item we created. |
| 28 base::mac::RemoveFromLoginItems(); | 56 base::mac::RemoveFromLoginItems(); |
| 29 } | 57 } |
| 30 | 58 |
| 31 void SetUserCreatedLoginItemPrefCallback() { | 59 void SetUserCreatedLoginItemPrefCallback() { |
| 32 PrefService* service = g_browser_process->local_state(); | 60 PrefService* service = g_browser_process->local_state(); |
| 33 service->SetBoolean(prefs::kUserCreatedLoginItem, true); | 61 service->SetBoolean(prefs::kUserCreatedLoginItem, true); |
| 34 } | 62 } |
| 35 | 63 |
| 36 void EnableLaunchOnStartupCallback() { | 64 void EnableLaunchOnStartupCallback(bool should_add_login_item) { |
| 37 // Return if Chrome is already a Login Item (avoid overriding user choice). | 65 // Check if Chrome is already a Login Item (avoid overriding user choice). |
| 38 if (base::mac::CheckLoginItemStatus(NULL)) { | 66 if (base::mac::CheckLoginItemStatus(NULL)) { |
| 39 // Call back to the UI thread to set our preference so we don't delete the | 67 // Call back to the UI thread to set our preference so we don't delete the |
| 40 // user's login item when we disable launch on startup. There's a race | 68 // user's login item when we disable launch on startup. There's a race |
| 41 // condition here if the user disables launch on startup before our callback | 69 // condition here if the user disables launch on startup before our callback |
| 42 // is run, but the user can manually disable "Open At Login" via the dock if | 70 // is run, but the user can manually disable "Open At Login" via the dock if |
| 43 // this happens. | 71 // this happens. |
| 44 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 72 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 45 base::Bind(SetUserCreatedLoginItemPrefCallback)); | 73 base::Bind(SetUserCreatedLoginItemPrefCallback)); |
| 46 return; | 74 return; |
| 47 } | 75 } |
| 48 | 76 |
| 49 base::mac::AddToLoginItems(true); // Hide on startup. | 77 if (should_add_login_item) |
| 78 base::mac::AddToLoginItems(true); // Hide on startup. |
| 79 #if !defined(NDEBUG) |
| 80 else |
| 81 DCHECK(!login_item_removed); // Check for race condition (see above). |
| 82 #endif |
| 50 } | 83 } |
| 51 | 84 |
| 52 } // namespace | 85 } // namespace |
| 53 | 86 |
| 54 void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) { | 87 void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) { |
| 55 // This functionality is only defined for default profile, currently. | 88 // This functionality is only defined for default profile, currently. |
| 56 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir)) | 89 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir)) |
| 57 return; | 90 return; |
| 58 | 91 |
| 59 if (should_launch) { | 92 if (should_launch) { |
| 93 PrefService* service = g_browser_process->local_state(); |
| 94 // Create a login item if the user did not remove our login item |
| 95 // previously. We call out to the FILE thread either way since we |
| 96 // want to check for a user-created login item. |
| 97 bool should_add_login_item = |
| 98 !service->GetBoolean(prefs::kUserRemovedLoginItem); |
| 60 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 99 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 61 base::Bind(EnableLaunchOnStartupCallback)); | 100 base::Bind(EnableLaunchOnStartupCallback, |
| 101 should_add_login_item)); |
| 62 } else { | 102 } else { |
| 63 PrefService* service = g_browser_process->local_state(); | 103 PrefService* service = g_browser_process->local_state(); |
| 64 if (service->GetBoolean(prefs::kUserCreatedLoginItem)) { | 104 if (service->GetBoolean(prefs::kUserCreatedLoginItem)) { |
| 65 // We didn't create the login item, so nothing to do here. | 105 // We didn't create the login item, so nothing to do here. Clear our |
| 106 // prefs so if the user removes the login item before installing a |
| 107 // background app, we will revert to the default behavior. |
| 66 service->ClearPref(prefs::kUserCreatedLoginItem); | 108 service->ClearPref(prefs::kUserCreatedLoginItem); |
| 109 service->ClearPref(prefs::kUserRemovedLoginItem); |
| 110 #if !defined(NDEBUG) |
| 111 login_item_removed = false; |
| 112 #endif |
| 67 return; | 113 return; |
| 68 } | 114 } |
| 69 // Call to the File thread to remove the login item since it requires | 115 // Call to the File thread to remove the login item since it requires |
| 70 // accessing the disk. | 116 // accessing the disk. |
| 71 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 117 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 72 base::Bind(DisableLaunchOnStartupCallback)); | 118 base::Bind(DisableLaunchOnStartupCallback)); |
| 73 } | 119 } |
| 74 } | 120 } |
| 75 | 121 |
| 76 void BackgroundModeManager::DisplayAppInstalledNotification( | 122 void BackgroundModeManager::DisplayAppInstalledNotification( |
| 77 const extensions::Extension* extension) { | 123 const extensions::Extension* extension) { |
| 78 // TODO(atwilson): Display a platform-appropriate notification here. | 124 // TODO(atwilson): Display a platform-appropriate notification here. |
| 79 // http://crbug.com/74970 | 125 // http://crbug.com/74970 |
| 80 } | 126 } |
| 81 | 127 |
| 82 string16 BackgroundModeManager::GetPreferencesMenuLabel() { | 128 string16 BackgroundModeManager::GetPreferencesMenuLabel() { |
| 83 return l10n_util::GetStringUTF16(IDS_OPTIONS); | 129 return l10n_util::GetStringUTF16(IDS_OPTIONS); |
| 84 } | 130 } |
| OLD | NEW |