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 /chrome/common/auto_start_linux.cc | |
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
Diffstat (limited to 'chrome/common/auto_start_linux.cc')
-rw-r--r-- | chrome/common/auto_start_linux.cc | 61 |
1 files changed, 61 insertions, 0 deletions
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); +} |