diff options
author | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-23 02:45:34 +0000 |
---|---|---|
committer | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-23 02:45:34 +0000 |
commit | b907defea0f7059faa4502f505d793fd272860d2 (patch) | |
tree | 121f160f20893a4ba8841dab5980aa6c61d001e7 | |
parent | b1d454e9a59d482da63eba58ac6e08654524c996 (diff) | |
download | chromium_src-b907defea0f7059faa4502f505d793fd272860d2.zip chromium_src-b907defea0f7059faa4502f505d793fd272860d2.tar.gz chromium_src-b907defea0f7059faa4502f505d793fd272860d2.tar.bz2 |
Moved common parts of Linux autostart code to chrome/common so it can be reused in the service process as well.
BUG=None
TEST=None
Review URL: http://codereview.chromium.org/6546024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75699 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/background_mode_manager_linux.cc | 52 | ||||
-rw-r--r-- | chrome/chrome_common.gypi | 4 | ||||
-rw-r--r-- | chrome/common/auto_start_linux.cc | 61 | ||||
-rw-r--r-- | chrome/common/auto_start_linux.h | 28 |
4 files changed, 104 insertions, 41 deletions
diff --git a/chrome/browser/background_mode_manager_linux.cc b/chrome/browser/background_mode_manager_linux.cc index 4f4b5707..5ee1b0c 100644 --- a/chrome/browser/background_mode_manager_linux.cc +++ b/chrome/browser/background_mode_manager_linux.cc @@ -15,6 +15,7 @@ #include "chrome/browser/browser_thread.h" #include "chrome/browser/shell_integration.h" #include "chrome/browser/ui/gtk/gtk_util.h" +#include "chrome/common/auto_start_linux.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_version_info.h" #include "grit/generated_resources.h" @@ -32,23 +33,6 @@ class EnableLaunchOnStartupTask : public Task { virtual void Run(); }; -static const FilePath::CharType kAutostart[] = "autostart"; -static const FilePath::CharType kConfig[] = ".config"; -static const char kXdgConfigHome[] = "XDG_CONFIG_HOME"; - -FilePath GetAutostartDirectory(base::Environment* environment) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - FilePath result = - base::nix::GetXDGDirectory(environment, kXdgConfigHome, kConfig); - result = result.Append(kAutostart); - return result; -} - -FilePath GetAutostartFilename(base::Environment* environment) { - FilePath directory = GetAutostartDirectory(environment); - return directory.Append(ShellIntegration::GetDesktopName(environment)); -} - } // namespace void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) { @@ -66,43 +50,31 @@ void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) { void DisableLaunchOnStartupTask::Run() { scoped_ptr<base::Environment> environment(base::Environment::Create()); - if (!file_util::Delete(GetAutostartFilename(environment.get()), false)) { + if (!AutoStart::Remove(ShellIntegration::GetDesktopName(environment.get()))) { NOTREACHED() << "Failed to deregister launch on login."; } } // TODO(rickcam): Bug 56280: Share implementation with ShellIntegration void EnableLaunchOnStartupTask::Run() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); scoped_ptr<base::Environment> environment(base::Environment::Create()); scoped_ptr<chrome::VersionInfo> version_info(new chrome::VersionInfo()); - FilePath autostart_directory = GetAutostartDirectory(environment.get()); - FilePath autostart_file = GetAutostartFilename(environment.get()); - if (!file_util::DirectoryExists(autostart_directory) && - !file_util::CreateDirectory(autostart_directory)) { - NOTREACHED() - << "Failed to register launch on login. No autostart directory."; - return; - } + std::string wrapper_script; if (!environment->GetVar("CHROME_WRAPPER", &wrapper_script)) { LOG(WARNING) << "Failed to register launch on login. CHROME_WRAPPER not set."; return; } - std::string autostart_file_contents = - "[Desktop Entry]\n" - "Type=Application\n" - "Terminal=false\n" - "Exec=" + wrapper_script + - " --enable-background-mode --no-startup-window\n" - "Name=" + version_info->Name() + "\n"; - std::string::size_type content_length = autostart_file_contents.length(); - if (file_util::WriteFile(autostart_file, autostart_file_contents.c_str(), - content_length) != - static_cast<int>(content_length)) { - NOTREACHED() << "Failed to register launch on login. Failed to write " - << autostart_file.value(); - file_util::Delete(GetAutostartFilename(environment.get()), false); + std::string command_line = + wrapper_script + " --no-startup-window"; + if (!AutoStart::AddApplication( + ShellIntegration::GetDesktopName(environment.get()), + version_info->Name(), + command_line, + false)) { + NOTREACHED() << "Failed to register launch on login."; } } diff --git a/chrome/chrome_common.gypi b/chrome/chrome_common.gypi index 618364b..dbdc1c8 100644 --- a/chrome/chrome_common.gypi +++ b/chrome/chrome_common.gypi @@ -1,4 +1,4 @@ -# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -27,6 +27,8 @@ 'common/about_handler.h', 'common/app_mode_common_mac.h', 'common/app_mode_common_mac.mm', + 'common/auto_start_linux.cc', + 'common/auto_start_linux.h', 'common/autofill_messages.cc', 'common/autofill_messages.h', 'common/bindings_policy.h', diff --git a/chrome/common/auto_start_linux.cc b/chrome/common/auto_start_linux.cc new file mode 100644 index 0000000..7d97705 --- /dev/null +++ b/chrome/common/auto_start_linux.cc @@ -0,0 +1,61 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/auto_start_linux.h" + +#include "base/environment.h" +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/logging.h" +#include "base/nix/xdg_util.h" + +namespace { + +const FilePath::CharType kAutostart[] = "autostart"; +const FilePath::CharType kConfig[] = ".config"; +const char kXdgConfigHome[] = "XDG_CONFIG_HOME"; + +FilePath GetAutostartDirectory(base::Environment* environment) { + FilePath result = + base::nix::GetXDGDirectory(environment, kXdgConfigHome, kConfig); + result = result.Append(kAutostart); + return result; +} + +} // namespace + +bool AutoStart::AddApplication(const std::string& autostart_filename, + const std::string& application_name, + const std::string& command_line, + bool is_terminal_app) { + scoped_ptr<base::Environment> environment(base::Environment::Create()); + FilePath autostart_directory = GetAutostartDirectory(environment.get()); + if (!file_util::DirectoryExists(autostart_directory) && + !file_util::CreateDirectory(autostart_directory)) { + return false; + } + + FilePath autostart_file = autostart_directory.Append(autostart_filename); + std::string autostart_file_contents = + "[Desktop Entry]\n" + "Type=Application\n" + "Terminal=" + is_terminal_app ? "true\n" : "false\n" + "Exec=" + command_line + "\n" + "Name=" + application_name + "\n"; + std::string::size_type content_length = autostart_file_contents.length(); + if (file_util::WriteFile(autostart_file, autostart_file_contents.c_str(), + content_length) != + static_cast<int>(content_length)) { + file_util::Delete(autostart_file, false); + return false; + } + return true; +} + +bool AutoStart::Remove(const std::string& autostart_filename) { + scoped_ptr<base::Environment> environment(base::Environment::Create()); + FilePath autostart_directory = GetAutostartDirectory(environment.get()); + FilePath autostart_file = autostart_directory.Append(autostart_filename); + return file_util::Delete(autostart_file, false); +} diff --git a/chrome/common/auto_start_linux.h b/chrome/common/auto_start_linux.h new file mode 100644 index 0000000..9d7cdf0c --- /dev/null +++ b/chrome/common/auto_start_linux.h @@ -0,0 +1,28 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_COMMON_AUTO_START_LINUX_H_ +#define CHROME_COMMON_AUTO_START_LINUX_H_ +#pragma once + +#include <string> + +#include "base/basictypes.h" + +class AutoStart { + public: + // Registers an application to autostart on user login. |is_terminal_app| + // specifies whether the app will run in a terminal window. + static bool AddApplication(const std::string& autostart_filename, + const std::string& application_name, + const std::string& command_line, + bool is_terminal_app); + // Removes an autostart file. + static bool Remove(const std::string& autostart_filename); + + private: + DISALLOW_IMPLICIT_CONSTRUCTORS(AutoStart); +}; + +#endif // CHROME_COMMON_AUTO_START_LINUX_H_ |