summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_install_dialog.cc
blob: dad6d26c9f9facaa8794fe5d26a4f4e73b26989a (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
// Copyright (c) 2011 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/file_path.h"
#include "base/message_loop.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/common/extensions/extension.h"

namespace {

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

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

void DoAutoConfirm(ExtensionInstallUI::Delegate* delegate) {
  bool proceed = (auto_confirm_for_tests == 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));
}

}  // namespace

void ShowExtensionInstallDialog(Profile* profile,
                                ExtensionInstallUI::Delegate* delegate,
                                const Extension* extension,
                                SkBitmap* icon,
                                const ExtensionInstallUI::Prompt& prompt) {
  if (auto_confirm_for_tests != DO_NOT_SKIP) {
    DoAutoConfirm(delegate);
    return;
  }
  ShowExtensionInstallDialogImpl(profile, delegate, extension, icon, prompt);
}

bool ShowExtensionInstallDialogForManifest(
    Profile *profile,
    ExtensionInstallUI::Delegate* delegate,
    const DictionaryValue* manifest,
    const std::string& id,
    const std::string& localized_name,
    const std::string& localized_description,
    SkBitmap* icon,
    const ExtensionInstallUI::Prompt& prompt,
    scoped_refptr<Extension>* dummy_extension) {
  scoped_ptr<DictionaryValue> localized_manifest;
  if (!localized_name.empty() || !localized_description.empty()) {
    localized_manifest.reset(manifest->DeepCopy());
    if (!localized_name.empty()) {
      localized_manifest->SetString(extension_manifest_keys::kName,
                                    localized_name);
    }
    if (!localized_description.empty()) {
      localized_manifest->SetString(extension_manifest_keys::kDescription,
                                    localized_description);
    }
  }

  std::string init_errors;
  *dummy_extension = Extension::CreateWithId(
      FilePath(),
      Extension::INTERNAL,
      localized_manifest.get() ? *localized_manifest.get() : *manifest,
      Extension::NO_FLAGS,
      id,
      &init_errors);
  if (!dummy_extension->get()) {
    return false;
  }

  if (icon->empty())
    icon = const_cast<SkBitmap*>(&Extension::GetDefaultIcon(
        (*dummy_extension)->is_app()));

  // In tests, we may have setup to proceed or abort without putting up the real
  // confirmation dialog.
  if (auto_confirm_for_tests != DO_NOT_SKIP) {
    DoAutoConfirm(delegate);
    return true;
  }

  ExtensionInstallUI::Prompt filled_out_prompt = prompt;
  filled_out_prompt.SetPermissions(
      (*dummy_extension)->GetPermissionMessageStrings());

  ShowExtensionInstallDialog(profile,
                             delegate,
                             dummy_extension->get(),
                             icon,
                             filled_out_prompt);
  return true;
}

void SetExtensionInstallDialogAutoConfirmForTests(
    bool should_proceed) {
  auto_confirm_for_tests = should_proceed ? PROCEED : ABORT;
}