OLD | NEW |
1 // Copyright (c) 2010 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/environment.h" |
19 #include "base/file_path.h" | 19 #include "base/file_path.h" |
20 #include "base/file_util.h" | 20 #include "base/file_util.h" |
21 #include "base/i18n/file_util_icu.h" | 21 #include "base/i18n/file_util_icu.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_number_conversions.h" | 26 #include "base/string_number_conversions.h" |
27 #include "base/string_tokenizer.h" | 27 #include "base/string_tokenizer.h" |
28 #include "base/string_util.h" | 28 #include "base/string_util.h" |
29 #include "base/task.h" | 29 #include "base/task.h" |
30 #include "base/thread.h" | 30 #include "base/thread.h" |
31 #include "base/string_number_conversions.h" | 31 #include "base/string_number_conversions.h" |
32 #include "base/utf_string_conversions.h" | 32 #include "base/utf_string_conversions.h" |
33 #include "chrome/browser/browser_process.h" | 33 #include "chrome/browser/browser_process.h" |
34 #include "chrome/common/chrome_constants.h" | 34 #include "chrome/common/chrome_constants.h" |
35 #include "chrome/common/chrome_paths.h" | 35 #include "chrome/common/chrome_paths.h" |
36 #include "chrome/common/chrome_plugin_util.h" | 36 #include "chrome/common/chrome_plugin_util.h" |
37 #include "chrome/common/chrome_switches.h" | 37 #include "chrome/common/chrome_switches.h" |
38 #include "chrome/browser/chrome_thread.h" | 38 #include "chrome/browser/chrome_thread.h" |
39 #include "gfx/codec/png_codec.h" | 39 #include "gfx/codec/png_codec.h" |
40 #include "googleurl/src/gurl.h" | 40 #include "googleurl/src/gurl.h" |
41 | 41 |
42 namespace { | 42 namespace { |
43 | 43 |
44 std::string GetDesktopName(base::EnvVarGetter* env_getter) { | 44 std::string GetDesktopName(base::Environment* env) { |
45 #if defined(GOOGLE_CHROME_BUILD) | 45 #if defined(GOOGLE_CHROME_BUILD) |
46 return "google-chrome.desktop"; | 46 return "google-chrome.desktop"; |
47 #else // CHROMIUM_BUILD | 47 #else // CHROMIUM_BUILD |
48 // Allow $CHROME_DESKTOP to override the built-in value, so that development | 48 // Allow $CHROME_DESKTOP to override the built-in value, so that development |
49 // versions can set themselves as the default without interfering with | 49 // versions can set themselves as the default without interfering with |
50 // non-official, packaged versions using the built-in value. | 50 // non-official, packaged versions using the built-in value. |
51 std::string name; | 51 std::string name; |
52 if (env_getter->GetEnv("CHROME_DESKTOP", &name) && !name.empty()) | 52 if (env->GetEnv("CHROME_DESKTOP", &name) && !name.empty()) |
53 return name; | 53 return name; |
54 return "chromium-browser.desktop"; | 54 return "chromium-browser.desktop"; |
55 #endif | 55 #endif |
56 } | 56 } |
57 | 57 |
58 // Helper to launch xdg scripts. We don't want them to ask any questions on the | 58 // Helper to launch xdg scripts. We don't want them to ask any questions on the |
59 // terminal etc. | 59 // terminal etc. |
60 bool LaunchXdgUtility(const std::vector<std::string>& argv) { | 60 bool LaunchXdgUtility(const std::vector<std::string>& argv) { |
61 // xdg-settings internally runs xdg-mime, which uses mv to move newly-created | 61 // xdg-settings internally runs xdg-mime, which uses mv to move newly-created |
62 // files on top of originals after making changes to them. In the event that | 62 // files on top of originals after making changes to them. In the event that |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 194 |
195 } // namespace | 195 } // namespace |
196 | 196 |
197 // We delegate the difficulty of setting the default browser in Linux desktop | 197 // We delegate the difficulty of setting the default browser in Linux desktop |
198 // environments to a new xdg utility, xdg-settings. We have to include a copy of | 198 // environments to a new xdg utility, xdg-settings. We have to include a copy of |
199 // it for this to work, obviously, but that's actually the suggested approach | 199 // it for this to work, obviously, but that's actually the suggested approach |
200 // for xdg utilities anyway. | 200 // for xdg utilities anyway. |
201 | 201 |
202 // static | 202 // static |
203 bool ShellIntegration::SetAsDefaultBrowser() { | 203 bool ShellIntegration::SetAsDefaultBrowser() { |
204 scoped_ptr<base::EnvVarGetter> env_getter(base::EnvVarGetter::Create()); | 204 scoped_ptr<base::Environment> env(base::Environment::Create()); |
205 | 205 |
206 std::vector<std::string> argv; | 206 std::vector<std::string> argv; |
207 argv.push_back("xdg-settings"); | 207 argv.push_back("xdg-settings"); |
208 argv.push_back("set"); | 208 argv.push_back("set"); |
209 argv.push_back("default-web-browser"); | 209 argv.push_back("default-web-browser"); |
210 argv.push_back(GetDesktopName(env_getter.get())); | 210 argv.push_back(GetDesktopName(env.get())); |
211 return LaunchXdgUtility(argv); | 211 return LaunchXdgUtility(argv); |
212 } | 212 } |
213 | 213 |
214 // static | 214 // static |
215 ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() { | 215 ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() { |
216 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 216 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
217 | 217 |
218 scoped_ptr<base::EnvVarGetter> env_getter(base::EnvVarGetter::Create()); | 218 scoped_ptr<base::Environment> env(base::Environment::Create()); |
219 | 219 |
220 std::vector<std::string> argv; | 220 std::vector<std::string> argv; |
221 argv.push_back("xdg-settings"); | 221 argv.push_back("xdg-settings"); |
222 argv.push_back("check"); | 222 argv.push_back("check"); |
223 argv.push_back("default-web-browser"); | 223 argv.push_back("default-web-browser"); |
224 argv.push_back(GetDesktopName(env_getter.get())); | 224 argv.push_back(GetDesktopName(env.get())); |
225 | 225 |
226 std::string reply; | 226 std::string reply; |
227 if (!base::GetAppOutput(CommandLine(argv), &reply)) { | 227 if (!base::GetAppOutput(CommandLine(argv), &reply)) { |
228 // xdg-settings failed: we can't determine or set the default browser. | 228 // xdg-settings failed: we can't determine or set the default browser. |
229 return UNKNOWN_DEFAULT_BROWSER; | 229 return UNKNOWN_DEFAULT_BROWSER; |
230 } | 230 } |
231 | 231 |
232 // Allow any reply that starts with "yes". | 232 // Allow any reply that starts with "yes". |
233 return (reply.find("yes") == 0) ? IS_DEFAULT_BROWSER : NOT_DEFAULT_BROWSER; | 233 return (reply.find("yes") == 0) ? IS_DEFAULT_BROWSER : NOT_DEFAULT_BROWSER; |
234 } | 234 } |
235 | 235 |
236 // static | 236 // static |
237 bool ShellIntegration::IsFirefoxDefaultBrowser() { | 237 bool ShellIntegration::IsFirefoxDefaultBrowser() { |
238 std::vector<std::string> argv; | 238 std::vector<std::string> argv; |
239 argv.push_back("xdg-settings"); | 239 argv.push_back("xdg-settings"); |
240 argv.push_back("get"); | 240 argv.push_back("get"); |
241 argv.push_back("default-web-browser"); | 241 argv.push_back("default-web-browser"); |
242 | 242 |
243 std::string browser; | 243 std::string browser; |
244 // We don't care about the return value here. | 244 // We don't care about the return value here. |
245 base::GetAppOutput(CommandLine(argv), &browser); | 245 base::GetAppOutput(CommandLine(argv), &browser); |
246 return browser.find("irefox") != std::string::npos; | 246 return browser.find("irefox") != std::string::npos; |
247 } | 247 } |
248 | 248 |
249 // static | 249 // static |
250 bool ShellIntegration::GetDesktopShortcutTemplate( | 250 bool ShellIntegration::GetDesktopShortcutTemplate( |
251 base::EnvVarGetter* env_getter, std::string* output) { | 251 base::Environment* env, std::string* output) { |
252 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 252 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
253 | 253 |
254 std::vector<FilePath> search_paths; | 254 std::vector<FilePath> search_paths; |
255 | 255 |
256 std::string xdg_data_home; | 256 std::string xdg_data_home; |
257 if (env_getter->GetEnv("XDG_DATA_HOME", &xdg_data_home) && | 257 if (env->GetEnv("XDG_DATA_HOME", &xdg_data_home) && |
258 !xdg_data_home.empty()) { | 258 !xdg_data_home.empty()) { |
259 search_paths.push_back(FilePath(xdg_data_home)); | 259 search_paths.push_back(FilePath(xdg_data_home)); |
260 } | 260 } |
261 | 261 |
262 std::string xdg_data_dirs; | 262 std::string xdg_data_dirs; |
263 if (env_getter->GetEnv("XDG_DATA_DIRS", &xdg_data_dirs) && | 263 if (env->GetEnv("XDG_DATA_DIRS", &xdg_data_dirs) && |
264 !xdg_data_dirs.empty()) { | 264 !xdg_data_dirs.empty()) { |
265 StringTokenizer tokenizer(xdg_data_dirs, ":"); | 265 StringTokenizer tokenizer(xdg_data_dirs, ":"); |
266 while (tokenizer.GetNext()) { | 266 while (tokenizer.GetNext()) { |
267 FilePath data_dir(tokenizer.token()); | 267 FilePath data_dir(tokenizer.token()); |
268 search_paths.push_back(data_dir); | 268 search_paths.push_back(data_dir); |
269 search_paths.push_back(data_dir.Append("applications")); | 269 search_paths.push_back(data_dir.Append("applications")); |
270 } | 270 } |
271 } | 271 } |
272 | 272 |
273 // Add some fallback paths for systems which don't have XDG_DATA_DIRS or have | 273 // Add some fallback paths for systems which don't have XDG_DATA_DIRS or have |
274 // it incomplete. | 274 // it incomplete. |
275 search_paths.push_back(FilePath("/usr/share/applications")); | 275 search_paths.push_back(FilePath("/usr/share/applications")); |
276 search_paths.push_back(FilePath("/usr/local/share/applications")); | 276 search_paths.push_back(FilePath("/usr/local/share/applications")); |
277 | 277 |
278 std::string template_filename(GetDesktopName(env_getter)); | 278 std::string template_filename(GetDesktopName(env)); |
279 for (std::vector<FilePath>::const_iterator i = search_paths.begin(); | 279 for (std::vector<FilePath>::const_iterator i = search_paths.begin(); |
280 i != search_paths.end(); ++i) { | 280 i != search_paths.end(); ++i) { |
281 FilePath path = (*i).Append(template_filename); | 281 FilePath path = (*i).Append(template_filename); |
282 LOG(INFO) << "Looking for desktop file template in " << path.value(); | 282 LOG(INFO) << "Looking for desktop file template in " << path.value(); |
283 if (file_util::PathExists(path)) { | 283 if (file_util::PathExists(path)) { |
284 LOG(INFO) << "Found desktop file template at " << path.value(); | 284 LOG(INFO) << "Found desktop file template at " << path.value(); |
285 return file_util::ReadFileToString(path, output); | 285 return file_util::ReadFileToString(path, output); |
286 } | 286 } |
287 } | 287 } |
288 | 288 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 std::string contents = GetDesktopFileContents( | 381 std::string contents = GetDesktopFileContents( |
382 shortcut_template, shortcut_info.url, shortcut_info.extension_id, | 382 shortcut_template, shortcut_info.url, shortcut_info.extension_id, |
383 shortcut_info.title, icon_name); | 383 shortcut_info.title, icon_name); |
384 | 384 |
385 if (shortcut_info.create_on_desktop) | 385 if (shortcut_info.create_on_desktop) |
386 CreateShortcutOnDesktop(shortcut_filename, contents); | 386 CreateShortcutOnDesktop(shortcut_filename, contents); |
387 | 387 |
388 if (shortcut_info.create_in_applications_menu) | 388 if (shortcut_info.create_in_applications_menu) |
389 CreateShortcutInApplicationsMenu(shortcut_filename, contents); | 389 CreateShortcutInApplicationsMenu(shortcut_filename, contents); |
390 } | 390 } |
OLD | NEW |