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. 4 5 #include "chrome/common/auto_start_linux.h" 6 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" 13 14 namespace { 15 16 const base::FilePath::CharType kAutostart[] = "autostart"; 17 18 base::FilePath GetAutostartDirectory(base::Environment* environment) { 19 base::FilePath result = base::nix::GetXDGDirectory( 20 environment, 21 base::nix::kXdgConfigHomeEnvVar, 22 base::nix::kDotConfigDir); 23 result = result.Append(kAutostart); 24 return result; 25 } 26 27 } // namespace 28 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)) { 37 return false; 38 } 39 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 = 44 "[Desktop Entry]\n" 45 "Type=Application\n" 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(), 51 content_length) != 52 static_cast<int>(content_length)) { 53 base::DeleteFile(autostart_file, false); 54 return false; 55 } 56 return true; 57 } 58 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); 65 } 66 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); 74 } 75 76 bool AutoStart::GetAutostartFileValue(const std::string& autostart_filename, 77 const std::string& value_name, 78 std::string* value) { 79 std::string contents; 80 if (!GetAutostartFileContents(autostart_filename, &contents)) 81 return false; 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()); 87 return true; 88 } 89 } 90 return false; 91 } 92