summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_install_dialog.cc
blob: 10bd82cb57a9c4da0db1de67c5ad8865b4355d5f (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
// 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/extensions/extension_install_dialog.h"

#include "base/bind.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/values.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_manifest_constants.h"
#include "ui/gfx/image/image.h"

namespace {

// A flag used for SetExtensionInstallDialogAutoConfirmForTests
enum AutoConfirmForTest {
  DO_NOT_SKIP = 0,
  PROCEED,
  ABORT
};

void AutoConfirmTask(ExtensionInstallPrompt::Delegate* delegate, bool proceed) {
  if (proceed)
    delegate->InstallUIProceed();
  else
    delegate->InstallUIAbort(true);
}

void DoAutoConfirm(AutoConfirmForTest setting,
                   ExtensionInstallPrompt::Delegate* delegate) {
  bool proceed = (setting == PROCEED);
  // We use PostTask instead of calling the delegate directly here, because in
  // the real implementations it's highly likely the message loop will be
  // pumping a few times before the user clicks accept or cancel.
  MessageLoop::current()->PostTask(
        FROM_HERE,
        base::Bind(&AutoConfirmTask, delegate, proceed));
}

AutoConfirmForTest CheckAutoConfirmCommandLineSwitch() {
  const CommandLine* cmdline = CommandLine::ForCurrentProcess();
  if (!cmdline->HasSwitch(switches::kAppsGalleryInstallAutoConfirmForTests))
    return DO_NOT_SKIP;
  std::string value = cmdline->GetSwitchValueASCII(
      switches::kAppsGalleryInstallAutoConfirmForTests);
  if (value == "accept")
    return PROCEED;
  else if (value == "cancel")
    return ABORT;
  else
    NOTREACHED();
  return DO_NOT_SKIP;
}

}  // namespace

void ShowExtensionInstallDialog(Browser* browser,
                                ExtensionInstallPrompt::Delegate* delegate,
                                const ExtensionInstallPrompt::Prompt& prompt) {
  AutoConfirmForTest auto_confirm = CheckAutoConfirmCommandLineSwitch();
  if (auto_confirm != DO_NOT_SKIP) {
    DoAutoConfirm(auto_confirm, delegate);
    return;
  }
  ShowExtensionInstallDialogImpl(browser, delegate, prompt);
}