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
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/messaging/incognito_connectability.h"
#include <string>
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/lazy_instance.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/api/messaging/incognito_connectability_infobar_delegate.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/grit/generated_resources.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/infobar.h"
#include "content/public/browser/web_contents.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "ui/base/l10n/l10n_util.h"
namespace extensions {
namespace {
IncognitoConnectability::ScopedAlertTracker::Mode g_alert_mode =
IncognitoConnectability::ScopedAlertTracker::INTERACTIVE;
int g_alert_count = 0;
} // namespace
IncognitoConnectability::ScopedAlertTracker::ScopedAlertTracker(Mode mode)
: last_checked_invocation_count_(g_alert_count) {
DCHECK_EQ(INTERACTIVE, g_alert_mode);
DCHECK_NE(INTERACTIVE, mode);
g_alert_mode = mode;
}
IncognitoConnectability::ScopedAlertTracker::~ScopedAlertTracker() {
DCHECK_NE(INTERACTIVE, g_alert_mode);
g_alert_mode = INTERACTIVE;
}
int IncognitoConnectability::ScopedAlertTracker::GetAndResetAlertCount() {
int result = g_alert_count - last_checked_invocation_count_;
last_checked_invocation_count_ = g_alert_count;
return result;
}
IncognitoConnectability::IncognitoConnectability(
content::BrowserContext* context) {
CHECK(context->IsOffTheRecord());
}
IncognitoConnectability::~IncognitoConnectability() = default;
// static
IncognitoConnectability* IncognitoConnectability::Get(
content::BrowserContext* context) {
return BrowserContextKeyedAPIFactory<IncognitoConnectability>::Get(context);
}
void IncognitoConnectability::Query(const Extension* extension,
content::WebContents* web_contents,
const GURL& url,
base::OnceCallback<void(bool)> callback) {
GURL origin = url.DeprecatedGetOriginAsURL();
if (origin.is_empty()) {
std::move(callback).Run(false);
return;
}
if (IsInMap(extension, origin, allowed_origins_)) {
std::move(callback).Run(true);
return;
}
if (IsInMap(extension, origin, disallowed_origins_)) {
std::move(callback).Run(false);
return;
}
PendingOrigin& pending_origin =
pending_origins_[make_pair(extension->id(), origin)];
infobars::ContentInfoBarManager* infobar_manager =
infobars::ContentInfoBarManager::FromWebContents(web_contents);
TabContext& tab_context = pending_origin[infobar_manager];
tab_context.callbacks.push_back(std::move(callback));
if (tab_context.infobar) {
// This tab is already displaying an infobar for this extension and origin.
return;
}
// We need to ask the user.
++g_alert_count;
switch (g_alert_mode) {
// Production code should always be using INTERACTIVE.
case ScopedAlertTracker::INTERACTIVE: {
int template_id =
extension->is_app()
? IDS_EXTENSION_PROMPT_APP_CONNECT_FROM_INCOGNITO
: IDS_EXTENSION_PROMPT_EXTENSION_CONNECT_FROM_INCOGNITO;
tab_context.infobar = IncognitoConnectabilityInfoBarDelegate::Create(
infobar_manager,
l10n_util::GetStringFUTF16(template_id,
base::UTF8ToUTF16(origin.spec()),
base::UTF8ToUTF16(extension->name())),
base::BindOnce(&IncognitoConnectability::OnInteractiveResponse,
weak_factory_.GetWeakPtr(), extension->id(), origin,
infobar_manager));
break;
}
// Testing code can override to always allow or deny.
case ScopedAlertTracker::ALWAYS_ALLOW:
case ScopedAlertTracker::ALWAYS_DENY:
OnInteractiveResponse(extension->id(), origin, infobar_manager,
g_alert_mode);
break;
}
}
IncognitoConnectability::TabContext::TabContext() : infobar(nullptr) {
}
IncognitoConnectability::TabContext::~TabContext() = default;
void IncognitoConnectability::OnInteractiveResponse(
const ExtensionId& extension_id,
const GURL& origin,
infobars::ContentInfoBarManager* infobar_manager,
ScopedAlertTracker::Mode response) {
switch (response) {
case ScopedAlertTracker::ALWAYS_ALLOW:
allowed_origins_[extension_id].insert(origin);
break;
case ScopedAlertTracker::ALWAYS_DENY:
disallowed_origins_[extension_id].insert(origin);
break;
default:
// Otherwise the user has not expressed an explicit preference and so
// nothing should be permanently recorded.
break;
}
PendingOriginMap::iterator origin_it =
pending_origins_.find(make_pair(extension_id, origin));
CHECK(origin_it != pending_origins_.end());
PendingOrigin& pending_origin = origin_it->second;
DCHECK(base::Contains(pending_origin, infobar_manager));
std::vector<base::OnceCallback<void(bool)>> callbacks;
if (response == ScopedAlertTracker::INTERACTIVE) {
// No definitive answer for this extension and origin. Execute only the
// callbacks associated with this tab.
TabContext& tab_context = pending_origin[infobar_manager];
callbacks.swap(tab_context.callbacks);
pending_origin.erase(infobar_manager);
} else {
// We have a definitive answer for this extension and origin. Close all
// other infobars and answer all the callbacks.
for (auto& map_entry : pending_origin) {
infobars::ContentInfoBarManager* other_infobar_manager = map_entry.first;
TabContext& other_tab_context = map_entry.second;
if (other_infobar_manager != infobar_manager) {
// Disarm the delegate so that it doesn't think the infobar has been
// dismissed.
IncognitoConnectabilityInfoBarDelegate* delegate =
static_cast<IncognitoConnectabilityInfoBarDelegate*>(
other_tab_context.infobar->delegate());
delegate->set_answered();
other_infobar_manager->RemoveInfoBar(other_tab_context.infobar);
}
callbacks.insert(
callbacks.end(),
std::make_move_iterator(other_tab_context.callbacks.begin()),
std::make_move_iterator(other_tab_context.callbacks.end()));
}
pending_origins_.erase(origin_it);
}
DCHECK(!callbacks.empty());
for (auto& callback : callbacks) {
std::move(callback).Run(response == ScopedAlertTracker::ALWAYS_ALLOW);
}
}
bool IncognitoConnectability::IsInMap(const Extension* extension,
const GURL& origin,
const ExtensionToOriginsMap& map) {
DCHECK_EQ(origin, origin.DeprecatedGetOriginAsURL());
auto it = map.find(extension->id());
return it != map.end() && it->second.count(origin) > 0;
}
static base::LazyInstance<
BrowserContextKeyedAPIFactory<IncognitoConnectability>>::DestructorAtExit
g_incognito_connectability_factory = LAZY_INSTANCE_INITIALIZER;
// static
BrowserContextKeyedAPIFactory<IncognitoConnectability>*
IncognitoConnectability::GetFactoryInstance() {
return g_incognito_connectability_factory.Pointer();
}
// static
void IncognitoConnectability::EnsureFactoryBuilt() {
GetFactoryInstance();
}
} // namespace extensions
|