OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/auto_start_linux.h" | 5 #include "chrome/common/auto_start_linux.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
| 9 #include <memory> |
| 10 |
9 #include "base/environment.h" | 11 #include "base/environment.h" |
10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
11 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/nix/xdg_util.h" | 15 #include "base/nix/xdg_util.h" |
15 #include "base/strings/string_tokenizer.h" | 16 #include "base/strings/string_tokenizer.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 const base::FilePath::CharType kAutostart[] = "autostart"; | 20 const base::FilePath::CharType kAutostart[] = "autostart"; |
20 | 21 |
21 base::FilePath GetAutostartDirectory(base::Environment* environment) { | 22 base::FilePath GetAutostartDirectory(base::Environment* environment) { |
22 base::FilePath result = base::nix::GetXDGDirectory( | 23 base::FilePath result = base::nix::GetXDGDirectory( |
23 environment, | 24 environment, |
24 base::nix::kXdgConfigHomeEnvVar, | 25 base::nix::kXdgConfigHomeEnvVar, |
25 base::nix::kDotConfigDir); | 26 base::nix::kDotConfigDir); |
26 result = result.Append(kAutostart); | 27 result = result.Append(kAutostart); |
27 return result; | 28 return result; |
28 } | 29 } |
29 | 30 |
30 } // namespace | 31 } // namespace |
31 | 32 |
32 bool AutoStart::AddApplication(const std::string& autostart_filename, | 33 bool AutoStart::AddApplication(const std::string& autostart_filename, |
33 const std::string& application_name, | 34 const std::string& application_name, |
34 const std::string& command_line, | 35 const std::string& command_line, |
35 bool is_terminal_app) { | 36 bool is_terminal_app) { |
36 scoped_ptr<base::Environment> environment(base::Environment::Create()); | 37 std::unique_ptr<base::Environment> environment(base::Environment::Create()); |
37 base::FilePath autostart_directory = GetAutostartDirectory(environment.get()); | 38 base::FilePath autostart_directory = GetAutostartDirectory(environment.get()); |
38 if (!base::DirectoryExists(autostart_directory) && | 39 if (!base::DirectoryExists(autostart_directory) && |
39 !base::CreateDirectory(autostart_directory)) { | 40 !base::CreateDirectory(autostart_directory)) { |
40 return false; | 41 return false; |
41 } | 42 } |
42 | 43 |
43 base::FilePath autostart_file = | 44 base::FilePath autostart_file = |
44 autostart_directory.Append(autostart_filename); | 45 autostart_directory.Append(autostart_filename); |
45 std::string terminal = is_terminal_app ? "true" : "false"; | 46 std::string terminal = is_terminal_app ? "true" : "false"; |
46 std::string autostart_file_contents = | 47 std::string autostart_file_contents = |
47 "[Desktop Entry]\n" | 48 "[Desktop Entry]\n" |
48 "Type=Application\n" | 49 "Type=Application\n" |
49 "Terminal=" + terminal + "\n" | 50 "Terminal=" + terminal + "\n" |
50 "Exec=" + command_line + "\n" | 51 "Exec=" + command_line + "\n" |
51 "Name=" + application_name + "\n"; | 52 "Name=" + application_name + "\n"; |
52 std::string::size_type content_length = autostart_file_contents.length(); | 53 std::string::size_type content_length = autostart_file_contents.length(); |
53 if (base::WriteFile(autostart_file, autostart_file_contents.c_str(), | 54 if (base::WriteFile(autostart_file, autostart_file_contents.c_str(), |
54 content_length) != | 55 content_length) != |
55 static_cast<int>(content_length)) { | 56 static_cast<int>(content_length)) { |
56 base::DeleteFile(autostart_file, false); | 57 base::DeleteFile(autostart_file, false); |
57 return false; | 58 return false; |
58 } | 59 } |
59 return true; | 60 return true; |
60 } | 61 } |
61 | 62 |
62 bool AutoStart::Remove(const std::string& autostart_filename) { | 63 bool AutoStart::Remove(const std::string& autostart_filename) { |
63 scoped_ptr<base::Environment> environment(base::Environment::Create()); | 64 std::unique_ptr<base::Environment> environment(base::Environment::Create()); |
64 base::FilePath autostart_directory = GetAutostartDirectory(environment.get()); | 65 base::FilePath autostart_directory = GetAutostartDirectory(environment.get()); |
65 base::FilePath autostart_file = | 66 base::FilePath autostart_file = |
66 autostart_directory.Append(autostart_filename); | 67 autostart_directory.Append(autostart_filename); |
67 return base::DeleteFile(autostart_file, false); | 68 return base::DeleteFile(autostart_file, false); |
68 } | 69 } |
69 | 70 |
70 bool AutoStart::GetAutostartFileContents( | 71 bool AutoStart::GetAutostartFileContents( |
71 const std::string& autostart_filename, std::string* contents) { | 72 const std::string& autostart_filename, std::string* contents) { |
72 scoped_ptr<base::Environment> environment(base::Environment::Create()); | 73 std::unique_ptr<base::Environment> environment(base::Environment::Create()); |
73 base::FilePath autostart_directory = GetAutostartDirectory(environment.get()); | 74 base::FilePath autostart_directory = GetAutostartDirectory(environment.get()); |
74 base::FilePath autostart_file = | 75 base::FilePath autostart_file = |
75 autostart_directory.Append(autostart_filename); | 76 autostart_directory.Append(autostart_filename); |
76 return base::ReadFileToString(autostart_file, contents); | 77 return base::ReadFileToString(autostart_file, contents); |
77 } | 78 } |
78 | 79 |
79 bool AutoStart::GetAutostartFileValue(const std::string& autostart_filename, | 80 bool AutoStart::GetAutostartFileValue(const std::string& autostart_filename, |
80 const std::string& value_name, | 81 const std::string& value_name, |
81 std::string* value) { | 82 std::string* value) { |
82 std::string contents; | 83 std::string contents; |
83 if (!GetAutostartFileContents(autostart_filename, &contents)) | 84 if (!GetAutostartFileContents(autostart_filename, &contents)) |
84 return false; | 85 return false; |
85 base::StringTokenizer tokenizer(contents, "\n"); | 86 base::StringTokenizer tokenizer(contents, "\n"); |
86 std::string token = value_name + "="; | 87 std::string token = value_name + "="; |
87 while (tokenizer.GetNext()) { | 88 while (tokenizer.GetNext()) { |
88 if (tokenizer.token().substr(0, token.length()) == token) { | 89 if (tokenizer.token().substr(0, token.length()) == token) { |
89 *value = tokenizer.token().substr(token.length()); | 90 *value = tokenizer.token().substr(token.length()); |
90 return true; | 91 return true; |
91 } | 92 } |
92 } | 93 } |
93 return false; | 94 return false; |
94 } | 95 } |
OLD | NEW |