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.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/threading/thread_restrictions.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/policy/policy_path_parser.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_paths.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_CHROMEOS)
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chromeos/chromeos_switches.h"
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
25a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#if !defined(OS_WIN)
26a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/common/chrome_version_info.h"
2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/grit/chromium_strings.h"
28a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
29a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif
30a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::DefaultWebClientSetPermission
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ShellIntegration::CanSetAsDefaultProtocolClient() {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Allowed as long as the browser can become the operating system default
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // browser.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return CanSetAsDefaultBrowser();
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gAppModeInfo = info;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const struct ShellIntegration::AppModeInfo* ShellIntegration::AppModeInfo() {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return gAppModeInfo;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ShellIntegration::IsRunningInAppMode() {
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return gAppModeInfo != NULL;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)CommandLine ShellIntegration::CommandLineArgsForLauncher(
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& url,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& extension_app_id,
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& profile_path) {
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::ThreadRestrictions::AssertIOAllowed();
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CommandLine new_cmd_line(CommandLine::NO_PROGRAM);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AppendProfileArgs(
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      extension_app_id.empty() ? base::FilePath() : profile_path,
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      &new_cmd_line);
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If |extension_app_id| is present, we use the kAppId switch rather than
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // the kApp switch (the launch url will be read from the extension app
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // during launch.
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!extension_app_id.empty()) {
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else {
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Use '--app=url' instead of just 'url' to launch the browser with minimal
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // chrome.
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Note: Do not change this flag!  Old Gears shortcuts will break if you do!
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return new_cmd_line;
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// static
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void ShellIntegration::AppendProfileArgs(
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const base::FilePath& profile_path,
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    CommandLine* command_line) {
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(command_line);
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use the same UserDataDir for new launches that we currently have set.
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath user_data_dir =
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      cmd_line.GetSwitchValuePath(switches::kUserDataDir);
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_MACOSX) || defined(OS_WIN)
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!user_data_dir.empty()) {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Make sure user_data_dir is an absolute path.
98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_data_dir = base::MakeAbsoluteFilePath(user_data_dir);
997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (!user_data_dir.empty() && base::PathExists(user_data_dir))
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::FilePath profile = cmd_line.GetSwitchValuePath(
105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      chromeos::switches::kLoginProfile);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!profile.empty())
107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    command_line->AppendSwitchPath(chromeos::switches::kLoginProfile, profile);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!profile_path.empty())
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    command_line->AppendSwitchPath(switches::kProfileDirectory,
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                   profile_path.BaseName());
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(OS_WIN)
116a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
117a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 ShellIntegration::GetAppShortcutsSubdirName() {
118a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_CANARY)
119a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY);
120a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME);
121a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
122a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ShellIntegration::SetAsDefaultBrowserInteractive() {
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& protocol) {
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ShellIntegration::DefaultWebClientObserver::
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IsInteractiveSetDefaultPermitted() {
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ShellIntegration::DefaultWebClientWorker
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DefaultWebClientObserver* observer)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : observer_(observer) {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (observer_) {
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BrowserThread::PostTask(
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        BrowserThread::FILE, FROM_HERE,
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::Bind(
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            &DefaultWebClientWorker::ExecuteCheckIsDefault, this));
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool interactive_permitted = false;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (observer_) {
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    interactive_permitted = observer_->IsInteractiveSetDefaultPermitted();
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BrowserThread::PostTask(
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      BrowserThread::FILE, FROM_HERE,
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault, this,
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 interactive_permitted));
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Our associated view has gone away, so we shouldn't call back to it if
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // our worker thread returns after the view is dead.
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observer_ = NULL;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DefaultWebClientWorker, private:
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DefaultWebClientState state = CheckIsDefault();
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BrowserThread::PostTask(
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      BrowserThread::UI, FROM_HERE,
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          &DefaultWebClientWorker::CompleteCheckIsDefault, this, state));
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DefaultWebClientState state) {
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UpdateUI(state);
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The worker has finished everything it needs to do, so free the observer
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if we own it.
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (observer_ && observer_->IsOwnedByWorker()) {
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete observer_;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    observer_ = NULL;
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault(
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool interactive_permitted) {
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool result = SetAsDefault(interactive_permitted);
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BrowserThread::PostTask(
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      BrowserThread::UI, FROM_HERE,
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault, this, result));
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault(
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool succeeded) {
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // First tell the observer what the SetAsDefault call has returned.
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (observer_)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    observer_->OnSetAsDefaultConcluded(succeeded);
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set as default completed, check again to make sure it stuck...
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartCheckIsDefault();
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShellIntegration::DefaultWebClientWorker::UpdateUI(
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DefaultWebClientState state) {
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (observer_) {
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    switch (state) {
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      case NOT_DEFAULT:
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT);
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      case IS_DEFAULT:
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      case UNKNOWN_DEFAULT:
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        observer_->SetDefaultWebClientUIState(STATE_UNKNOWN);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      default:
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ShellIntegration::DefaultBrowserWorker
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DefaultWebClientObserver* observer)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : DefaultWebClientWorker(observer) {
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DefaultBrowserWorker, private:
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::DefaultWebClientState
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return ShellIntegration::GetDefaultBrowser();
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ShellIntegration::DefaultBrowserWorker::SetAsDefault(
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool interactive_permitted) {
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool result = false;
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (ShellIntegration::CanSetAsDefaultBrowser()) {
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ShellIntegration::SET_DEFAULT_UNATTENDED:
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      result = ShellIntegration::SetAsDefaultBrowser();
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ShellIntegration::SET_DEFAULT_INTERACTIVE:
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (interactive_permitted)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        result = ShellIntegration::SetAsDefaultBrowserInteractive();
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ShellIntegration::DefaultProtocolClientWorker
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DefaultWebClientObserver* observer, const std::string& protocol)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : DefaultWebClientWorker(observer),
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      protocol_(protocol) {
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DefaultProtocolClientWorker, private:
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::DefaultWebClientState
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ShellIntegration::IsDefaultProtocolClient(protocol_);
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault(
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool interactive_permitted) {
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool result = false;
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (ShellIntegration::CanSetAsDefaultProtocolClient()) {
302d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    case ShellIntegration::SET_DEFAULT_NOT_ALLOWED:
303d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      result = false;
304d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      break;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ShellIntegration::SET_DEFAULT_UNATTENDED:
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      result = ShellIntegration::SetAsDefaultProtocolClient(protocol_);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ShellIntegration::SET_DEFAULT_INTERACTIVE:
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (interactive_permitted) {
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        result = ShellIntegration::SetAsDefaultProtocolClientInteractive(
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            protocol_);
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
318