summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/pending_extension_manager.cc
blob: 5b8e829a3a053b8744186ee6f9fe9bd3314c5f5f (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
// 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 "base/logging.h"
#include "base/stl_util-inl.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/pending_extension_manager.h"
#include "content/browser/browser_thread.h"

namespace {

// Install predicate used by AddFromDefaultAppList().
bool IsApp(const Extension& extension) {
  return extension.is_app();
}

// Install predicate used by AddFromExternalUpdateUrl().
bool AlwaysInstall(const Extension& extension) {
  return true;
}

}  // namespace

PendingExtensionManager::PendingExtensionManager(
    const ExtensionServiceInterface& service)
    : service_(service) {
}

PendingExtensionManager::~PendingExtensionManager() {}

bool PendingExtensionManager::GetById(
    const std::string& id,
    PendingExtensionInfo* out_pending_extension_info) const {

  PendingExtensionMap::const_iterator it = pending_extension_map_.find(id);
  if (it != pending_extension_map_.end()) {
    *out_pending_extension_info = it->second;
    return true;
  }

  return false;
}

void PendingExtensionManager::Remove(const std::string& id) {
  pending_extension_map_.erase(id);
}

bool PendingExtensionManager::IsIdPending(const std::string& id) const {
  return ContainsKey(pending_extension_map_, id);
}

void PendingExtensionManager::AddFromSync(
    const std::string& id,
    const GURL& update_url,
    PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
    bool install_silently,
    bool enable_on_install,
    bool enable_incognito_on_install) {
  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (service_.GetExtensionById(id, true)) {
    LOG(DFATAL) << "Trying to add pending extension " << id
                << " which already exists";
    return;
  }

  AddExtensionImpl(id, update_url, should_allow_install, true,
                   install_silently, enable_on_install,
                   enable_incognito_on_install,
                   Extension::INTERNAL);
}

void PendingExtensionManager::AddFromExternalUpdateUrl(
    const std::string& id, const GURL& update_url,
    Extension::Location location) {
  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  const bool kIsFromSync = false;
  const bool kInstallSilently = true;
  const bool kEnableOnInstall = true;
  const bool kEnableIncognitoOnInstall = false;

  if (service_.IsExternalExtensionUninstalled(id))
    return;

  if (service_.GetExtensionById(id, true)) {
    LOG(DFATAL) << "Trying to add extension " << id
                << " by external update, but it is already installed.";
    return;
  }

  AddExtensionImpl(id, update_url, &AlwaysInstall,
                   kIsFromSync, kInstallSilently,
                   kEnableOnInstall, kEnableIncognitoOnInstall,
                   location);
}


// TODO(akalin): Change DefaultAppList to DefaultExtensionList and
// remove the IsApp() check.
void PendingExtensionManager::AddFromDefaultAppList(
    const std::string& id) {
  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  const bool kIsFromSync = false;
  const bool kInstallSilently = true;
  const bool kEnableOnInstall = true;
  const bool kEnableIncognitoOnInstall = true;

  // This can legitimately happen if the user manually installed one of the
  // default apps before this code ran.
  if (service_.GetExtensionById(id, true))
    return;

  AddExtensionImpl(id, GURL(), &IsApp,
                   kIsFromSync, kInstallSilently,
                   kEnableOnInstall, kEnableIncognitoOnInstall,
                   Extension::INTERNAL);
}

void PendingExtensionManager::AddFromExternalFile(
    const std::string& id,
    Extension::Location location) {

  GURL kUpdateUrl = GURL();
  bool kIsFromSync = false;
  bool kInstallSilently = true;
  bool kEnableOnInstall = true;
  bool kEnableIncognitoOnInstall = false;

  pending_extension_map_[id] =
      PendingExtensionInfo(kUpdateUrl,
                           &AlwaysInstall,
                           kIsFromSync,
                           kInstallSilently,
                           kEnableOnInstall,
                           kEnableIncognitoOnInstall,
                           location);
}

void PendingExtensionManager::AddExtensionImpl(
    const std::string& id, const GURL& update_url,
    PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
    bool is_from_sync, bool install_silently,
    bool enable_on_install, bool enable_incognito_on_install,
    Extension::Location install_source) {
  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  // If a non-sync update is pending, a sync request should not
  // overwrite it.  This is important for external extensions.
  // If an external extension download is pending, and the user has
  // the extension in their sync profile, the install should set the
  // type to be external.  An external extension should not be
  // rejected if it fails the safty checks for a syncable extension.
  // TODO(skerner): Work out other potential overlapping conditions.
  // (crbug.com/61000)

  PendingExtensionInfo pending_extension_info;
  bool has_pending_ext = GetById(id, &pending_extension_info);
  if (has_pending_ext) {
    VLOG(1) << "Extension id " << id
            << " was entered for update more than once."
            << "  old is_from_sync = " << pending_extension_info.is_from_sync()
            << "  new is_from_sync = " << is_from_sync;
    if (!pending_extension_info.is_from_sync() && is_from_sync)
      return;
  }

  pending_extension_map_[id] =
      PendingExtensionInfo(update_url,
                           should_allow_install,
                           is_from_sync,
                           install_silently,
                           enable_on_install,
                           enable_incognito_on_install,
                           install_source);
}

void PendingExtensionManager::AddForTesting(
    const std::string& id,
    const PendingExtensionInfo& pending_extension_info) {
  pending_extension_map_[id] = pending_extension_info;
}