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 | 20 |
21 void SetUserRemovedLoginItemPrefCallback() { | |
22 PrefService* service = g_browser_process->local_state(); | |
23 service->SetBoolean(prefs::kUserRemovedLoginItem, true); | |
24 } | |
25 | |
21 void DisableLaunchOnStartupCallback() { | 26 void DisableLaunchOnStartupCallback() { |
22 // Check if Chrome is not a login Item, or is a Login Item but w/o 'hidden' | 27 // 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. | 28 // flag - most likely user has modified the setting, don't override it. |
24 bool is_hidden = false; | 29 bool is_hidden = false; |
25 if (!base::mac::CheckLoginItemStatus(&is_hidden) || !is_hidden) | 30 if (!base::mac::CheckLoginItemStatus(&is_hidden)) { |
31 // No login item - this means the user must have already removed it, so | |
32 // call back to the UI thread to set a preference so we don't try to | |
33 // recreate it the next time they enable/install a background app. | |
34 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
Dmitry Titov
2012/08/09 18:22:23
So the detection of the user action (when user cle
| |
35 base::Bind(SetUserRemovedLoginItemPrefCallback)); | |
36 return; | |
37 } | |
38 | |
39 // If the login item does not have the "hidden" flag set, just leave it there | |
40 // since it means the user must have created it. | |
41 if (!is_hidden) | |
26 return; | 42 return; |
27 | 43 |
44 // Remove the login item we created. | |
28 base::mac::RemoveFromLoginItems(); | 45 base::mac::RemoveFromLoginItems(); |
29 } | 46 } |
30 | 47 |
31 void SetUserCreatedLoginItemPrefCallback() { | 48 void SetUserCreatedLoginItemPrefCallback() { |
32 PrefService* service = g_browser_process->local_state(); | 49 PrefService* service = g_browser_process->local_state(); |
33 service->SetBoolean(prefs::kUserCreatedLoginItem, true); | 50 service->SetBoolean(prefs::kUserCreatedLoginItem, true); |
34 } | 51 } |
35 | 52 |
36 void EnableLaunchOnStartupCallback() { | 53 void EnableLaunchOnStartupCallback(bool should_add_login_item) { |
37 // Return if Chrome is already a Login Item (avoid overriding user choice). | 54 // Check if Chrome is already a Login Item (avoid overriding user choice). |
38 if (base::mac::CheckLoginItemStatus(NULL)) { | 55 if (base::mac::CheckLoginItemStatus(NULL)) { |
39 // Call back to the UI thread to set our preference so we don't delete the | 56 // 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 | 57 // 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 | 58 // 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 | 59 // is run, but the user can manually disable "Open At Login" via the dock if |
43 // this happens. | 60 // this happens. |
44 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 61 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
45 base::Bind(SetUserCreatedLoginItemPrefCallback)); | 62 base::Bind(SetUserCreatedLoginItemPrefCallback)); |
46 return; | 63 return; |
47 } | 64 } |
48 | 65 |
49 base::mac::AddToLoginItems(true); // Hide on startup. | 66 if (should_add_login_item) |
67 base::mac::AddToLoginItems(true); // Hide on startup. | |
50 } | 68 } |
51 | 69 |
52 } // namespace | 70 } // namespace |
53 | 71 |
54 void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) { | 72 void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) { |
55 // This functionality is only defined for default profile, currently. | 73 // This functionality is only defined for default profile, currently. |
56 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir)) | 74 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir)) |
57 return; | 75 return; |
58 | 76 |
59 if (should_launch) { | 77 if (should_launch) { |
78 PrefService* service = g_browser_process->local_state(); | |
79 // Create a login item if the user did not remove our login item | |
80 // previously. We call out to the FILE thread either way since we | |
81 // want to check for a user-created login item. | |
82 bool should_add_login_item = | |
83 !service->GetBoolean(prefs::kUserRemovedLoginItem); | |
60 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 84 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
61 base::Bind(EnableLaunchOnStartupCallback)); | 85 base::Bind(EnableLaunchOnStartupCallback, |
86 should_add_login_item)); | |
62 } else { | 87 } else { |
63 PrefService* service = g_browser_process->local_state(); | 88 PrefService* service = g_browser_process->local_state(); |
64 if (service->GetBoolean(prefs::kUserCreatedLoginItem)) { | 89 if (service->GetBoolean(prefs::kUserCreatedLoginItem)) { |
65 // We didn't create the login item, so nothing to do here. | 90 // We didn't create the login item, so nothing to do here. Clear our |
91 // prefs so if the user removes the login item before installing a | |
92 // background app, we will revert to the default behavior. | |
66 service->ClearPref(prefs::kUserCreatedLoginItem); | 93 service->ClearPref(prefs::kUserCreatedLoginItem); |
94 service->ClearPref(prefs::kUserRemovedLoginItem); | |
67 return; | 95 return; |
68 } | 96 } |
69 // Call to the File thread to remove the login item since it requires | 97 // Call to the File thread to remove the login item since it requires |
70 // accessing the disk. | 98 // accessing the disk. |
71 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 99 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
72 base::Bind(DisableLaunchOnStartupCallback)); | 100 base::Bind(DisableLaunchOnStartupCallback)); |
73 } | 101 } |
74 } | 102 } |
75 | 103 |
76 void BackgroundModeManager::DisplayAppInstalledNotification( | 104 void BackgroundModeManager::DisplayAppInstalledNotification( |
77 const extensions::Extension* extension) { | 105 const extensions::Extension* extension) { |
78 // TODO(atwilson): Display a platform-appropriate notification here. | 106 // TODO(atwilson): Display a platform-appropriate notification here. |
79 // http://crbug.com/74970 | 107 // http://crbug.com/74970 |
80 } | 108 } |
81 | 109 |
82 string16 BackgroundModeManager::GetPreferencesMenuLabel() { | 110 string16 BackgroundModeManager::GetPreferencesMenuLabel() { |
83 return l10n_util::GetStringUTF16(IDS_OPTIONS); | 111 return l10n_util::GetStringUTF16(IDS_OPTIONS); |
84 } | 112 } |
OLD | NEW |