1 // Copyright (c) 2012 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.
5 #include "chrome/common/auto_start_linux.h"
7 #include "base/environment.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/nix/xdg_util.h"
12 #include "base/strings/string_tokenizer.h"
16 const base::FilePath::CharType kAutostart
[] = "autostart";
18 base::FilePath
GetAutostartDirectory(base::Environment
* environment
) {
19 base::FilePath result
= base::nix::GetXDGDirectory(
21 base::nix::kXdgConfigHomeEnvVar
,
22 base::nix::kDotConfigDir
);
23 result
= result
.Append(kAutostart
);
29 bool AutoStart::AddApplication(const std::string
& autostart_filename
,
30 const std::string
& application_name
,
31 const std::string
& command_line
,
32 bool is_terminal_app
) {
33 scoped_ptr
<base::Environment
> environment(base::Environment::Create());
34 base::FilePath autostart_directory
= GetAutostartDirectory(environment
.get());
35 if (!base::DirectoryExists(autostart_directory
) &&
36 !base::CreateDirectory(autostart_directory
)) {
40 base::FilePath autostart_file
=
41 autostart_directory
.Append(autostart_filename
);
42 std::string terminal
= is_terminal_app
? "true" : "false";
43 std::string autostart_file_contents
=
46 "Terminal=" + terminal
+ "\n"
47 "Exec=" + command_line
+ "\n"
48 "Name=" + application_name
+ "\n";
49 std::string::size_type content_length
= autostart_file_contents
.length();
50 if (base::WriteFile(autostart_file
, autostart_file_contents
.c_str(),
52 static_cast<int>(content_length
)) {
53 base::DeleteFile(autostart_file
, false);
59 bool AutoStart::Remove(const std::string
& autostart_filename
) {
60 scoped_ptr
<base::Environment
> environment(base::Environment::Create());
61 base::FilePath autostart_directory
= GetAutostartDirectory(environment
.get());
62 base::FilePath autostart_file
=
63 autostart_directory
.Append(autostart_filename
);
64 return base::DeleteFile(autostart_file
, false);
67 bool AutoStart::GetAutostartFileContents(
68 const std::string
& autostart_filename
, std::string
* contents
) {
69 scoped_ptr
<base::Environment
> environment(base::Environment::Create());
70 base::FilePath autostart_directory
= GetAutostartDirectory(environment
.get());
71 base::FilePath autostart_file
=
72 autostart_directory
.Append(autostart_filename
);
73 return base::ReadFileToString(autostart_file
, contents
);
76 bool AutoStart::GetAutostartFileValue(const std::string
& autostart_filename
,
77 const std::string
& value_name
,
80 if (!GetAutostartFileContents(autostart_filename
, &contents
))
82 base::StringTokenizer
tokenizer(contents
, "\n");
83 std::string token
= value_name
+ "=";
84 while (tokenizer
.GetNext()) {
85 if (tokenizer
.token().substr(0, token
.length()) == token
) {
86 *value
= tokenizer
.token().substr(token
.length());