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.
5 #include "chrome/browser/shell_integration_linux.h"
15 #include <sys/types.h>
21 #include "base/base_paths.h"
22 #include "base/command_line.h"
23 #include "base/environment.h"
24 #include "base/files/file_enumerator.h"
25 #include "base/files/file_path.h"
26 #include "base/files/file_util.h"
27 #include "base/files/scoped_temp_dir.h"
28 #include "base/i18n/file_util_icu.h"
29 #include "base/memory/ref_counted_memory.h"
30 #include "base/memory/scoped_ptr.h"
31 #include "base/message_loop/message_loop.h"
32 #include "base/path_service.h"
33 #include "base/posix/eintr_wrapper.h"
34 #include "base/process/kill.h"
35 #include "base/process/launch.h"
36 #include "base/strings/string_number_conversions.h"
37 #include "base/strings/string_tokenizer.h"
38 #include "base/strings/string_util.h"
39 #include "base/strings/utf_string_conversions.h"
40 #include "base/threading/thread.h"
41 #include "base/threading/thread_restrictions.h"
42 #include "build/build_config.h"
43 #include "chrome/browser/shell_integration.h"
44 #include "chrome/common/chrome_constants.h"
45 #include "chrome/common/chrome_switches.h"
46 #include "chrome/common/chrome_version_info.h"
47 #include "content/public/browser/browser_thread.h"
48 #include "grit/chrome_unscaled_resources.h"
49 #include "ui/base/resource/resource_bundle.h"
50 #include "ui/gfx/image/image_family.h"
53 using content::BrowserThread
;
57 // The Categories for the App Launcher desktop shortcut. Should be the same as
58 // the Chrome desktop shortcut, so they are in the same sub-menu.
59 const char kAppListCategories
[] = "Network;WebBrowser;";
61 // Helper to launch xdg scripts. We don't want them to ask any questions on the
62 // terminal etc. The function returns true if the utility launches and exits
63 // cleanly, in which case |exit_code| returns the utility's exit code.
64 bool LaunchXdgUtility(const std::vector
<std::string
>& argv
, int* exit_code
) {
65 // xdg-settings internally runs xdg-mime, which uses mv to move newly-created
66 // files on top of originals after making changes to them. In the event that
67 // the original files are owned by another user (e.g. root, which can happen
68 // if they are updated within sudo), mv will prompt the user to confirm if
69 // standard input is a terminal (otherwise it just does it). So make sure it's
70 // not, to avoid locking everything up waiting for mv.
71 *exit_code
= EXIT_FAILURE
;
72 int devnull
= open("/dev/null", O_RDONLY
);
75 base::FileHandleMappingVector no_stdin
;
76 no_stdin
.push_back(std::make_pair(devnull
, STDIN_FILENO
));
78 base::ProcessHandle handle
;
79 base::LaunchOptions options
;
80 options
.fds_to_remap
= &no_stdin
;
81 if (!base::LaunchProcess(argv
, options
, &handle
)) {
87 return base::WaitForExitCode(handle
, exit_code
);
90 std::string
CreateShortcutIcon(const gfx::ImageFamily
& icon_images
,
91 const base::FilePath
& shortcut_filename
) {
92 if (icon_images
.empty())
95 // TODO(phajdan.jr): Report errors from this function, possibly as infobars.
96 base::ScopedTempDir temp_dir
;
97 if (!temp_dir
.CreateUniqueTempDir())
100 base::FilePath temp_file_path
= temp_dir
.path().Append(
101 shortcut_filename
.ReplaceExtension("png"));
102 std::string icon_name
= temp_file_path
.BaseName().RemoveExtension().value();
104 for (gfx::ImageFamily::const_iterator it
= icon_images
.begin();
105 it
!= icon_images
.end(); ++it
) {
106 int width
= it
->Width();
107 scoped_refptr
<base::RefCountedMemory
> png_data
= it
->As1xPNGBytes();
108 if (png_data
->size() == 0) {
109 // If the bitmap could not be encoded to PNG format, skip it.
110 LOG(WARNING
) << "Could not encode icon " << icon_name
<< ".png at size "
114 int bytes_written
= base::WriteFile(temp_file_path
,
115 png_data
->front_as
<char>(),
118 if (bytes_written
!= static_cast<int>(png_data
->size()))
119 return std::string();
121 std::vector
<std::string
> argv
;
122 argv
.push_back("xdg-icon-resource");
123 argv
.push_back("install");
125 // Always install in user mode, even if someone runs the browser as root
127 argv
.push_back("--mode");
128 argv
.push_back("user");
130 argv
.push_back("--size");
131 argv
.push_back(base::IntToString(width
));
133 argv
.push_back(temp_file_path
.value());
134 argv
.push_back(icon_name
);
136 if (!LaunchXdgUtility(argv
, &exit_code
) || exit_code
) {
137 LOG(WARNING
) << "Could not install icon " << icon_name
<< ".png at size "
144 bool CreateShortcutOnDesktop(const base::FilePath
& shortcut_filename
,
145 const std::string
& contents
) {
146 // Make sure that we will later call openat in a secure way.
147 DCHECK_EQ(shortcut_filename
.BaseName().value(), shortcut_filename
.value());
149 base::FilePath desktop_path
;
150 if (!PathService::Get(base::DIR_USER_DESKTOP
, &desktop_path
))
153 int desktop_fd
= open(desktop_path
.value().c_str(), O_RDONLY
| O_DIRECTORY
);
157 int fd
= openat(desktop_fd
, shortcut_filename
.value().c_str(),
158 O_CREAT
| O_EXCL
| O_WRONLY
,
159 S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
161 if (IGNORE_EINTR(close(desktop_fd
)) < 0)
162 PLOG(ERROR
) << "close";
166 if (!base::WriteFileDescriptor(fd
, contents
.c_str(), contents
.size())) {
167 // Delete the file. No shortuct is better than corrupted one. Use unlinkat
168 // to make sure we're deleting the file in the directory we think we are.
169 // Even if an attacker manager to put something other at
170 // |shortcut_filename| we'll just undo his action.
171 unlinkat(desktop_fd
, shortcut_filename
.value().c_str(), 0);
174 if (IGNORE_EINTR(close(fd
)) < 0)
175 PLOG(ERROR
) << "close";
177 if (IGNORE_EINTR(close(desktop_fd
)) < 0)
178 PLOG(ERROR
) << "close";
183 void DeleteShortcutOnDesktop(const base::FilePath
& shortcut_filename
) {
184 base::FilePath desktop_path
;
185 if (PathService::Get(base::DIR_USER_DESKTOP
, &desktop_path
))
186 base::DeleteFile(desktop_path
.Append(shortcut_filename
), false);
189 // Creates a shortcut with |shortcut_filename| and |contents| in the system
190 // applications menu. If |directory_filename| is non-empty, creates a sub-menu
191 // with |directory_filename| and |directory_contents|, and stores the shortcut
192 // under the sub-menu.
193 bool CreateShortcutInApplicationsMenu(const base::FilePath
& shortcut_filename
,
194 const std::string
& contents
,
195 const base::FilePath
& directory_filename
,
196 const std::string
& directory_contents
) {
197 base::ScopedTempDir temp_dir
;
198 if (!temp_dir
.CreateUniqueTempDir())
201 base::FilePath temp_directory_path
;
202 if (!directory_filename
.empty()) {
203 temp_directory_path
= temp_dir
.path().Append(directory_filename
);
205 int bytes_written
= base::WriteFile(temp_directory_path
,
206 directory_contents
.data(),
207 directory_contents
.length());
209 if (bytes_written
!= static_cast<int>(directory_contents
.length()))
213 base::FilePath temp_file_path
= temp_dir
.path().Append(shortcut_filename
);
215 int bytes_written
= base::WriteFile(temp_file_path
, contents
.data(),
218 if (bytes_written
!= static_cast<int>(contents
.length()))
221 std::vector
<std::string
> argv
;
222 argv
.push_back("xdg-desktop-menu");
223 argv
.push_back("install");
225 // Always install in user mode, even if someone runs the browser as root
227 argv
.push_back("--mode");
228 argv
.push_back("user");
230 // If provided, install the shortcut file inside the given directory.
231 if (!directory_filename
.empty())
232 argv
.push_back(temp_directory_path
.value());
233 argv
.push_back(temp_file_path
.value());
235 LaunchXdgUtility(argv
, &exit_code
);
236 return exit_code
== 0;
239 void DeleteShortcutInApplicationsMenu(
240 const base::FilePath
& shortcut_filename
,
241 const base::FilePath
& directory_filename
) {
242 std::vector
<std::string
> argv
;
243 argv
.push_back("xdg-desktop-menu");
244 argv
.push_back("uninstall");
246 // Uninstall in user mode, to match the install.
247 argv
.push_back("--mode");
248 argv
.push_back("user");
250 // The file does not need to exist anywhere - xdg-desktop-menu will uninstall
251 // items from the menu with a matching name.
252 // If |directory_filename| is supplied, this will also remove the item from
253 // the directory, and remove the directory if it is empty.
254 if (!directory_filename
.empty())
255 argv
.push_back(directory_filename
.value());
256 argv
.push_back(shortcut_filename
.value());
258 LaunchXdgUtility(argv
, &exit_code
);
261 #if defined(USE_GLIB)
262 // Quote a string such that it appears as one verbatim argument for the Exec
263 // key in a desktop file.
264 std::string
QuoteArgForDesktopFileExec(const std::string
& arg
) {
265 // http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
267 // Quoting is only necessary if the argument has a reserved character.
268 if (arg
.find_first_of(" \t\n\"'\\><~|&;$*?#()`") == std::string::npos
)
269 return arg
; // No quoting necessary.
271 std::string quoted
= "\"";
272 for (size_t i
= 0; i
< arg
.size(); ++i
) {
273 // Note that the set of backslashed characters is smaller than the
274 // set of reserved characters.
290 // Quote a command line so it is suitable for use as the Exec key in a desktop
291 // file. Note: This should be used instead of GetCommandLineString, which does
292 // not properly quote the string; this function is designed for the Exec key.
293 std::string
QuoteCommandLineForDesktopFileExec(
294 const CommandLine
& command_line
) {
295 // http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
297 std::string quoted_path
= "";
298 const CommandLine::StringVector
& argv
= command_line
.argv();
299 for (CommandLine::StringVector::const_iterator i
= argv
.begin();
300 i
!= argv
.end(); ++i
) {
301 if (i
!= argv
.begin())
303 quoted_path
+= QuoteArgForDesktopFileExec(*i
);
309 const char kDesktopEntry
[] = "Desktop Entry";
311 const char kXdgOpenShebang
[] = "#!/usr/bin/env xdg-open";
314 const char kXdgSettings
[] = "xdg-settings";
315 const char kXdgSettingsDefaultBrowser
[] = "default-web-browser";
316 const char kXdgSettingsDefaultSchemeHandler
[] = "default-url-scheme-handler";
318 const char kDirectoryFilename
[] = "chrome-apps.directory";
320 #if defined(GOOGLE_CHROME_BUILD)
321 const char kAppListDesktopName
[] = "chrome-app-list";
322 #else // CHROMIUM_BUILD
323 const char kAppListDesktopName
[] = "chromium-app-list";
326 // Utility function to get the path to the version of a script shipped with
327 // Chrome. |script| gives the name of the script. |chrome_version| returns the
328 // path to the Chrome version of the script, and the return value of the
329 // function is true if the function is successful and the Chrome version is
330 // not the script found on the PATH.
331 bool GetChromeVersionOfScript(const std::string
& script
,
332 std::string
* chrome_version
) {
333 // Get the path to the Chrome version.
334 base::FilePath chrome_dir
;
335 if (!PathService::Get(base::DIR_EXE
, &chrome_dir
))
338 base::FilePath chrome_version_path
= chrome_dir
.Append(script
);
339 *chrome_version
= chrome_version_path
.value();
341 // Check if this is different to the one on path.
342 std::vector
<std::string
> argv
;
343 argv
.push_back("which");
344 argv
.push_back(script
);
345 std::string path_version
;
346 if (base::GetAppOutput(CommandLine(argv
), &path_version
)) {
347 // Remove trailing newline
348 path_version
.erase(path_version
.length() - 1, 1);
349 base::FilePath
path_version_path(path_version
);
350 return (chrome_version_path
!= path_version_path
);
355 // Value returned by xdg-settings if it can't understand our request.
356 const int EXIT_XDG_SETTINGS_SYNTAX_ERROR
= 1;
358 // We delegate the difficulty of setting the default browser and default url
359 // scheme handler in Linux desktop environments to an xdg utility, xdg-settings.
361 // When calling this script we first try to use the script on PATH. If that
362 // fails we then try to use the script that we have included. This gives
363 // scripts on the system priority over ours, as distribution vendors may have
364 // tweaked the script, but still allows our copy to be used if the script on the
365 // system fails, as the system copy may be missing capabilities of the Chrome
368 // If |protocol| is empty this function sets Chrome as the default browser,
369 // otherwise it sets Chrome as the default handler application for |protocol|.
370 bool SetDefaultWebClient(const std::string
& protocol
) {
371 #if defined(OS_CHROMEOS)
374 scoped_ptr
<base::Environment
> env(base::Environment::Create());
376 std::vector
<std::string
> argv
;
377 argv
.push_back(kXdgSettings
);
378 argv
.push_back("set");
379 if (protocol
.empty()) {
380 argv
.push_back(kXdgSettingsDefaultBrowser
);
382 argv
.push_back(kXdgSettingsDefaultSchemeHandler
);
383 argv
.push_back(protocol
);
385 argv
.push_back(shell_integration_linux::GetDesktopName(env
.get()));
388 bool ran_ok
= LaunchXdgUtility(argv
, &exit_code
);
389 if (ran_ok
&& exit_code
== EXIT_XDG_SETTINGS_SYNTAX_ERROR
) {
390 if (GetChromeVersionOfScript(kXdgSettings
, &argv
[0])) {
391 ran_ok
= LaunchXdgUtility(argv
, &exit_code
);
395 return ran_ok
&& exit_code
== EXIT_SUCCESS
;
399 // If |protocol| is empty this function checks if Chrome is the default browser,
400 // otherwise it checks if Chrome is the default handler application for
402 ShellIntegration::DefaultWebClientState
GetIsDefaultWebClient(
403 const std::string
& protocol
) {
404 #if defined(OS_CHROMEOS)
405 return ShellIntegration::UNKNOWN_DEFAULT
;
407 base::ThreadRestrictions::AssertIOAllowed();
409 scoped_ptr
<base::Environment
> env(base::Environment::Create());
411 std::vector
<std::string
> argv
;
412 argv
.push_back(kXdgSettings
);
413 argv
.push_back("check");
414 if (protocol
.empty()) {
415 argv
.push_back(kXdgSettingsDefaultBrowser
);
417 argv
.push_back(kXdgSettingsDefaultSchemeHandler
);
418 argv
.push_back(protocol
);
420 argv
.push_back(shell_integration_linux::GetDesktopName(env
.get()));
424 bool ran_ok
= base::GetAppOutputWithExitCode(CommandLine(argv
), &reply
,
426 if (ran_ok
&& success_code
== EXIT_XDG_SETTINGS_SYNTAX_ERROR
) {
427 if (GetChromeVersionOfScript(kXdgSettings
, &argv
[0])) {
428 ran_ok
= base::GetAppOutputWithExitCode(CommandLine(argv
), &reply
,
433 if (!ran_ok
|| success_code
!= EXIT_SUCCESS
) {
434 // xdg-settings failed: we can't determine or set the default browser.
435 return ShellIntegration::UNKNOWN_DEFAULT
;
438 // Allow any reply that starts with "yes".
439 return (reply
.find("yes") == 0) ? ShellIntegration::IS_DEFAULT
:
440 ShellIntegration::NOT_DEFAULT
;
444 // Get the value of NoDisplay from the [Desktop Entry] section of a .desktop
445 // file, given in |shortcut_contents|. If the key is not found, returns false.
446 bool GetNoDisplayFromDesktopFile(const std::string
& shortcut_contents
) {
447 #if defined(USE_GLIB)
448 // An empty file causes a crash with glib <= 2.32, so special case here.
449 if (shortcut_contents
.empty())
452 GKeyFile
* key_file
= g_key_file_new();
454 if (!g_key_file_load_from_data(key_file
, shortcut_contents
.c_str(),
455 shortcut_contents
.size(), G_KEY_FILE_NONE
,
457 LOG(WARNING
) << "Unable to read desktop file template: " << err
->message
;
459 g_key_file_free(key_file
);
463 bool nodisplay
= false;
464 char* nodisplay_c_string
= g_key_file_get_string(key_file
, kDesktopEntry
,
466 if (nodisplay_c_string
) {
467 if (!g_strcmp0(nodisplay_c_string
, "true"))
469 g_free(nodisplay_c_string
);
474 g_key_file_free(key_file
);
482 // Gets the path to the Chrome executable or wrapper script.
483 // Returns an empty path if the executable path could not be found.
484 base::FilePath
GetChromeExePath() {
485 // Try to get the name of the wrapper script that launched Chrome.
486 scoped_ptr
<base::Environment
> environment(base::Environment::Create());
487 std::string wrapper_script
;
488 if (environment
->GetVar("CHROME_WRAPPER", &wrapper_script
)) {
489 return base::FilePath(wrapper_script
);
492 // Just return the name of the executable path for Chrome.
493 base::FilePath chrome_exe_path
;
494 PathService::Get(base::FILE_EXE
, &chrome_exe_path
);
495 return chrome_exe_path
;
501 ShellIntegration::DefaultWebClientSetPermission
502 ShellIntegration::CanSetAsDefaultBrowser() {
503 return SET_DEFAULT_UNATTENDED
;
507 bool ShellIntegration::SetAsDefaultBrowser() {
508 return SetDefaultWebClient(std::string());
512 bool ShellIntegration::SetAsDefaultProtocolClient(
513 const std::string
& protocol
) {
514 return SetDefaultWebClient(protocol
);
518 ShellIntegration::DefaultWebClientState
519 ShellIntegration::GetDefaultBrowser() {
520 return GetIsDefaultWebClient(std::string());
524 base::string16
ShellIntegration::GetApplicationNameForProtocol(
526 return base::ASCIIToUTF16("xdg-open");
530 ShellIntegration::DefaultWebClientState
531 ShellIntegration::IsDefaultProtocolClient(const std::string
& protocol
) {
532 return GetIsDefaultWebClient(protocol
);
536 bool ShellIntegration::IsFirefoxDefaultBrowser() {
537 std::vector
<std::string
> argv
;
538 argv
.push_back(kXdgSettings
);
539 argv
.push_back("get");
540 argv
.push_back(kXdgSettingsDefaultBrowser
);
543 // We don't care about the return value here.
544 base::GetAppOutput(CommandLine(argv
), &browser
);
545 return browser
.find("irefox") != std::string::npos
;
548 namespace shell_integration_linux
{
550 bool GetDataWriteLocation(base::Environment
* env
, base::FilePath
* search_path
) {
551 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
553 std::string xdg_data_home
;
555 if (env
->GetVar("XDG_DATA_HOME", &xdg_data_home
) && !xdg_data_home
.empty()) {
556 *search_path
= base::FilePath(xdg_data_home
);
558 } else if (env
->GetVar("HOME", &home
) && !home
.empty()) {
559 *search_path
= base::FilePath(home
).Append(".local").Append("share");
565 std::vector
<base::FilePath
> GetDataSearchLocations(base::Environment
* env
) {
566 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
568 std::vector
<base::FilePath
> search_paths
;
570 base::FilePath write_location
;
571 if (GetDataWriteLocation(env
, &write_location
))
572 search_paths
.push_back(write_location
);
574 std::string xdg_data_dirs
;
575 if (env
->GetVar("XDG_DATA_DIRS", &xdg_data_dirs
) && !xdg_data_dirs
.empty()) {
576 base::StringTokenizer
tokenizer(xdg_data_dirs
, ":");
577 while (tokenizer
.GetNext()) {
578 base::FilePath
data_dir(tokenizer
.token());
579 search_paths
.push_back(data_dir
);
582 search_paths
.push_back(base::FilePath("/usr/local/share"));
583 search_paths
.push_back(base::FilePath("/usr/share"));
589 std::string
GetProgramClassName() {
590 DCHECK(CommandLine::InitializedForCurrentProcess());
591 // Get the res_name component from argv[0].
592 const CommandLine
* command_line
= CommandLine::ForCurrentProcess();
593 std::string class_name
= command_line
->GetProgram().BaseName().value();
594 if (!class_name
.empty())
595 class_name
[0] = base::ToUpperASCII(class_name
[0]);
599 std::string
GetDesktopName(base::Environment
* env
) {
600 #if defined(GOOGLE_CHROME_BUILD)
601 chrome::VersionInfo::Channel
product_channel(
602 chrome::VersionInfo::GetChannel());
603 switch (product_channel
) {
604 case chrome::VersionInfo::CHANNEL_DEV
:
605 return "google-chrome-unstable.desktop";
606 case chrome::VersionInfo::CHANNEL_BETA
:
607 return "google-chrome-beta.desktop";
609 return "google-chrome.desktop";
611 #else // CHROMIUM_BUILD
612 // Allow $CHROME_DESKTOP to override the built-in value, so that development
613 // versions can set themselves as the default without interfering with
614 // non-official, packaged versions using the built-in value.
616 if (env
->GetVar("CHROME_DESKTOP", &name
) && !name
.empty())
618 return "chromium-browser.desktop";
622 std::string
GetIconName() {
623 #if defined(GOOGLE_CHROME_BUILD)
624 return "google-chrome";
625 #else // CHROMIUM_BUILD
626 return "chromium-browser";
630 web_app::ShortcutLocations
GetExistingShortcutLocations(
631 base::Environment
* env
,
632 const base::FilePath
& profile_path
,
633 const std::string
& extension_id
) {
634 base::FilePath desktop_path
;
635 // If Get returns false, just leave desktop_path empty.
636 PathService::Get(base::DIR_USER_DESKTOP
, &desktop_path
);
637 return GetExistingShortcutLocations(env
, profile_path
, extension_id
,
641 web_app::ShortcutLocations
GetExistingShortcutLocations(
642 base::Environment
* env
,
643 const base::FilePath
& profile_path
,
644 const std::string
& extension_id
,
645 const base::FilePath
& desktop_path
) {
646 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
648 base::FilePath shortcut_filename
= GetExtensionShortcutFilename(
649 profile_path
, extension_id
);
650 DCHECK(!shortcut_filename
.empty());
651 web_app::ShortcutLocations locations
;
653 // Determine whether there is a shortcut on desktop.
654 if (!desktop_path
.empty()) {
655 locations
.on_desktop
=
656 base::PathExists(desktop_path
.Append(shortcut_filename
));
659 // Determine whether there is a shortcut in the applications directory.
660 std::string shortcut_contents
;
661 if (GetExistingShortcutContents(env
, shortcut_filename
, &shortcut_contents
)) {
662 // If the shortcut contents contain NoDisplay=true, it should be hidden.
663 // Otherwise since these shortcuts are for apps, they are always in the
664 // "Chrome Apps" directory.
665 locations
.applications_menu_location
=
666 GetNoDisplayFromDesktopFile(shortcut_contents
)
667 ? web_app::APP_MENU_LOCATION_HIDDEN
668 : web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS
;
674 bool GetExistingShortcutContents(base::Environment
* env
,
675 const base::FilePath
& desktop_filename
,
676 std::string
* output
) {
677 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
679 std::vector
<base::FilePath
> search_paths
= GetDataSearchLocations(env
);
681 for (std::vector
<base::FilePath
>::const_iterator i
= search_paths
.begin();
682 i
!= search_paths
.end(); ++i
) {
683 base::FilePath path
= i
->Append("applications").Append(desktop_filename
);
684 VLOG(1) << "Looking for desktop file in " << path
.value();
685 if (base::PathExists(path
)) {
686 VLOG(1) << "Found desktop file at " << path
.value();
687 return base::ReadFileToString(path
, output
);
694 base::FilePath
GetWebShortcutFilename(const GURL
& url
) {
695 // Use a prefix, because xdg-desktop-menu requires it.
696 std::string filename
=
697 std::string(chrome::kBrowserProcessExecutableName
) + "-" + url
.spec();
698 base::i18n::ReplaceIllegalCharactersInPath(&filename
, '_');
700 base::FilePath desktop_path
;
701 if (!PathService::Get(base::DIR_USER_DESKTOP
, &desktop_path
))
702 return base::FilePath();
704 base::FilePath filepath
= desktop_path
.Append(filename
);
705 base::FilePath
alternative_filepath(filepath
.value() + ".desktop");
706 for (size_t i
= 1; i
< 100; ++i
) {
707 if (base::PathExists(base::FilePath(alternative_filepath
))) {
708 alternative_filepath
= base::FilePath(
709 filepath
.value() + "_" + base::IntToString(i
) + ".desktop");
711 return base::FilePath(alternative_filepath
).BaseName();
715 return base::FilePath();
718 base::FilePath
GetExtensionShortcutFilename(const base::FilePath
& profile_path
,
719 const std::string
& extension_id
) {
720 DCHECK(!extension_id
.empty());
722 // Use a prefix, because xdg-desktop-menu requires it.
723 std::string
filename(chrome::kBrowserProcessExecutableName
);
725 .append(extension_id
)
727 .append(profile_path
.BaseName().value());
728 base::i18n::ReplaceIllegalCharactersInPath(&filename
, '_');
729 // Spaces in filenames break xdg-desktop-menu
730 // (see https://bugs.freedesktop.org/show_bug.cgi?id=66605).
731 base::ReplaceChars(filename
, " ", "_", &filename
);
732 return base::FilePath(filename
.append(".desktop"));
735 std::vector
<base::FilePath
> GetExistingProfileShortcutFilenames(
736 const base::FilePath
& profile_path
,
737 const base::FilePath
& directory
) {
738 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
739 // Use a prefix, because xdg-desktop-menu requires it.
740 std::string
prefix(chrome::kBrowserProcessExecutableName
);
742 std::string
suffix("-");
743 suffix
.append(profile_path
.BaseName().value());
744 base::i18n::ReplaceIllegalCharactersInPath(&suffix
, '_');
745 // Spaces in filenames break xdg-desktop-menu
746 // (see https://bugs.freedesktop.org/show_bug.cgi?id=66605).
747 base::ReplaceChars(suffix
, " ", "_", &suffix
);
748 std::string glob
= prefix
+ "*" + suffix
+ ".desktop";
750 base::FileEnumerator
files(directory
, false, base::FileEnumerator::FILES
,
752 base::FilePath shortcut_file
= files
.Next();
753 std::vector
<base::FilePath
> shortcut_paths
;
754 while (!shortcut_file
.empty()) {
755 shortcut_paths
.push_back(shortcut_file
.BaseName());
756 shortcut_file
= files
.Next();
758 return shortcut_paths
;
761 std::string
GetDesktopFileContents(
762 const base::FilePath
& chrome_exe_path
,
763 const std::string
& app_name
,
765 const std::string
& extension_id
,
766 const base::string16
& title
,
767 const std::string
& icon_name
,
768 const base::FilePath
& profile_path
,
769 const std::string
& categories
,
771 CommandLine cmd_line
= ShellIntegration::CommandLineArgsForLauncher(
772 url
, extension_id
, profile_path
);
773 cmd_line
.SetProgram(chrome_exe_path
);
774 return GetDesktopFileContentsForCommand(cmd_line
, app_name
, url
, title
,
775 icon_name
, categories
, no_display
);
778 std::string
GetDesktopFileContentsForCommand(
779 const CommandLine
& command_line
,
780 const std::string
& app_name
,
782 const base::string16
& title
,
783 const std::string
& icon_name
,
784 const std::string
& categories
,
786 #if defined(USE_GLIB)
787 // Although not required by the spec, Nautilus on Ubuntu Karmic creates its
788 // launchers with an xdg-open shebang. Follow that convention.
789 std::string output_buffer
= std::string(kXdgOpenShebang
) + "\n";
791 // See http://standards.freedesktop.org/desktop-entry-spec/latest/
792 GKeyFile
* key_file
= g_key_file_new();
794 // Set keys with fixed values.
795 g_key_file_set_string(key_file
, kDesktopEntry
, "Version", "1.0");
796 g_key_file_set_string(key_file
, kDesktopEntry
, "Terminal", "false");
797 g_key_file_set_string(key_file
, kDesktopEntry
, "Type", "Application");
799 // Set the "Name" key.
800 std::string final_title
= base::UTF16ToUTF8(title
);
801 // Make sure no endline characters can slip in and possibly introduce
802 // additional lines (like Exec, which makes it a security risk). Also
803 // use the URL as a default when the title is empty.
804 if (final_title
.empty() ||
805 final_title
.find("\n") != std::string::npos
||
806 final_title
.find("\r") != std::string::npos
) {
807 final_title
= url
.spec();
809 g_key_file_set_string(key_file
, kDesktopEntry
, "Name", final_title
.c_str());
811 // Set the "Exec" key.
812 std::string final_path
= QuoteCommandLineForDesktopFileExec(command_line
);
813 g_key_file_set_string(key_file
, kDesktopEntry
, "Exec", final_path
.c_str());
815 // Set the "Icon" key.
816 if (!icon_name
.empty()) {
817 g_key_file_set_string(key_file
, kDesktopEntry
, "Icon", icon_name
.c_str());
819 g_key_file_set_string(key_file
, kDesktopEntry
, "Icon",
820 GetIconName().c_str());
823 // Set the "Categories" key.
824 if (!categories
.empty()) {
825 g_key_file_set_string(
826 key_file
, kDesktopEntry
, "Categories", categories
.c_str());
829 // Set the "NoDisplay" key.
831 g_key_file_set_string(key_file
, kDesktopEntry
, "NoDisplay", "true");
833 std::string wmclass
= web_app::GetWMClassFromAppName(app_name
);
834 g_key_file_set_string(key_file
, kDesktopEntry
, "StartupWMClass",
838 gchar
* data_dump
= g_key_file_to_data(key_file
, &length
, NULL
);
840 // If strlen(data_dump[0]) == 0, this check will fail.
841 if (data_dump
[0] == '\n') {
842 // Older versions of glib produce a leading newline. If this is the case,
843 // remove it to avoid double-newline after the shebang.
844 output_buffer
+= (data_dump
+ 1);
846 output_buffer
+= data_dump
;
851 g_key_file_free(key_file
);
852 return output_buffer
;
855 return std::string();
859 std::string
GetDirectoryFileContents(const base::string16
& title
,
860 const std::string
& icon_name
) {
861 #if defined(USE_GLIB)
862 // See http://standards.freedesktop.org/desktop-entry-spec/latest/
863 GKeyFile
* key_file
= g_key_file_new();
865 g_key_file_set_string(key_file
, kDesktopEntry
, "Version", "1.0");
866 g_key_file_set_string(key_file
, kDesktopEntry
, "Type", "Directory");
867 std::string final_title
= base::UTF16ToUTF8(title
);
868 g_key_file_set_string(key_file
, kDesktopEntry
, "Name", final_title
.c_str());
869 if (!icon_name
.empty()) {
870 g_key_file_set_string(key_file
, kDesktopEntry
, "Icon", icon_name
.c_str());
872 g_key_file_set_string(key_file
, kDesktopEntry
, "Icon",
873 GetIconName().c_str());
877 gchar
* data_dump
= g_key_file_to_data(key_file
, &length
, NULL
);
878 std::string output_buffer
;
880 // If strlen(data_dump[0]) == 0, this check will fail.
881 if (data_dump
[0] == '\n') {
882 // Older versions of glib produce a leading newline. If this is the case,
883 // remove it to avoid double-newline after the shebang.
884 output_buffer
+= (data_dump
+ 1);
886 output_buffer
+= data_dump
;
891 g_key_file_free(key_file
);
892 return output_buffer
;
895 return std::string();
899 bool CreateDesktopShortcut(
900 const web_app::ShortcutInfo
& shortcut_info
,
901 const web_app::ShortcutLocations
& creation_locations
) {
902 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
904 base::FilePath shortcut_filename
;
905 if (!shortcut_info
.extension_id
.empty()) {
906 shortcut_filename
= GetExtensionShortcutFilename(
907 shortcut_info
.profile_path
, shortcut_info
.extension_id
);
908 // For extensions we do not want duplicate shortcuts. So, delete any that
909 // already exist and replace them.
910 if (creation_locations
.on_desktop
)
911 DeleteShortcutOnDesktop(shortcut_filename
);
913 if (creation_locations
.applications_menu_location
!=
914 web_app::APP_MENU_LOCATION_NONE
) {
915 DeleteShortcutInApplicationsMenu(shortcut_filename
, base::FilePath());
918 shortcut_filename
= GetWebShortcutFilename(shortcut_info
.url
);
920 if (shortcut_filename
.empty())
923 std::string icon_name
=
924 CreateShortcutIcon(shortcut_info
.favicon
, shortcut_filename
);
926 std::string app_name
=
927 web_app::GenerateApplicationNameFromInfo(shortcut_info
);
931 base::FilePath chrome_exe_path
= GetChromeExePath();
932 if (chrome_exe_path
.empty()) {
933 LOG(WARNING
) << "Could not get executable path.";
937 if (creation_locations
.on_desktop
) {
938 std::string contents
= GetDesktopFileContents(
942 shortcut_info
.extension_id
,
945 shortcut_info
.profile_path
,
948 success
= CreateShortcutOnDesktop(shortcut_filename
, contents
);
951 if (creation_locations
.applications_menu_location
==
952 web_app::APP_MENU_LOCATION_NONE
) {
956 base::FilePath directory_filename
;
957 std::string directory_contents
;
958 switch (creation_locations
.applications_menu_location
) {
959 case web_app::APP_MENU_LOCATION_ROOT
:
960 case web_app::APP_MENU_LOCATION_HIDDEN
:
962 case web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS
:
963 directory_filename
= base::FilePath(kDirectoryFilename
);
964 directory_contents
= GetDirectoryFileContents(
965 ShellIntegration::GetAppShortcutsSubdirName(), "");
972 // Set NoDisplay=true if hidden. This will hide the application from
973 // user-facing menus.
974 std::string contents
= GetDesktopFileContents(
978 shortcut_info
.extension_id
,
981 shortcut_info
.profile_path
,
983 creation_locations
.applications_menu_location
==
984 web_app::APP_MENU_LOCATION_HIDDEN
);
985 success
= CreateShortcutInApplicationsMenu(
986 shortcut_filename
, contents
, directory_filename
, directory_contents
) &&
992 bool CreateAppListDesktopShortcut(
993 const std::string
& wm_class
,
994 const std::string
& title
) {
995 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
997 base::FilePath
desktop_name(kAppListDesktopName
);
998 base::FilePath shortcut_filename
= desktop_name
.AddExtension("desktop");
1000 // We do not want duplicate shortcuts. Delete any that already exist and
1002 DeleteShortcutInApplicationsMenu(shortcut_filename
, base::FilePath());
1004 base::FilePath chrome_exe_path
= GetChromeExePath();
1005 if (chrome_exe_path
.empty()) {
1006 LOG(WARNING
) << "Could not get executable path.";
1010 gfx::ImageFamily icon_images
;
1011 ResourceBundle
& resource_bundle
= ResourceBundle::GetSharedInstance();
1012 icon_images
.Add(*resource_bundle
.GetImageSkiaNamed(IDR_APP_LIST_16
));
1013 icon_images
.Add(*resource_bundle
.GetImageSkiaNamed(IDR_APP_LIST_32
));
1014 icon_images
.Add(*resource_bundle
.GetImageSkiaNamed(IDR_APP_LIST_48
));
1015 icon_images
.Add(*resource_bundle
.GetImageSkiaNamed(IDR_APP_LIST_256
));
1016 std::string icon_name
= CreateShortcutIcon(icon_images
, desktop_name
);
1018 CommandLine
command_line(chrome_exe_path
);
1019 command_line
.AppendSwitch(switches::kShowAppList
);
1020 std::string contents
=
1021 GetDesktopFileContentsForCommand(command_line
,
1024 base::UTF8ToUTF16(title
),
1028 return CreateShortcutInApplicationsMenu(
1029 shortcut_filename
, contents
, base::FilePath(), "");
1032 void DeleteDesktopShortcuts(const base::FilePath
& profile_path
,
1033 const std::string
& extension_id
) {
1034 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
1036 base::FilePath shortcut_filename
= GetExtensionShortcutFilename(
1037 profile_path
, extension_id
);
1038 DCHECK(!shortcut_filename
.empty());
1040 DeleteShortcutOnDesktop(shortcut_filename
);
1041 // Delete shortcuts from |kDirectoryFilename|.
1042 // Note that it is possible that shortcuts were not created in the Chrome Apps
1043 // directory. It doesn't matter: this will still delete the shortcut even if
1044 // it isn't in the directory.
1045 DeleteShortcutInApplicationsMenu(shortcut_filename
,
1046 base::FilePath(kDirectoryFilename
));
1049 void DeleteAllDesktopShortcuts(const base::FilePath
& profile_path
) {
1050 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
1052 scoped_ptr
<base::Environment
> env(base::Environment::Create());
1054 // Delete shortcuts from Desktop.
1055 base::FilePath desktop_path
;
1056 if (PathService::Get(base::DIR_USER_DESKTOP
, &desktop_path
)) {
1057 std::vector
<base::FilePath
> shortcut_filenames_desktop
=
1058 GetExistingProfileShortcutFilenames(profile_path
, desktop_path
);
1059 for (std::vector
<base::FilePath
>::const_iterator it
=
1060 shortcut_filenames_desktop
.begin();
1061 it
!= shortcut_filenames_desktop
.end(); ++it
) {
1062 DeleteShortcutOnDesktop(*it
);
1066 // Delete shortcuts from |kDirectoryFilename|.
1067 base::FilePath applications_menu
;
1068 if (GetDataWriteLocation(env
.get(), &applications_menu
)) {
1069 applications_menu
= applications_menu
.AppendASCII("applications");
1070 std::vector
<base::FilePath
> shortcut_filenames_app_menu
=
1071 GetExistingProfileShortcutFilenames(profile_path
, applications_menu
);
1072 for (std::vector
<base::FilePath
>::const_iterator it
=
1073 shortcut_filenames_app_menu
.begin();
1074 it
!= shortcut_filenames_app_menu
.end(); ++it
) {
1075 DeleteShortcutInApplicationsMenu(*it
,
1076 base::FilePath(kDirectoryFilename
));
1081 } // namespace shell_integration_linux