summaryrefslogtreecommitdiffstats
path: root/chrome/browser/shell_integration.cc
blob: c1f83461247867c57c29bcd66914515d18c8240f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/shell_integration.h"

#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "base/timer/timer.h"
#include "chrome/browser/policy/policy_path_parser.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/browser_thread.h"

#if defined(OS_CHROMEOS)
#include "chromeos/chromeos_switches.h"
#endif

#if !defined(OS_WIN)
#include "chrome/common/channel_info.h"
#include "chrome/grit/chromium_strings.h"
#include "ui/base/l10n/l10n_util.h"
#endif

using content::BrowserThread;

namespace {

const struct ShellIntegration::AppModeInfo* gAppModeInfo = nullptr;

}  // namespace

#if !defined(OS_WIN)
// static
bool ShellIntegration::SetAsDefaultBrowserInteractive() {
  return false;
}

// static
bool ShellIntegration::IsSetAsDefaultAsynchronous() {
  return false;
}

// static
bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
    const std::string& protocol) {
  return false;
}
#endif  // !defined(OS_WIN)

// static
ShellIntegration::DefaultWebClientSetPermission
    ShellIntegration::CanSetAsDefaultProtocolClient() {
  // Allowed as long as the browser can become the operating system default
  // browser.
  DefaultWebClientSetPermission permission = CanSetAsDefaultBrowser();

  // Set as default asynchronous is only supported for default web browser.
  return (permission == SET_DEFAULT_ASYNCHRONOUS) ? SET_DEFAULT_INTERACTIVE
                                                  : permission;
}

#if !defined(OS_WIN)
// static
bool ShellIntegration::IsElevationNeededForSettingDefaultProtocolClient() {
  return false;
}
#endif  // !defined(OS_WIN)

// static
void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) {
  gAppModeInfo = info;
}

// static
const struct ShellIntegration::AppModeInfo* ShellIntegration::AppModeInfo() {
  return gAppModeInfo;
}

// static
bool ShellIntegration::IsRunningInAppMode() {
  return gAppModeInfo != NULL;
}

// static
base::CommandLine ShellIntegration::CommandLineArgsForLauncher(
    const GURL& url,
    const std::string& extension_app_id,
    const base::FilePath& profile_path) {
  base::ThreadRestrictions::AssertIOAllowed();
  base::CommandLine new_cmd_line(base::CommandLine::NO_PROGRAM);

  AppendProfileArgs(
      extension_app_id.empty() ? base::FilePath() : profile_path,
      &new_cmd_line);

  // If |extension_app_id| is present, we use the kAppId switch rather than
  // the kApp switch (the launch url will be read from the extension app
  // during launch.
  if (!extension_app_id.empty()) {
    new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
  } else {
    // Use '--app=url' instead of just 'url' to launch the browser with minimal
    // chrome.
    // Note: Do not change this flag!  Old Gears shortcuts will break if you do!
    new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
  }
  return new_cmd_line;
}

// static
void ShellIntegration::AppendProfileArgs(const base::FilePath& profile_path,
                                         base::CommandLine* command_line) {
  DCHECK(command_line);
  const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();

  // Use the same UserDataDir for new launches that we currently have set.
  base::FilePath user_data_dir =
      cmd_line.GetSwitchValuePath(switches::kUserDataDir);
#if defined(OS_MACOSX) || defined(OS_WIN)
  policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
#endif
  if (!user_data_dir.empty()) {
    // Make sure user_data_dir is an absolute path.
    user_data_dir = base::MakeAbsoluteFilePath(user_data_dir);
    if (!user_data_dir.empty() && base::PathExists(user_data_dir))
      command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
  }

#if defined(OS_CHROMEOS)
  base::FilePath profile = cmd_line.GetSwitchValuePath(
      chromeos::switches::kLoginProfile);
  if (!profile.empty())
    command_line->AppendSwitchPath(chromeos::switches::kLoginProfile, profile);
#else
  if (!profile_path.empty())
    command_line->AppendSwitchPath(switches::kProfileDirectory,
                                   profile_path.BaseName());
#endif
}

#if !defined(OS_WIN)
base::string16 ShellIntegration::GetAppShortcutsSubdirName() {
  if (chrome::GetChannel() == version_info::Channel::CANARY)
    return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY);
  return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME);
}
#endif  // !defined(OS_WIN)

///////////////////////////////////////////////////////////////////////////////
// ShellIntegration::DefaultWebClientObserver
//

bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
  return false;
}

bool ShellIntegration::DefaultWebClientObserver::
    IsInteractiveSetDefaultPermitted() {
  return false;
}

///////////////////////////////////////////////////////////////////////////////
// ShellIntegration::DefaultWebClientWorker
//

ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
    DefaultWebClientObserver* observer)
    : observer_(observer) {}

void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
  if (observer_)
    observer_->SetDefaultWebClientUIState(STATE_PROCESSING);

  BrowserThread::PostTask(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&DefaultWebClientWorker::CheckIsDefault, this));
}

void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
  // Cancel the already running process if another start is requested.
  if (set_as_default_in_progress_) {
    if (set_as_default_initialized_) {
      FinalizeSetAsDefault();
      set_as_default_initialized_ = false;
    }

    ReportAttemptResult(AttemptResult::RETRY);
  }

  set_as_default_in_progress_ = true;
  bool interactive_permitted = true;
  if (observer_) {
    observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
    interactive_permitted = observer_->IsInteractiveSetDefaultPermitted();
  }

  set_as_default_initialized_ = InitializeSetAsDefault();

  // Remember the start time.
  start_time_ = base::TimeTicks::Now();

  BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
                          base::Bind(&DefaultWebClientWorker::SetAsDefault,
                                     this, interactive_permitted));
}

void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
  // Our associated view has gone away, so we shouldn't call back to it if
  // our worker thread returns after the view is dead.
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  observer_ = nullptr;

  if (set_as_default_initialized_) {
    FinalizeSetAsDefault();
    set_as_default_initialized_ = false;
  }

  if (set_as_default_in_progress_)
    ReportAttemptResult(AttemptResult::ABANDONED);
}

///////////////////////////////////////////////////////////////////////////////
// DefaultWebClientWorker, private:

ShellIntegration::DefaultWebClientWorker::~DefaultWebClientWorker() {}

void ShellIntegration::DefaultWebClientWorker::OnCheckIsDefaultComplete(
    DefaultWebClientState state) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  UpdateUI(state);

  if (check_default_should_report_success_) {
    check_default_should_report_success_ = false;

    ReportAttemptResult(state == DefaultWebClientState::IS_DEFAULT
                            ? AttemptResult::SUCCESS
                            : AttemptResult::NO_ERRORS_NOT_DEFAULT);
  }

  // The worker has finished everything it needs to do, so free the observer
  // if we own it.
  if (observer_ && observer_->IsOwnedByWorker()) {
    delete observer_;
    observer_ = nullptr;
  }
}

void ShellIntegration::DefaultWebClientWorker::OnSetAsDefaultAttemptComplete(
    AttemptResult result) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  // Hold on to a reference because if this was called via the default browser
  // callback in StartupBrowserCreator, clearing the callback in
  // FinalizeSetAsDefault() would otherwise remove the last reference and delete
  // us in the middle of this function.
  scoped_refptr<DefaultWebClientWorker> scoped_ref(this);

  if (set_as_default_in_progress_) {
    set_as_default_in_progress_ = false;

    if (set_as_default_initialized_) {
      FinalizeSetAsDefault();
      set_as_default_initialized_ = false;
    }
    if (observer_) {
      bool succeeded = result == AttemptResult::SUCCESS ||
                       result == AttemptResult::ALREADY_DEFAULT;
      observer_->OnSetAsDefaultConcluded(succeeded);
    }

    // Report failures here. Successes are reported in
    // OnCheckIsDefaultComplete() after checking that the change is verified.
    check_default_should_report_success_ = result == AttemptResult::SUCCESS;
    if (!check_default_should_report_success_)
      ReportAttemptResult(result);

    // Start the default browser check which will notify the observer as to
    // whether Chrome is really the default browser. This is needed because
    // detecting that the process was successful is not 100% sure.
    // For example, on Windows 10+, the user might have unchecked the "Always
    // use this app" checkbox which can't be detected.
    StartCheckIsDefault();
  }
}

void ShellIntegration::DefaultWebClientWorker::ReportAttemptResult(
    AttemptResult result) {
  const char* histogram_prefix = GetHistogramPrefix();

  // Report result.
  base::LinearHistogram::FactoryGet(
      base::StringPrintf("%s.SetDefaultResult", histogram_prefix), 1,
      AttemptResult::NUM_ATTEMPT_RESULT_TYPES,
      AttemptResult::NUM_ATTEMPT_RESULT_TYPES + 1,
      base::HistogramBase::kUmaTargetedHistogramFlag)
      ->Add(result);

  // Report asynchronous duration.
  if (IsSetAsDefaultAsynchronous() && ShouldReportDurationForResult(result)) {
    base::Histogram::FactoryTimeGet(
        base::StringPrintf("%s.SetDefaultAsyncDuration_%s", histogram_prefix,
                           AttemptResultToString(result)),
        base::TimeDelta::FromMilliseconds(10), base::TimeDelta::FromMinutes(3),
        50, base::HistogramBase::kUmaTargetedHistogramFlag)
        ->AddTime(base::TimeTicks::Now() - start_time_);
  }
}

bool ShellIntegration::DefaultWebClientWorker::InitializeSetAsDefault() {
  return true;
}

void ShellIntegration::DefaultWebClientWorker::FinalizeSetAsDefault() {}

void ShellIntegration::DefaultWebClientWorker::UpdateUI(
    DefaultWebClientState state) {
  if (observer_) {
    switch (state) {
      case NOT_DEFAULT:
        observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT);
        break;
      case IS_DEFAULT:
        observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT);
        break;
      case UNKNOWN_DEFAULT:
        observer_->SetDefaultWebClientUIState(STATE_UNKNOWN);
        break;
      default:
        break;
    }
  }
}

// static
bool ShellIntegration::DefaultWebClientWorker::ShouldReportDurationForResult(
    AttemptResult result) {
  return result == SUCCESS || result == FAILURE || result == ABANDONED ||
         result == RETRY;
}

// static
const char* ShellIntegration::DefaultWebClientWorker::AttemptResultToString(
    AttemptResult result) {
  switch (result) {
    case SUCCESS:
      return "Success";
    case ALREADY_DEFAULT:
      return "AlreadyDefault";
    case FAILURE:
      return "Failure";
    case ABANDONED:
      return "Abandoned";
    case LAUNCH_FAILURE:
      return "LaunchFailure";
    case OTHER_WORKER:
      return "OtherWorker";
    case RETRY:
      return "Retry";
    case NO_ERRORS_NOT_DEFAULT:
      return "NoErrorsNotDefault";
    case NUM_ATTEMPT_RESULT_TYPES:
      break;
  }
  NOTREACHED();
  return "";
}

///////////////////////////////////////////////////////////////////////////////
// ShellIntegration::DefaultBrowserWorker
//

ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
    DefaultWebClientObserver* observer)
    : DefaultWebClientWorker(observer) {
}

ShellIntegration::DefaultBrowserWorker::~DefaultBrowserWorker() {}

///////////////////////////////////////////////////////////////////////////////
// DefaultBrowserWorker, private:

void ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
  DefaultWebClientState state = GetDefaultBrowser();
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&DefaultBrowserWorker::OnCheckIsDefaultComplete, this, state));
}

void ShellIntegration::DefaultBrowserWorker::SetAsDefault(
    bool interactive_permitted) {
  AttemptResult result = AttemptResult::FAILURE;
  switch (CanSetAsDefaultBrowser()) {
    case SET_DEFAULT_NOT_ALLOWED:
      NOTREACHED();
      break;
    case SET_DEFAULT_UNATTENDED:
      if (SetAsDefaultBrowser())
        result = AttemptResult::SUCCESS;
      break;
    case SET_DEFAULT_INTERACTIVE:
      if (interactive_permitted && SetAsDefaultBrowserInteractive())
        result = AttemptResult::SUCCESS;
      break;
    case SET_DEFAULT_ASYNCHRONOUS:
#if defined(OS_WIN)
      if (!interactive_permitted)
        break;
      if (GetDefaultBrowser() == IS_DEFAULT) {
        // Don't start the asynchronous operation since it could result in
        // losing the default browser status.
        result = AttemptResult::ALREADY_DEFAULT;
        break;
      }
      // This function will cause OnSetAsDefaultAttemptComplete() to be called
      // asynchronously via a filter established in InitializeSetAsDefault().
      if (!SetAsDefaultBrowserAsynchronous()) {
        result = AttemptResult::LAUNCH_FAILURE;
        break;
      }
      return;
#else
      NOTREACHED();
      break;
#endif
  }
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&DefaultBrowserWorker::OnSetAsDefaultAttemptComplete, this,
                 result));
}

const char* ShellIntegration::DefaultBrowserWorker::GetHistogramPrefix() {
  return "DefaultBrowser";
}

///////////////////////////////////////////////////////////////////////////////
// ShellIntegration::DefaultProtocolClientWorker
//

ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
    DefaultWebClientObserver* observer, const std::string& protocol)
    : DefaultWebClientWorker(observer),
      protocol_(protocol) {
}

///////////////////////////////////////////////////////////////////////////////
// DefaultProtocolClientWorker, private:

ShellIntegration::DefaultProtocolClientWorker::~DefaultProtocolClientWorker() {}

void ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
  DefaultWebClientState state = IsDefaultProtocolClient(protocol_);
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&DefaultProtocolClientWorker::OnCheckIsDefaultComplete, this,
                 state));
}

void ShellIntegration::DefaultProtocolClientWorker::SetAsDefault(
    bool interactive_permitted) {
  AttemptResult result = AttemptResult::FAILURE;
  switch (CanSetAsDefaultProtocolClient()) {
    case SET_DEFAULT_NOT_ALLOWED:
      // Not allowed, do nothing.
      break;
    case SET_DEFAULT_UNATTENDED:
      if (SetAsDefaultProtocolClient(protocol_))
        result = AttemptResult::SUCCESS;
      break;
    case SET_DEFAULT_INTERACTIVE:
      if (interactive_permitted &&
          SetAsDefaultProtocolClientInteractive(protocol_)) {
        result = AttemptResult::SUCCESS;
      }
      break;
    case SET_DEFAULT_ASYNCHRONOUS:
      NOTREACHED();
      break;
  }
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&DefaultProtocolClientWorker::OnSetAsDefaultAttemptComplete,
                 this, result));
}

const char*
ShellIntegration::DefaultProtocolClientWorker::GetHistogramPrefix() {
  return "DefaultProtocolClient";
}