• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "chrome/common/auto_start_linux.h"
6 
7 #include "base/environment.h"
8 #include "base/file_path.h"
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "base/nix/xdg_util.h"
12 #include "base/string_tokenizer.h"
13 
14 namespace {
15 
16 const FilePath::CharType kAutostart[] = "autostart";
17 const FilePath::CharType kConfig[] = ".config";
18 const char kXdgConfigHome[] = "XDG_CONFIG_HOME";
19 
GetAutostartDirectory(base::Environment * environment)20 FilePath GetAutostartDirectory(base::Environment* environment) {
21   FilePath result =
22       base::nix::GetXDGDirectory(environment, kXdgConfigHome, kConfig);
23   result = result.Append(kAutostart);
24   return result;
25 }
26 
27 }  // namespace
28 
AddApplication(const std::string & autostart_filename,const std::string & application_name,const std::string & command_line,bool is_terminal_app)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   FilePath autostart_directory = GetAutostartDirectory(environment.get());
35   if (!file_util::DirectoryExists(autostart_directory) &&
36       !file_util::CreateDirectory(autostart_directory)) {
37     return false;
38   }
39 
40   FilePath autostart_file = autostart_directory.Append(autostart_filename);
41   std::string terminal = is_terminal_app ? "true" : "false";
42   std::string autostart_file_contents =
43       "[Desktop Entry]\n"
44       "Type=Application\n"
45       "Terminal=" + terminal + "\n"
46       "Exec=" + command_line + "\n"
47       "Name=" + application_name + "\n";
48   std::string::size_type content_length = autostart_file_contents.length();
49   if (file_util::WriteFile(autostart_file, autostart_file_contents.c_str(),
50                            content_length) !=
51       static_cast<int>(content_length)) {
52     file_util::Delete(autostart_file, false);
53     return false;
54   }
55   return true;
56 }
57 
Remove(const std::string & autostart_filename)58 bool AutoStart::Remove(const std::string& autostart_filename) {
59   scoped_ptr<base::Environment> environment(base::Environment::Create());
60   FilePath autostart_directory = GetAutostartDirectory(environment.get());
61   FilePath autostart_file = autostart_directory.Append(autostart_filename);
62   return file_util::Delete(autostart_file, false);
63 }
64 
GetAutostartFileContents(const std::string & autostart_filename,std::string * contents)65 bool AutoStart::GetAutostartFileContents(
66     const std::string& autostart_filename, std::string* contents) {
67   scoped_ptr<base::Environment> environment(base::Environment::Create());
68   FilePath autostart_directory = GetAutostartDirectory(environment.get());
69   FilePath autostart_file = autostart_directory.Append(autostart_filename);
70   return file_util::ReadFileToString(autostart_file, contents);
71 }
72 
GetAutostartFileValue(const std::string & autostart_filename,const std::string & value_name,std::string * value)73 bool AutoStart::GetAutostartFileValue(const std::string& autostart_filename,
74                                       const std::string& value_name,
75                                       std::string* value) {
76   std::string contents;
77   if (!GetAutostartFileContents(autostart_filename, &contents))
78     return false;
79   StringTokenizer tokenizer(contents, "\n");
80   std::string token = value_name + "=";
81   while (tokenizer.GetNext()) {
82     if (tokenizer.token().substr(0, token.length()) == token) {
83       *value = tokenizer.token().substr(token.length());
84       return true;
85     }
86   }
87   return false;
88 }
89