OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <unistd.h> |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/environment.h" |
| 10 #include "base/file_path.h" |
| 11 #include "base/file_util.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/nix/xdg_util.h" |
| 14 #include "base/task.h" |
| 15 #include "chrome/browser/background_mode_manager.h" |
| 16 #include "chrome/browser/browser_thread.h" |
| 17 #include "chrome/browser/gtk/gtk_util.h" |
| 18 #include "chrome/browser/prefs/pref_service.h" |
| 19 #include "chrome/browser/shell_integration.h" |
| 20 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/common/chrome_version_info.h" |
| 22 #include "chrome/common/pref_names.h" |
| 23 #include "grit/generated_resources.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 class DisableLaunchOnStartupTask : public Task { |
| 28 public: |
| 29 virtual void Run(); |
| 30 }; |
| 31 |
| 32 class EnableLaunchOnStartupTask : public Task { |
| 33 public: |
| 34 virtual void Run(); |
| 35 }; |
| 36 |
| 37 static const FilePath::CharType kAutostart[] = "autostart"; |
| 38 static const FilePath::CharType kConfig[] = ".config"; |
| 39 static const char kXdgConfigHome[] = "XDG_CONFIG_HOME"; |
| 40 |
| 41 FilePath GetAutostartDirectory(base::Environment* environment) { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 43 FilePath result = |
| 44 base::nix::GetXDGDirectory(environment, kXdgConfigHome, kConfig); |
| 45 result = result.Append(kAutostart); |
| 46 return result; |
| 47 } |
| 48 |
| 49 FilePath GetAutostartFilename(base::Environment* environment) { |
| 50 FilePath directory = GetAutostartDirectory(environment); |
| 51 return directory.Append(ShellIntegration::GetDesktopName(environment)); |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
| 56 void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) { |
| 57 // This functionality is only defined for default profile, currently. |
| 58 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir)) |
| 59 return; |
| 60 if (should_launch) { |
| 61 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 62 new EnableLaunchOnStartupTask()); |
| 63 } else { |
| 64 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 65 new DisableLaunchOnStartupTask()); |
| 66 } |
| 67 } |
| 68 |
| 69 void DisableLaunchOnStartupTask::Run() { |
| 70 scoped_ptr<base::Environment> environment(base::Environment::Create()); |
| 71 if (!file_util::Delete(GetAutostartFilename(environment.get()), false)) { |
| 72 NOTREACHED() << "Failed to deregister launch on login."; |
| 73 } |
| 74 } |
| 75 |
| 76 // TODO(rickcam): Bug 56280: Share implementation with ShellIntegration |
| 77 void EnableLaunchOnStartupTask::Run() { |
| 78 scoped_ptr<base::Environment> environment(base::Environment::Create()); |
| 79 scoped_ptr<chrome::VersionInfo> version_info(new chrome::VersionInfo()); |
| 80 FilePath autostart_directory = GetAutostartDirectory(environment.get()); |
| 81 FilePath autostart_file = GetAutostartFilename(environment.get()); |
| 82 if (!file_util::DirectoryExists(autostart_directory) && |
| 83 !file_util::CreateDirectory(autostart_directory)) { |
| 84 NOTREACHED() |
| 85 << "Failed to register launch on login. No autostart directory."; |
| 86 return; |
| 87 } |
| 88 std::string wrapper_script; |
| 89 if (!environment->GetVar("CHROME_WRAPPER", &wrapper_script)) { |
| 90 LOG(WARNING) |
| 91 << "Failed to register launch on login. CHROME_WRAPPER not set."; |
| 92 return; |
| 93 } |
| 94 std::string autostart_file_contents = |
| 95 "[Desktop Entry]\n" |
| 96 "Type=Application\n" |
| 97 "Terminal=false\n" |
| 98 "Exec=" + wrapper_script + |
| 99 " --enable-background-mode --no-startup-window\n" |
| 100 "Name=" + version_info->Name() + "\n"; |
| 101 std::string::size_type content_length = autostart_file_contents.length(); |
| 102 if (file_util::WriteFile(autostart_file, autostart_file_contents.c_str(), |
| 103 content_length) != |
| 104 static_cast<int>(content_length)) { |
| 105 NOTREACHED() << "Failed to register launch on login. Failed to write " |
| 106 << autostart_file.value(); |
| 107 file_util::Delete(GetAutostartFilename(environment.get()), false); |
| 108 } |
| 109 } |
| 110 |
| 111 string16 BackgroundModeManager::GetPreferencesMenuLabel() { |
| 112 string16 result = gtk_util::GetStockPreferencesMenuLabel(); |
| 113 if (!result.empty()) |
| 114 return result; |
| 115 return l10n_util::GetStringUTF16(IDS_PREFERENCES); |
| 116 } |
OLD | NEW |