15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/shell_integration_linux.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <fcntl.h>
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#if defined(USE_GLIB)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <glib.h>
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdlib.h>
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/stat.h>
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/types.h>
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <unistd.h>
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_paths.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/environment.h"
242385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "base/files/file_enumerator.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/scoped_temp_dir.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/i18n/file_util_icu.h"
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/memory/ref_counted_memory.h"
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
319ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/posix/eintr_wrapper.h"
34bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch#include "base/process/kill.h"
35bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch#include "base/process/launch.h"
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_tokenizer.h"
387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/strings/string_util.h"
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread.h"
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/threading/thread_restrictions.h"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h"
43a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "chrome/browser/shell_integration.h"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_constants.h"
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
46d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "chrome/common/chrome_version_info.h"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "grit/chrome_unscaled_resources.h"
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/image/image_family.h"
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// The Categories for the App Launcher desktop shortcut. Should be the same as
585c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// the Chrome desktop shortcut, so they are in the same sub-menu.
595c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuconst char kAppListCategories[] = "Network;WebBrowser;";
605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Helper to launch xdg scripts. We don't want them to ask any questions on the
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// terminal etc. The function returns true if the utility launches and exits
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// cleanly, in which case |exit_code| returns the utility's exit code.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool LaunchXdgUtility(const std::vector<std::string>& argv, int* exit_code) {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // xdg-settings internally runs xdg-mime, which uses mv to move newly-created
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // files on top of originals after making changes to them. In the event that
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the original files are owned by another user (e.g. root, which can happen
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if they are updated within sudo), mv will prompt the user to confirm if
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // standard input is a terminal (otherwise it just does it). So make sure it's
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not, to avoid locking everything up waiting for mv.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *exit_code = EXIT_FAILURE;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int devnull = open("/dev/null", O_RDONLY);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (devnull < 0)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::FileHandleMappingVector no_stdin;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  no_stdin.push_back(std::make_pair(devnull, STDIN_FILENO));
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::ProcessHandle handle;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::LaunchOptions options;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  options.fds_to_remap = &no_stdin;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!base::LaunchProcess(argv, options, &handle)) {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    close(devnull);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  close(devnull);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base::WaitForExitCode(handle, exit_code);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::string CreateShortcutIcon(const gfx::ImageFamily& icon_images,
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               const base::FilePath& shortcut_filename) {
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (icon_images.empty())
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return std::string();
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(phajdan.jr): Report errors from this function, possibly as infobars.
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::ScopedTempDir temp_dir;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!temp_dir.CreateUniqueTempDir())
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return std::string();
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath temp_file_path = temp_dir.path().Append(
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      shortcut_filename.ReplaceExtension("png"));
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string icon_name = temp_file_path.BaseName().RemoveExtension().value();
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (gfx::ImageFamily::const_iterator it = icon_images.begin();
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       it != icon_images.end(); ++it) {
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    int width = it->Width();
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    scoped_refptr<base::RefCountedMemory> png_data = it->As1xPNGBytes();
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (png_data->size() == 0) {
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // If the bitmap could not be encoded to PNG format, skip it.
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      LOG(WARNING) << "Could not encode icon " << icon_name << ".png at size "
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   << width << ".";
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    int bytes_written = base::WriteFile(temp_file_path,
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                        png_data->front_as<char>(),
116a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                        png_data->size());
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (bytes_written != static_cast<int>(png_data->size()))
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return std::string();
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<std::string> argv;
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    argv.push_back("xdg-icon-resource");
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    argv.push_back("install");
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Always install in user mode, even if someone runs the browser as root
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // (people do that).
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    argv.push_back("--mode");
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    argv.push_back("user");
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    argv.push_back("--size");
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    argv.push_back(base::IntToString(width));
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    argv.push_back(temp_file_path.value());
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    argv.push_back(icon_name);
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int exit_code;
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!LaunchXdgUtility(argv, &exit_code) || exit_code) {
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      LOG(WARNING) << "Could not install icon " << icon_name << ".png at size "
138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   << width << ".";
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return icon_name;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool CreateShortcutOnDesktop(const base::FilePath& shortcut_filename,
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const std::string& contents) {
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make sure that we will later call openat in a secure way.
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK_EQ(shortcut_filename.BaseName().value(), shortcut_filename.value());
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath desktop_path;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!PathService::Get(base::DIR_USER_DESKTOP, &desktop_path))
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int desktop_fd = open(desktop_path.value().c_str(), O_RDONLY | O_DIRECTORY);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (desktop_fd < 0)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int fd = openat(desktop_fd, shortcut_filename.value().c_str(),
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  O_CREAT | O_EXCL | O_WRONLY,
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (fd < 0) {
161a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (IGNORE_EINTR(close(desktop_fd)) < 0)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PLOG(ERROR) << "close";
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ssize_t bytes_written = base::WriteFileDescriptor(fd, contents.data(),
167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                    contents.length());
168a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (IGNORE_EINTR(close(fd)) < 0)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PLOG(ERROR) << "close";
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (bytes_written != static_cast<ssize_t>(contents.length())) {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Delete the file. No shortuct is better than corrupted one. Use unlinkat
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // to make sure we're deleting the file in the directory we think we are.
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Even if an attacker manager to put something other at
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // |shortcut_filename| we'll just undo his action.
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unlinkat(desktop_fd, shortcut_filename.value().c_str(), 0);
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
179a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (IGNORE_EINTR(close(desktop_fd)) < 0)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PLOG(ERROR) << "close";
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DeleteShortcutOnDesktop(const base::FilePath& shortcut_filename) {
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath desktop_path;
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (PathService::Get(base::DIR_USER_DESKTOP, &desktop_path))
1887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    base::DeleteFile(desktop_path.Append(shortcut_filename), false);
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Creates a shortcut with |shortcut_filename| and |contents| in the system
192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// applications menu. If |directory_filename| is non-empty, creates a sub-menu
193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// with |directory_filename| and |directory_contents|, and stores the shortcut
194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// under the sub-menu.
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool CreateShortcutInApplicationsMenu(const base::FilePath& shortcut_filename,
196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                      const std::string& contents,
197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                      const base::FilePath& directory_filename,
198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                      const std::string& directory_contents) {
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::ScopedTempDir temp_dir;
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!temp_dir.CreateUniqueTempDir())
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath temp_directory_path;
204c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!directory_filename.empty()) {
205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    temp_directory_path = temp_dir.path().Append(directory_filename);
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    int bytes_written = base::WriteFile(temp_directory_path,
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                        directory_contents.data(),
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                        directory_contents.length());
210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (bytes_written != static_cast<int>(directory_contents.length()))
212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return false;
213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath temp_file_path = temp_dir.path().Append(shortcut_filename);
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
217a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int bytes_written = base::WriteFile(temp_file_path, contents.data(),
218a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                      contents.length());
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (bytes_written != static_cast<int>(contents.length()))
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> argv;
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("xdg-desktop-menu");
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("install");
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Always install in user mode, even if someone runs the browser as root
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (people do that).
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("--mode");
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("user");
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If provided, install the shortcut file inside the given directory.
233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!directory_filename.empty())
234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    argv.push_back(temp_directory_path.value());
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back(temp_file_path.value());
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int exit_code;
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LaunchXdgUtility(argv, &exit_code);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return exit_code == 0;
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void DeleteShortcutInApplicationsMenu(
242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::FilePath& shortcut_filename,
243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::FilePath& directory_filename) {
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> argv;
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("xdg-desktop-menu");
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("uninstall");
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Uninstall in user mode, to match the install.
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("--mode");
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("user");
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The file does not need to exist anywhere - xdg-desktop-menu will uninstall
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // items from the menu with a matching name.
254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If |directory_filename| is supplied, this will also remove the item from
255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the directory, and remove the directory if it is empty.
256c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!directory_filename.empty())
257c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    argv.push_back(directory_filename.value());
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back(shortcut_filename.value());
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int exit_code;
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LaunchXdgUtility(argv, &exit_code);
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(USE_GLIB)
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Quote a string such that it appears as one verbatim argument for the Exec
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// key in a desktop file.
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string QuoteArgForDesktopFileExec(const std::string& arg) {
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Quoting is only necessary if the argument has a reserved character.
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (arg.find_first_of(" \t\n\"'\\><~|&;$*?#()`") == std::string::npos)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return arg;  // No quoting necessary.
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string quoted = "\"";
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < arg.size(); ++i) {
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Note that the set of backslashed characters is smaller than the
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // set of reserved characters.
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    switch (arg[i]) {
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case '"':
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case '`':
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case '$':
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case '\\':
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        quoted += '\\';
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    quoted += arg[i];
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  quoted += '"';
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return quoted;
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Quote a command line so it is suitable for use as the Exec key in a desktop
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// file. Note: This should be used instead of GetCommandLineString, which does
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// not properly quote the string; this function is designed for the Exec key.
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::string QuoteCommandLineForDesktopFileExec(
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const CommandLine& command_line) {
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string quoted_path = "";
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const CommandLine::StringVector& argv = command_line.argv();
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (CommandLine::StringVector::const_iterator i = argv.begin();
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       i != argv.end(); ++i) {
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (i != argv.begin())
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      quoted_path += " ";
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    quoted_path += QuoteArgForDesktopFileExec(*i);
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return quoted_path;
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kDesktopEntry[] = "Desktop Entry";
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kXdgOpenShebang[] = "#!/usr/bin/env xdg-open";
3141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kXdgSettings[] = "xdg-settings";
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kXdgSettingsDefaultBrowser[] = "default-web-browser";
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kXdgSettingsDefaultSchemeHandler[] = "default-url-scheme-handler";
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
320c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kDirectoryFilename[] = "chrome-apps.directory";
3212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(GOOGLE_CHROME_BUILD)
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kAppListDesktopName[] = "chrome-app-list";
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#else  // CHROMIUM_BUILD
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kAppListDesktopName[] = "chromium-app-list";
3265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Utility function to get the path to the version of a script shipped with
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Chrome. |script| gives the name of the script. |chrome_version| returns the
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// path to the Chrome version of the script, and the return value of the
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// function is true if the function is successful and the Chrome version is
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not the script found on the PATH.
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool GetChromeVersionOfScript(const std::string& script,
334cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              std::string* chrome_version) {
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the path to the Chrome version.
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath chrome_dir;
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!PathService::Get(base::DIR_EXE, &chrome_dir))
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath chrome_version_path = chrome_dir.Append(script);
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *chrome_version = chrome_version_path.value();
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check if this is different to the one on path.
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> argv;
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("which");
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back(script);
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string path_version;
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (base::GetAppOutput(CommandLine(argv), &path_version)) {
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Remove trailing newline
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    path_version.erase(path_version.length() - 1, 1);
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath path_version_path(path_version);
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return (chrome_version_path != path_version_path);
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Value returned by xdg-settings if it can't understand our request.
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int EXIT_XDG_SETTINGS_SYNTAX_ERROR = 1;
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We delegate the difficulty of setting the default browser and default url
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// scheme handler in Linux desktop environments to an xdg utility, xdg-settings.
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// When calling this script we first try to use the script on PATH. If that
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// fails we then try to use the script that we have included. This gives
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// scripts on the system priority over ours, as distribution vendors may have
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// tweaked the script, but still allows our copy to be used if the script on the
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// system fails, as the system copy may be missing capabilities of the Chrome
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// copy.
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If |protocol| is empty this function sets Chrome as the default browser,
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// otherwise it sets Chrome as the default handler application for |protocol|.
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool SetDefaultWebClient(const std::string& protocol) {
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::Environment> env(base::Environment::Create());
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> argv;
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back(kXdgSettings);
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("set");
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (protocol.empty()) {
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    argv.push_back(kXdgSettingsDefaultBrowser);
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    argv.push_back(kXdgSettingsDefaultSchemeHandler);
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    argv.push_back(protocol);
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
387cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  argv.push_back(shell_integration_linux::GetDesktopName(env.get()));
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int exit_code;
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ran_ok = LaunchXdgUtility(argv, &exit_code);
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (ran_ok && exit_code == EXIT_XDG_SETTINGS_SYNTAX_ERROR) {
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (GetChromeVersionOfScript(kXdgSettings, &argv[0])) {
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ran_ok = LaunchXdgUtility(argv, &exit_code);
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ran_ok && exit_code == EXIT_SUCCESS;
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If |protocol| is empty this function checks if Chrome is the default browser,
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// otherwise it checks if Chrome is the default handler application for
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |protocol|.
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::DefaultWebClientState GetIsDefaultWebClient(
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& protocol) {
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
40768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  return ShellIntegration::UNKNOWN_DEFAULT;
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::ThreadRestrictions::AssertIOAllowed();
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::Environment> env(base::Environment::Create());
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> argv;
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back(kXdgSettings);
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("check");
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (protocol.empty()) {
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    argv.push_back(kXdgSettingsDefaultBrowser);
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    argv.push_back(kXdgSettingsDefaultSchemeHandler);
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    argv.push_back(protocol);
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
422cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  argv.push_back(shell_integration_linux::GetDesktopName(env.get()));
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string reply;
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int success_code;
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ran_ok = base::GetAppOutputWithExitCode(CommandLine(argv), &reply,
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                               &success_code);
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (ran_ok && success_code == EXIT_XDG_SETTINGS_SYNTAX_ERROR) {
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (GetChromeVersionOfScript(kXdgSettings, &argv[0])) {
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ran_ok = base::GetAppOutputWithExitCode(CommandLine(argv), &reply,
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                              &success_code);
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!ran_ok || success_code != EXIT_SUCCESS) {
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // xdg-settings failed: we can't determine or set the default browser.
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return ShellIntegration::UNKNOWN_DEFAULT;
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Allow any reply that starts with "yes".
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return (reply.find("yes") == 0) ? ShellIntegration::IS_DEFAULT :
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                    ShellIntegration::NOT_DEFAULT;
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Get the value of NoDisplay from the [Desktop Entry] section of a .desktop
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// file, given in |shortcut_contents|. If the key is not found, returns false.
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool GetNoDisplayFromDesktopFile(const std::string& shortcut_contents) {
449cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#if defined(USE_GLIB)
4502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // An empty file causes a crash with glib <= 2.32, so special case here.
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (shortcut_contents.empty())
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GKeyFile* key_file = g_key_file_new();
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GError* err = NULL;
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!g_key_file_load_from_data(key_file, shortcut_contents.c_str(),
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 shortcut_contents.size(), G_KEY_FILE_NONE,
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 &err)) {
4592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(WARNING) << "Unable to read desktop file template: " << err->message;
4602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    g_error_free(err);
4612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    g_key_file_free(key_file);
4622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
4632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool nodisplay = false;
4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  char* nodisplay_c_string = g_key_file_get_string(key_file, kDesktopEntry,
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                   "NoDisplay", &err);
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (nodisplay_c_string) {
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!g_strcmp0(nodisplay_c_string, "true"))
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      nodisplay = true;
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    g_free(nodisplay_c_string);
4720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  } else {
4730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    g_error_free(err);
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  g_key_file_free(key_file);
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return nodisplay;
478cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#else
479cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NOTIMPLEMENTED();
480cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return false;
481cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
484eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Gets the path to the Chrome executable or wrapper script.
485eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns an empty path if the executable path could not be found.
486eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbase::FilePath GetChromeExePath() {
487eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Try to get the name of the wrapper script that launched Chrome.
488eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  scoped_ptr<base::Environment> environment(base::Environment::Create());
489eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  std::string wrapper_script;
490eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (environment->GetVar("CHROME_WRAPPER", &wrapper_script)) {
491eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return base::FilePath(wrapper_script);
492eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
493eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
494eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Just return the name of the executable path for Chrome.
495eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  base::FilePath chrome_exe_path;
496eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  PathService::Get(base::FILE_EXE, &chrome_exe_path);
497eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return chrome_exe_path;
498eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
499eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
500cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::DefaultWebClientSetPermission
504cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)ShellIntegration::CanSetAsDefaultBrowser() {
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return SET_DEFAULT_UNATTENDED;
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ShellIntegration::SetAsDefaultBrowser() {
510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return SetDefaultWebClient(std::string());
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
514cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool ShellIntegration::SetAsDefaultProtocolClient(
515cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const std::string& protocol) {
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return SetDefaultWebClient(protocol);
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
520cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)ShellIntegration::DefaultWebClientState
521cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)ShellIntegration::GetDefaultBrowser() {
522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return GetIsDefaultWebClient(std::string());
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
526a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)base::string16 ShellIntegration::GetApplicationNameForProtocol(
527a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const GURL& url) {
5285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return base::ASCIIToUTF16("xdg-open");
5292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::DefaultWebClientState
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) {
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetIsDefaultWebClient(protocol);
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ShellIntegration::IsFirefoxDefaultBrowser() {
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> argv;
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back(kXdgSettings);
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back("get");
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  argv.push_back(kXdgSettingsDefaultBrowser);
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string browser;
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We don't care about the return value here.
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::GetAppOutput(CommandLine(argv), &browser);
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return browser.find("irefox") != std::string::npos;
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
550cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace shell_integration_linux {
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5522385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochbool GetDataWriteLocation(base::Environment* env, base::FilePath* search_path) {
5532385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
5542385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
5552385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  std::string xdg_data_home;
5562385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  std::string home;
5572385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  if (env->GetVar("XDG_DATA_HOME", &xdg_data_home) && !xdg_data_home.empty()) {
5582385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    *search_path = base::FilePath(xdg_data_home);
5592385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    return true;
5602385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  } else if (env->GetVar("HOME", &home) && !home.empty()) {
5612385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    *search_path = base::FilePath(home).Append(".local").Append("share");
5622385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    return true;
5632385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  }
5642385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  return false;
5652385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch}
5662385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
5672385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochstd::vector<base::FilePath> GetDataSearchLocations(base::Environment* env) {
5682385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
5692385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
5702385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  std::vector<base::FilePath> search_paths;
5712385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
5722385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  base::FilePath write_location;
5732385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  if (GetDataWriteLocation(env, &write_location))
5742385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    search_paths.push_back(write_location);
5752385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
5762385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  std::string xdg_data_dirs;
5772385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  if (env->GetVar("XDG_DATA_DIRS", &xdg_data_dirs) && !xdg_data_dirs.empty()) {
5782385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    base::StringTokenizer tokenizer(xdg_data_dirs, ":");
5792385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    while (tokenizer.GetNext()) {
5802385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      base::FilePath data_dir(tokenizer.token());
5812385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      search_paths.push_back(data_dir);
5822385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    }
5832385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  } else {
5842385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    search_paths.push_back(base::FilePath("/usr/local/share"));
5852385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    search_paths.push_back(base::FilePath("/usr/share"));
5862385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  }
5872385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
5882385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  return search_paths;
5892385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch}
5902385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
59158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)std::string GetProgramClassName() {
59258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK(CommandLine::InitializedForCurrentProcess());
59358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Get the res_name component from argv[0].
59458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const CommandLine* command_line = CommandLine::ForCurrentProcess();
59558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::string class_name = command_line->GetProgram().BaseName().value();
59658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (!class_name.empty())
59758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    class_name[0] = base::ToUpperASCII(class_name[0]);
59858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return class_name;
59958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
60058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string GetDesktopName(base::Environment* env) {
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(GOOGLE_CHROME_BUILD)
603a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  chrome::VersionInfo::Channel product_channel(
604a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      chrome::VersionInfo::GetChannel());
605a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  switch (product_channel) {
606a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    case chrome::VersionInfo::CHANNEL_DEV:
607a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return "google-chrome-unstable.desktop";
608a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    case chrome::VersionInfo::CHANNEL_BETA:
609a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return "google-chrome-beta.desktop";
610a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    default:
611a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return "google-chrome.desktop";
612a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else  // CHROMIUM_BUILD
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Allow $CHROME_DESKTOP to override the built-in value, so that development
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // versions can set themselves as the default without interfering with
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // non-official, packaged versions using the built-in value.
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string name;
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (env->GetVar("CHROME_DESKTOP", &name) && !name.empty())
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return name;
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return "chromium-browser.desktop";
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
624c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)std::string GetIconName() {
625c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(GOOGLE_CHROME_BUILD)
626c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return "google-chrome";
627c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#else  // CHROMIUM_BUILD
628c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return "chromium-browser";
629c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
630c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
631c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
632a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochweb_app::ShortcutLocations GetExistingShortcutLocations(
6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::Environment* env,
6342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path,
6352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& extension_id) {
6362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath desktop_path;
6372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If Get returns false, just leave desktop_path empty.
6382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PathService::Get(base::DIR_USER_DESKTOP, &desktop_path);
6392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return GetExistingShortcutLocations(env, profile_path, extension_id,
640a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                      desktop_path);
6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
643a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochweb_app::ShortcutLocations GetExistingShortcutLocations(
6442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::Environment* env,
6452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path,
6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& extension_id,
6472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& desktop_path) {
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath shortcut_filename = GetExtensionShortcutFilename(
6512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_path, extension_id);
6522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!shortcut_filename.empty());
653a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  web_app::ShortcutLocations locations;
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Determine whether there is a shortcut on desktop.
6562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!desktop_path.empty()) {
6572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    locations.on_desktop =
6587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        base::PathExists(desktop_path.Append(shortcut_filename));
6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Determine whether there is a shortcut in the applications directory.
6622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string shortcut_contents;
6632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (GetExistingShortcutContents(env, shortcut_filename, &shortcut_contents)) {
664f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // If the shortcut contents contain NoDisplay=true, it should be hidden.
665f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // Otherwise since these shortcuts are for apps, they are always in the
666f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // "Chrome Apps" directory.
667f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    locations.applications_menu_location =
668f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        GetNoDisplayFromDesktopFile(shortcut_contents)
669f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            ? web_app::APP_MENU_LOCATION_HIDDEN
670f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            : web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
6712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return locations;
6742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool GetExistingShortcutContents(base::Environment* env,
6772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 const base::FilePath& desktop_filename,
6782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 std::string* output) {
6792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
6802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6812385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  std::vector<base::FilePath> search_paths = GetDataSearchLocations(env);
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (std::vector<base::FilePath>::const_iterator i = search_paths.begin();
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       i != search_paths.end(); ++i) {
6852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath path = i->Append("applications").Append(desktop_filename);
6862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    VLOG(1) << "Looking for desktop file in " << path.value();
6877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (base::PathExists(path)) {
6882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      VLOG(1) << "Found desktop file at " << path.value();
68958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      return base::ReadFileToString(path, output);
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::FilePath GetWebShortcutFilename(const GURL& url) {
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use a prefix, because xdg-desktop-menu requires it.
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string filename =
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string(chrome::kBrowserProcessExecutableName) + "-" + url.spec();
7006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::i18n::ReplaceIllegalCharactersInPath(&filename, '_');
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath desktop_path;
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!PathService::Get(base::DIR_USER_DESKTOP, &desktop_path))
7042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return base::FilePath();
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath filepath = desktop_path.Append(filename);
7072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath alternative_filepath(filepath.value() + ".desktop");
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 1; i < 100; ++i) {
7097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (base::PathExists(base::FilePath(alternative_filepath))) {
7102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      alternative_filepath = base::FilePath(
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          filepath.value() + "_" + base::IntToString(i) + ".desktop");
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
7132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return base::FilePath(alternative_filepath).BaseName();
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return base::FilePath();
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::FilePath GetExtensionShortcutFilename(const base::FilePath& profile_path,
7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            const std::string& extension_id) {
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!extension_id.empty());
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use a prefix, because xdg-desktop-menu requires it.
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string filename(chrome::kBrowserProcessExecutableName);
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  filename.append("-")
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      .append(extension_id)
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      .append("-")
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      .append(profile_path.BaseName().value());
7306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::i18n::ReplaceIllegalCharactersInPath(&filename, '_');
7317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Spaces in filenames break xdg-desktop-menu
7327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // (see https://bugs.freedesktop.org/show_bug.cgi?id=66605).
733a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::ReplaceChars(filename, " ", "_", &filename);
7342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return base::FilePath(filename.append(".desktop"));
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7372385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochstd::vector<base::FilePath> GetExistingProfileShortcutFilenames(
7382385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const base::FilePath& profile_path,
7392385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const base::FilePath& directory) {
7402385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
7412385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // Use a prefix, because xdg-desktop-menu requires it.
7422385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  std::string prefix(chrome::kBrowserProcessExecutableName);
7432385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  prefix.append("-");
7442385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  std::string suffix("-");
7452385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  suffix.append(profile_path.BaseName().value());
7466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::i18n::ReplaceIllegalCharactersInPath(&suffix, '_');
7472385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // Spaces in filenames break xdg-desktop-menu
7482385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // (see https://bugs.freedesktop.org/show_bug.cgi?id=66605).
749a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::ReplaceChars(suffix, " ", "_", &suffix);
7502385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  std::string glob = prefix + "*" + suffix + ".desktop";
7512385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
7522385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  base::FileEnumerator files(directory, false, base::FileEnumerator::FILES,
7532385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch                             glob);
7542385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  base::FilePath shortcut_file = files.Next();
7552385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  std::vector<base::FilePath> shortcut_paths;
7562385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  while (!shortcut_file.empty()) {
7572385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    shortcut_paths.push_back(shortcut_file.BaseName());
7582385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    shortcut_file = files.Next();
7592385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  }
7602385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  return shortcut_paths;
7612385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch}
7622385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string GetDesktopFileContents(
764c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::FilePath& chrome_exe_path,
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& app_name,
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& url,
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& extension_id,
768a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& title,
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& icon_name,
7702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path,
7715c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    const std::string& categories,
7722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool no_display) {
7735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CommandLine cmd_line = ShellIntegration::CommandLineArgsForLauncher(
7745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      url, extension_id, profile_path);
7755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  cmd_line.SetProgram(chrome_exe_path);
7765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return GetDesktopFileContentsForCommand(cmd_line, app_name, url, title,
7775c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                                          icon_name, categories, no_display);
7785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
7795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::string GetDesktopFileContentsForCommand(
7815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const CommandLine& command_line,
7825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& app_name,
7835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GURL& url,
7845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::string16& title,
7855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& icon_name,
7865c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    const std::string& categories,
7875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool no_display) {
788cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#if defined(USE_GLIB)
7892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Although not required by the spec, Nautilus on Ubuntu Karmic creates its
7902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // launchers with an xdg-open shebang. Follow that convention.
7912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string output_buffer = std::string(kXdgOpenShebang) + "\n";
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See http://standards.freedesktop.org/desktop-entry-spec/latest/
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GKeyFile* key_file = g_key_file_new();
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
796c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Set keys with fixed values.
797c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_key_file_set_string(key_file, kDesktopEntry, "Version", "1.0");
798c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_key_file_set_string(key_file, kDesktopEntry, "Terminal", "false");
799c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_key_file_set_string(key_file, kDesktopEntry, "Type", "Application");
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set the "Name" key.
8025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string final_title = base::UTF16ToUTF8(title);
8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make sure no endline characters can slip in and possibly introduce
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // additional lines (like Exec, which makes it a security risk). Also
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // use the URL as a default when the title is empty.
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (final_title.empty() ||
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      final_title.find("\n") != std::string::npos ||
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      final_title.find("\r") != std::string::npos) {
8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    final_title = url.spec();
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  g_key_file_set_string(key_file, kDesktopEntry, "Name", final_title.c_str());
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set the "Exec" key.
8145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string final_path = QuoteCommandLineForDesktopFileExec(command_line);
815c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_key_file_set_string(key_file, kDesktopEntry, "Exec", final_path.c_str());
816c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set the "Icon" key.
818c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!icon_name.empty()) {
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    g_key_file_set_string(key_file, kDesktopEntry, "Icon", icon_name.c_str());
820c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
821c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    g_key_file_set_string(key_file, kDesktopEntry, "Icon",
822c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                          GetIconName().c_str());
823c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8255c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // Set the "Categories" key.
8265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  if (!categories.empty()) {
8275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    g_key_file_set_string(
8285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        key_file, kDesktopEntry, "Categories", categories.c_str());
8295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
8305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
8312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Set the "NoDisplay" key.
8322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (no_display)
8332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    g_key_file_set_string(key_file, kDesktopEntry, "NoDisplay", "true");
8342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string wmclass = web_app::GetWMClassFromAppName(app_name);
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  g_key_file_set_string(key_file, kDesktopEntry, "StartupWMClass",
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        wmclass.c_str());
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
839c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  gsize length = 0;
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gchar* data_dump = g_key_file_to_data(key_file, &length, NULL);
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (data_dump) {
8422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If strlen(data_dump[0]) == 0, this check will fail.
8432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (data_dump[0] == '\n') {
8442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Older versions of glib produce a leading newline. If this is the case,
8452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // remove it to avoid double-newline after the shebang.
8462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      output_buffer += (data_dump + 1);
8472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
8482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      output_buffer += data_dump;
8492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    g_free(data_dump);
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  g_key_file_free(key_file);
8545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return output_buffer;
855cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#else
856cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NOTIMPLEMENTED();
857cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return std::string();
858cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
861a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)std::string GetDirectoryFileContents(const base::string16& title,
862c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                     const std::string& icon_name) {
863cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#if defined(USE_GLIB)
864c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // See http://standards.freedesktop.org/desktop-entry-spec/latest/
865c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GKeyFile* key_file = g_key_file_new();
866c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
867c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_key_file_set_string(key_file, kDesktopEntry, "Version", "1.0");
868c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_key_file_set_string(key_file, kDesktopEntry, "Type", "Directory");
8695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string final_title = base::UTF16ToUTF8(title);
870c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_key_file_set_string(key_file, kDesktopEntry, "Name", final_title.c_str());
871c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!icon_name.empty()) {
872c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    g_key_file_set_string(key_file, kDesktopEntry, "Icon", icon_name.c_str());
873c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
874c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    g_key_file_set_string(key_file, kDesktopEntry, "Icon",
875c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                          GetIconName().c_str());
876c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
877c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
878c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  gsize length = 0;
879c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  gchar* data_dump = g_key_file_to_data(key_file, &length, NULL);
880c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string output_buffer;
881c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (data_dump) {
882c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // If strlen(data_dump[0]) == 0, this check will fail.
883c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (data_dump[0] == '\n') {
884c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // Older versions of glib produce a leading newline. If this is the case,
885c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      // remove it to avoid double-newline after the shebang.
886c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      output_buffer += (data_dump + 1);
887c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    } else {
888c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      output_buffer += data_dump;
889c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
890c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    g_free(data_dump);
891c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
892c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
893c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_key_file_free(key_file);
894c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return output_buffer;
895cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#else
896cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NOTIMPLEMENTED();
897cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return std::string();
898cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif
899c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
900c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool CreateDesktopShortcut(
902a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    const web_app::ShortcutInfo& shortcut_info,
903a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    const web_app::ShortcutLocations& creation_locations) {
9045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath shortcut_filename;
9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!shortcut_info.extension_id.empty()) {
9085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_filename = GetExtensionShortcutFilename(
9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        shortcut_info.profile_path, shortcut_info.extension_id);
9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // For extensions we do not want duplicate shortcuts. So, delete any that
9115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // already exist and replace them.
9122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (creation_locations.on_desktop)
9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DeleteShortcutOnDesktop(shortcut_filename);
914f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
915a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (creation_locations.applications_menu_location !=
916f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            web_app::APP_MENU_LOCATION_NONE) {
917c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DeleteShortcutInApplicationsMenu(shortcut_filename, base::FilePath());
918f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
9195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
9205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shortcut_filename = GetWebShortcutFilename(shortcut_info.url);
9215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (shortcut_filename.empty())
9235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
9245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string icon_name =
9265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      CreateShortcutIcon(shortcut_info.favicon, shortcut_filename);
9275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string app_name =
9295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      web_app::GenerateApplicationNameFromInfo(shortcut_info);
9305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool success = true;
9325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
933eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  base::FilePath chrome_exe_path = GetChromeExePath();
934eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (chrome_exe_path.empty()) {
935c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    LOG(WARNING) << "Could not get executable path.";
936c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return false;
937c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
938c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
9392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (creation_locations.on_desktop) {
940cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    std::string contents = GetDesktopFileContents(
941c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        chrome_exe_path,
9422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        app_name,
9432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        shortcut_info.url,
9442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        shortcut_info.extension_id,
9452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        shortcut_info.title,
9462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        icon_name,
9472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        shortcut_info.profile_path,
9485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        "",
9492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        false);
9505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    success = CreateShortcutOnDesktop(shortcut_filename, contents);
9512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
9525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
953f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (creation_locations.applications_menu_location ==
954f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          web_app::APP_MENU_LOCATION_NONE) {
955f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return success;
9562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
9575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
958f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  base::FilePath directory_filename;
959f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string directory_contents;
960f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  switch (creation_locations.applications_menu_location) {
961f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case web_app::APP_MENU_LOCATION_ROOT:
962f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case web_app::APP_MENU_LOCATION_HIDDEN:
963f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      break;
964f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS:
965f8ee788a64d60abd8f2d742a5fdedde054ecd910