OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/browser/shell_integration.h" | 5 #include "chrome/browser/shell_integration.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
17 #include "base/eintr_wrapper.h" | 17 #include "base/eintr_wrapper.h" |
| 18 #include "base/env_var.h" |
18 #include "base/file_path.h" | 19 #include "base/file_path.h" |
19 #include "base/file_util.h" | 20 #include "base/file_util.h" |
20 #include "base/i18n/file_util_icu.h" | 21 #include "base/i18n/file_util_icu.h" |
21 #include "base/linux_util.h" | |
22 #include "base/message_loop.h" | 22 #include "base/message_loop.h" |
23 #include "base/path_service.h" | 23 #include "base/path_service.h" |
24 #include "base/process_util.h" | 24 #include "base/process_util.h" |
25 #include "base/scoped_temp_dir.h" | 25 #include "base/scoped_temp_dir.h" |
26 #include "base/string_tokenizer.h" | 26 #include "base/string_tokenizer.h" |
27 #include "base/task.h" | 27 #include "base/task.h" |
28 #include "base/thread.h" | 28 #include "base/thread.h" |
29 #include "base/utf_string_conversions.h" | 29 #include "base/utf_string_conversions.h" |
30 #include "chrome/browser/browser_process.h" | 30 #include "chrome/browser/browser_process.h" |
31 #include "chrome/common/chrome_constants.h" | 31 #include "chrome/common/chrome_constants.h" |
32 #include "chrome/common/chrome_paths.h" | 32 #include "chrome/common/chrome_paths.h" |
33 #include "chrome/common/chrome_plugin_util.h" | 33 #include "chrome/common/chrome_plugin_util.h" |
34 #include "chrome/common/chrome_switches.h" | 34 #include "chrome/common/chrome_switches.h" |
35 #include "chrome/browser/chrome_thread.h" | 35 #include "chrome/browser/chrome_thread.h" |
36 #include "gfx/codec/png_codec.h" | 36 #include "gfx/codec/png_codec.h" |
37 #include "googleurl/src/gurl.h" | 37 #include "googleurl/src/gurl.h" |
38 | 38 |
39 namespace { | 39 namespace { |
40 | 40 |
41 std::string GetDesktopName(base::EnvironmentVariableGetter* env_getter) { | 41 std::string GetDesktopName(base::EnvVarGetter* env_getter) { |
42 #if defined(GOOGLE_CHROME_BUILD) | 42 #if defined(GOOGLE_CHROME_BUILD) |
43 return "google-chrome.desktop"; | 43 return "google-chrome.desktop"; |
44 #else // CHROMIUM_BUILD | 44 #else // CHROMIUM_BUILD |
45 // Allow $CHROME_DESKTOP to override the built-in value, so that development | 45 // Allow $CHROME_DESKTOP to override the built-in value, so that development |
46 // versions can set themselves as the default without interfering with | 46 // versions can set themselves as the default without interfering with |
47 // non-official, packaged versions using the built-in value. | 47 // non-official, packaged versions using the built-in value. |
48 std::string name; | 48 std::string name; |
49 if (env_getter->Getenv("CHROME_DESKTOP", &name) && !name.empty()) | 49 if (env_getter->GetEnv("CHROME_DESKTOP", &name) && !name.empty()) |
50 return name; | 50 return name; |
51 return "chromium-browser.desktop"; | 51 return "chromium-browser.desktop"; |
52 #endif | 52 #endif |
53 } | 53 } |
54 | 54 |
55 // Helper to launch xdg scripts. We don't want them to ask any questions on the | 55 // Helper to launch xdg scripts. We don't want them to ask any questions on the |
56 // terminal etc. | 56 // terminal etc. |
57 bool LaunchXdgUtility(const std::vector<std::string>& argv) { | 57 bool LaunchXdgUtility(const std::vector<std::string>& argv) { |
58 // xdg-settings internally runs xdg-mime, which uses mv to move newly-created | 58 // xdg-settings internally runs xdg-mime, which uses mv to move newly-created |
59 // files on top of originals after making changes to them. In the event that | 59 // files on top of originals after making changes to them. In the event that |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 | 188 |
189 } // namespace | 189 } // namespace |
190 | 190 |
191 // We delegate the difficulty of setting the default browser in Linux desktop | 191 // We delegate the difficulty of setting the default browser in Linux desktop |
192 // environments to a new xdg utility, xdg-settings. We have to include a copy of | 192 // environments to a new xdg utility, xdg-settings. We have to include a copy of |
193 // it for this to work, obviously, but that's actually the suggested approach | 193 // it for this to work, obviously, but that's actually the suggested approach |
194 // for xdg utilities anyway. | 194 // for xdg utilities anyway. |
195 | 195 |
196 // static | 196 // static |
197 bool ShellIntegration::SetAsDefaultBrowser() { | 197 bool ShellIntegration::SetAsDefaultBrowser() { |
198 scoped_ptr<base::EnvironmentVariableGetter> env_getter( | 198 scoped_ptr<base::EnvVarGetter> env_getter(base::EnvVarGetter::Create()); |
199 base::EnvironmentVariableGetter::Create()); | |
200 | 199 |
201 std::vector<std::string> argv; | 200 std::vector<std::string> argv; |
202 argv.push_back("xdg-settings"); | 201 argv.push_back("xdg-settings"); |
203 argv.push_back("set"); | 202 argv.push_back("set"); |
204 argv.push_back("default-web-browser"); | 203 argv.push_back("default-web-browser"); |
205 argv.push_back(GetDesktopName(env_getter.get())); | 204 argv.push_back(GetDesktopName(env_getter.get())); |
206 return LaunchXdgUtility(argv); | 205 return LaunchXdgUtility(argv); |
207 } | 206 } |
208 | 207 |
209 // static | 208 // static |
210 ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() { | 209 ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() { |
211 scoped_ptr<base::EnvironmentVariableGetter> env_getter( | 210 scoped_ptr<base::EnvVarGetter> env_getter(base::EnvVarGetter::Create()); |
212 base::EnvironmentVariableGetter::Create()); | |
213 | 211 |
214 std::vector<std::string> argv; | 212 std::vector<std::string> argv; |
215 argv.push_back("xdg-settings"); | 213 argv.push_back("xdg-settings"); |
216 argv.push_back("check"); | 214 argv.push_back("check"); |
217 argv.push_back("default-web-browser"); | 215 argv.push_back("default-web-browser"); |
218 argv.push_back(GetDesktopName(env_getter.get())); | 216 argv.push_back(GetDesktopName(env_getter.get())); |
219 | 217 |
220 std::string reply; | 218 std::string reply; |
221 if (!base::GetAppOutput(CommandLine(argv), &reply)) { | 219 if (!base::GetAppOutput(CommandLine(argv), &reply)) { |
222 // xdg-settings failed: we can't determine or set the default browser. | 220 // xdg-settings failed: we can't determine or set the default browser. |
(...skipping 12 matching lines...) Expand all Loading... |
235 argv.push_back("default-web-browser"); | 233 argv.push_back("default-web-browser"); |
236 | 234 |
237 std::string browser; | 235 std::string browser; |
238 // We don't care about the return value here. | 236 // We don't care about the return value here. |
239 base::GetAppOutput(CommandLine(argv), &browser); | 237 base::GetAppOutput(CommandLine(argv), &browser); |
240 return browser.find("irefox") != std::string::npos; | 238 return browser.find("irefox") != std::string::npos; |
241 } | 239 } |
242 | 240 |
243 // static | 241 // static |
244 bool ShellIntegration::GetDesktopShortcutTemplate( | 242 bool ShellIntegration::GetDesktopShortcutTemplate( |
245 base::EnvironmentVariableGetter* env_getter, std::string* output) { | 243 base::EnvVarGetter* env_getter, std::string* output) { |
246 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 244 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
247 | 245 |
248 std::vector<FilePath> search_paths; | 246 std::vector<FilePath> search_paths; |
249 | 247 |
250 std::string xdg_data_home; | 248 std::string xdg_data_home; |
251 if (env_getter->Getenv("XDG_DATA_HOME", &xdg_data_home) && | 249 if (env_getter->GetEnv("XDG_DATA_HOME", &xdg_data_home) && |
252 !xdg_data_home.empty()) { | 250 !xdg_data_home.empty()) { |
253 search_paths.push_back(FilePath(xdg_data_home)); | 251 search_paths.push_back(FilePath(xdg_data_home)); |
254 } | 252 } |
255 | 253 |
256 std::string xdg_data_dirs; | 254 std::string xdg_data_dirs; |
257 if (env_getter->Getenv("XDG_DATA_DIRS", &xdg_data_dirs) && | 255 if (env_getter->GetEnv("XDG_DATA_DIRS", &xdg_data_dirs) && |
258 !xdg_data_dirs.empty()) { | 256 !xdg_data_dirs.empty()) { |
259 StringTokenizer tokenizer(xdg_data_dirs, ":"); | 257 StringTokenizer tokenizer(xdg_data_dirs, ":"); |
260 while (tokenizer.GetNext()) { | 258 while (tokenizer.GetNext()) { |
261 FilePath data_dir(tokenizer.token()); | 259 FilePath data_dir(tokenizer.token()); |
262 search_paths.push_back(data_dir); | 260 search_paths.push_back(data_dir); |
263 search_paths.push_back(data_dir.Append("applications")); | 261 search_paths.push_back(data_dir.Append("applications")); |
264 } | 262 } |
265 } | 263 } |
266 | 264 |
267 // Add some fallback paths for systems which don't have XDG_DATA_DIRS or have | 265 // Add some fallback paths for systems which don't have XDG_DATA_DIRS or have |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 std::string contents = GetDesktopFileContents( | 370 std::string contents = GetDesktopFileContents( |
373 shortcut_template, shortcut_info.url, shortcut_info.extension_id, | 371 shortcut_template, shortcut_info.url, shortcut_info.extension_id, |
374 shortcut_info.title, icon_name); | 372 shortcut_info.title, icon_name); |
375 | 373 |
376 if (shortcut_info.create_on_desktop) | 374 if (shortcut_info.create_on_desktop) |
377 CreateShortcutOnDesktop(shortcut_filename, contents); | 375 CreateShortcutOnDesktop(shortcut_filename, contents); |
378 | 376 |
379 if (shortcut_info.create_in_applications_menu) | 377 if (shortcut_info.create_in_applications_menu) |
380 CreateShortcutInApplicationsMenu(shortcut_filename, contents); | 378 CreateShortcutInApplicationsMenu(shortcut_filename, contents); |
381 } | 379 } |
OLD | NEW |