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 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
|
// 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_web_ui.h"
#include <stddef.h>
#include <set>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/url_constants.h"
#include "components/favicon/core/favicon_service.h"
#include "components/favicon_base/favicon_util.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/common/bindings_policy.h"
#include "extensions/browser/extension_icon_placeholder.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/image_loader.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_icon_set.h"
#include "extensions/common/extension_resource.h"
#include "extensions/common/manifest_handlers/icons_handler.h"
#include "extensions/common/manifest_handlers/incognito_info.h"
#include "net/base/file_stream.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/page_transition_types.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/favicon_size.h"
#include "ui/gfx/image/image_skia.h"
using content::WebContents;
using extensions::Extension;
using extensions::URLOverrides;
namespace {
// The key to the override value for a page.
const char kEntry[] = "entry";
// The key to whether or not the override is active (i.e., can be used).
// Overrides may be inactive e.g. when an extension is disabled.
const char kActive[] = "active";
// Iterates over |list| and:
// - Converts any entries of the form <entry> to
// { 'entry': <entry>, 'active': true }.
// - Removes any duplicate entries.
// We do the conversion because we previously stored these values as strings
// rather than objects.
// TODO(devlin): Remove the conversion once everyone's updated.
void InitializeOverridesList(base::ListValue* list) {
base::ListValue migrated;
std::set<std::string> seen_entries;
for (auto& val : *list) {
std::unique_ptr<base::DictionaryValue> new_dict(
new base::DictionaryValue());
std::string entry_name;
base::DictionaryValue* existing_dict = nullptr;
if (val.GetAsDictionary(&existing_dict)) {
bool success = existing_dict->GetString(kEntry, &entry_name);
if (!success) // See comment about CHECK(success) in ForEachOverrideList.
continue;
new_dict->Swap(existing_dict);
} else if (val.GetAsString(&entry_name)) {
new_dict->SetString(kEntry, entry_name);
new_dict->SetBoolean(kActive, true);
} else {
NOTREACHED();
continue;
}
if (seen_entries.count(entry_name) == 0) {
seen_entries.insert(entry_name);
migrated.Append(std::move(new_dict));
}
}
list->Swap(&migrated);
}
// Adds |override| to |list|, or, if there's already an entry for the override,
// marks it as active.
void AddOverridesToList(base::ListValue* list, const GURL& override_url) {
const std::string& spec = override_url.spec();
for (auto& val : *list) {
base::DictionaryValue* dict = nullptr;
std::string entry;
if (!val.GetAsDictionary(&dict) || !dict->GetString(kEntry, &entry)) {
NOTREACHED();
continue;
}
if (entry == spec) {
dict->SetBoolean(kActive, true);
return; // All done!
}
GURL entry_url(entry);
if (!entry_url.is_valid()) {
NOTREACHED();
continue;
}
if (entry_url.host() == override_url.host()) {
dict->SetBoolean(kActive, true);
dict->SetString(kEntry, spec);
return;
}
}
auto dict = std::make_unique<base::DictionaryValue>();
dict->SetString(kEntry, spec);
dict->SetBoolean(kActive, true);
// Add the entry to the front of the list.
list->Insert(0, std::move(dict));
}
// Validates that each entry in |list| contains a valid url and points to an
// extension contained in |all_extensions| (and, if not, removes it).
void ValidateOverridesList(const extensions::ExtensionSet* all_extensions,
base::ListValue* list) {
base::ListValue migrated;
std::set<std::string> seen_hosts;
for (auto& val : *list) {
base::DictionaryValue* dict = nullptr;
std::string entry;
if (!val.GetAsDictionary(&dict) || !dict->GetString(kEntry, &entry)) {
NOTREACHED();
continue;
}
std::unique_ptr<base::DictionaryValue> new_dict(
new base::DictionaryValue());
new_dict->Swap(dict);
GURL override_url(entry);
if (!override_url.is_valid())
continue;
if (!all_extensions->GetByID(override_url.host()))
continue;
// If we've already seen this extension, remove the entry. Only retain the
// most recent entry for each extension.
if (!seen_hosts.insert(override_url.host()).second)
continue;
migrated.Append(std::move(new_dict));
}
list->Swap(&migrated);
}
// Reloads the page in |web_contents| if it uses the same profile as |profile|
// and if the current URL is a chrome URL.
void UnregisterAndReplaceOverrideForWebContents(const std::string& page,
Profile* profile,
WebContents* web_contents) {
if (Profile::FromBrowserContext(web_contents->GetBrowserContext()) != profile)
return;
GURL url = web_contents->GetURL();
if (!url.SchemeIs(content::kChromeUIScheme) || url.host_piece() != page)
return;
// Don't use Reload() since |url| isn't the same as the internal URL that
// NavigationController has.
web_contents->GetController().LoadURL(
url,
content::Referrer::SanitizeForRequest(
url,
content::Referrer(url, network::mojom::ReferrerPolicy::kDefault)),
ui::PAGE_TRANSITION_RELOAD, std::string());
}
enum UpdateBehavior {
UPDATE_DEACTIVATE, // Mark 'active' as false.
UPDATE_REMOVE, // Remove the entry from the list.
};
// Updates the entry (if any) for |override_url| in |overrides_list| according
// to |behavior|. Returns true if anything changed.
bool UpdateOverridesList(base::ListValue* overrides_list,
const std::string& override_url,
UpdateBehavior behavior) {
auto iter = std::find_if(
overrides_list->begin(), overrides_list->end(),
[&override_url](const base::Value& value) {
std::string entry;
const base::DictionaryValue* dict = nullptr;
return value.GetAsDictionary(&dict) &&
dict->GetString(kEntry, &entry) && entry == override_url;
});
if (iter != overrides_list->end()) {
switch (behavior) {
case UPDATE_DEACTIVATE: {
base::DictionaryValue* dict = nullptr;
bool success = iter->GetAsDictionary(&dict);
// See comment about CHECK(success) in ForEachOverrideList.
if (success) {
dict->SetBoolean(kActive, false);
break;
}
// Else fall through and erase the broken pref.
FALLTHROUGH;
}
case UPDATE_REMOVE:
overrides_list->Erase(iter, nullptr);
break;
}
return true;
}
return false;
}
// Updates each list referenced in |overrides| according to |behavior|.
void UpdateOverridesLists(Profile* profile,
const URLOverrides::URLOverrideMap& overrides,
UpdateBehavior behavior) {
if (overrides.empty())
return;
PrefService* prefs = profile->GetPrefs();
DictionaryPrefUpdate update(prefs, ExtensionWebUI::kExtensionURLOverrides);
base::DictionaryValue* all_overrides = update.Get();
for (const auto& page_override_pair : overrides) {
base::ListValue* page_overrides = nullptr;
// If it's being unregistered, it should already be in the list.
if (!all_overrides->GetList(page_override_pair.first, &page_overrides)) {
NOTREACHED();
continue;
}
if (UpdateOverridesList(page_overrides, page_override_pair.second.spec(),
behavior)) {
// This is the active override, so we need to find all existing
// tabs for this override and get them to reload the original URL.
base::Callback<void(WebContents*)> callback =
base::Bind(&UnregisterAndReplaceOverrideForWebContents,
page_override_pair.first, profile);
extensions::ExtensionTabUtil::ForEachTab(callback);
}
}
}
// Run favicon callback with image result. If no favicon was available then
// |image| will be empty.
void RunFaviconCallbackAsync(
const favicon_base::FaviconResultsCallback& callback,
const gfx::Image& image) {
std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results =
new std::vector<favicon_base::FaviconRawBitmapResult>();
const std::vector<gfx::ImageSkiaRep>& image_reps =
image.AsImageSkia().image_reps();
for (size_t i = 0; i < image_reps.size(); ++i) {
const gfx::ImageSkiaRep& image_rep = image_reps[i];
scoped_refptr<base::RefCountedBytes> bitmap_data(
new base::RefCountedBytes());
if (gfx::PNGCodec::EncodeBGRASkBitmap(image_rep.GetBitmap(), false,
&bitmap_data->data())) {
favicon_base::FaviconRawBitmapResult bitmap_result;
bitmap_result.bitmap_data = bitmap_data;
bitmap_result.pixel_size = gfx::Size(image_rep.pixel_width(),
image_rep.pixel_height());
// Leave |bitmap_result|'s icon URL as the default of GURL().
bitmap_result.icon_type = favicon_base::IconType::kFavicon;
favicon_bitmap_results->push_back(bitmap_result);
} else {
NOTREACHED() << "Could not encode extension favicon";
}
}
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(&favicon::FaviconService::FaviconResultsCallbackRunner,
callback, base::Owned(favicon_bitmap_results)));
}
bool ValidateOverrideURL(const base::Value* override_url_value,
const GURL& source_url,
const extensions::ExtensionSet& extensions,
GURL* override_url,
const Extension** extension) {
const base::DictionaryValue* dict = nullptr;
std::string override;
bool is_active = false;
if (!override_url_value || !override_url_value->GetAsDictionary(&dict) ||
!dict->GetBoolean(kActive, &is_active) || !is_active ||
!dict->GetString(kEntry, &override)) {
return false;
}
if (!source_url.query().empty())
override += "?" + source_url.query();
if (!source_url.ref().empty())
override += "#" + source_url.ref();
*override_url = GURL(override);
if (!override_url->is_valid()) {
return false;
}
*extension = extensions.GetByID(override_url->host());
if (!*extension) {
return false;
}
return true;
}
// Fetches each list in the overrides dictionary and runs |callback| on it.
void ForEachOverrideList(
Profile* profile,
const base::Callback<void(base::ListValue*)>& callback) {
PrefService* prefs = profile->GetPrefs();
DictionaryPrefUpdate update(prefs, ExtensionWebUI::kExtensionURLOverrides);
base::DictionaryValue* all_overrides = update.Get();
// DictionaryValue::Iterator cannot be used to modify the list. Generate the
// set of keys instead.
std::vector<std::string> keys;
for (base::DictionaryValue::Iterator iter(*all_overrides);
!iter.IsAtEnd(); iter.Advance()) {
keys.push_back(iter.key());
}
for (const std::string& key : keys) {
base::ListValue* list = nullptr;
bool success = all_overrides->GetList(key, &list);
// In a perfect world, we could CHECK(success) here. Unfortunately, if a
// user's prefs are mangled (by malware, user modification, hard drive
// corruption, evil robots, etc), this will fail. Instead, delete the pref.
if (!success) {
all_overrides->Remove(key, nullptr);
continue;
}
callback.Run(list);
}
}
} // namespace
const char ExtensionWebUI::kExtensionURLOverrides[] =
"extensions.chrome_url_overrides";
// static
void ExtensionWebUI::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(kExtensionURLOverrides);
}
// static
bool ExtensionWebUI::HandleChromeURLOverride(
GURL* url,
content::BrowserContext* browser_context) {
if (!url->SchemeIs(content::kChromeUIScheme))
return false;
Profile* profile = Profile::FromBrowserContext(browser_context);
const base::DictionaryValue* overrides =
profile->GetPrefs()->GetDictionary(kExtensionURLOverrides);
std::string url_host = url->host();
const base::ListValue* url_list = NULL;
if (!overrides || !overrides->GetList(url_host, &url_list))
return false;
extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(browser_context);
const extensions::ExtensionSet& extensions = registry->enabled_extensions();
GURL component_url;
bool found_component_override = false;
// Iterate over the URL list looking for a suitable override. If a
// valid non-component override is encountered it is chosen immediately.
for (size_t i = 0; i < url_list->GetSize(); ++i) {
const base::Value* val = NULL;
url_list->Get(i, &val);
GURL override_url;
const Extension* extension;
if (!ValidateOverrideURL(
val, *url, extensions, &override_url, &extension)) {
// Invalid overrides are cleaned up on startup.
continue;
}
// We can't handle chrome-extension URLs in incognito mode unless the
// extension uses split mode.
bool incognito_override_allowed =
extensions::IncognitoInfo::IsSplitMode(extension) &&
extensions::util::IsIncognitoEnabled(extension->id(), profile);
if (profile->IsOffTheRecord() && !incognito_override_allowed) {
continue;
}
if (!extensions::Manifest::IsComponentLocation(extension->location())) {
*url = override_url;
return true;
}
if (!found_component_override) {
found_component_override = true;
component_url = override_url;
}
}
// If no other non-component overrides were found, use the first known
// component override, if any.
if (found_component_override) {
*url = component_url;
return true;
}
return false;
}
// static
bool ExtensionWebUI::HandleChromeURLOverrideReverse(
GURL* url, content::BrowserContext* browser_context) {
Profile* profile = Profile::FromBrowserContext(browser_context);
const base::DictionaryValue* overrides =
profile->GetPrefs()->GetDictionary(kExtensionURLOverrides);
if (!overrides)
return false;
// Find the reverse mapping based on the given URL. For example this maps the
// internal URL
// chrome-extension://eemcgdkfndhakfknompkggombfjjjeno/main.html#1 to
// chrome://bookmarks/#1 for display in the omnibox.
for (base::DictionaryValue::Iterator dict_iter(*overrides);
!dict_iter.IsAtEnd(); dict_iter.Advance()) {
const base::ListValue* url_list = nullptr;
if (!dict_iter.value().GetAsList(&url_list))
continue;
for (auto list_iter = url_list->begin(); list_iter != url_list->end();
++list_iter) {
const base::DictionaryValue* dict = nullptr;
if (!list_iter->GetAsDictionary(&dict))
continue;
std::string override;
if (!dict->GetString(kEntry, &override))
continue;
if (base::StartsWith(url->spec(), override,
base::CompareCase::SENSITIVE)) {
GURL original_url(content::kChromeUIScheme + std::string("://") +
dict_iter.key() +
url->spec().substr(override.length()));
*url = original_url;
return true;
}
}
}
return false;
}
// static
void ExtensionWebUI::InitializeChromeURLOverrides(Profile* profile) {
ForEachOverrideList(profile, base::Bind(&InitializeOverridesList));
}
// static
void ExtensionWebUI::ValidateChromeURLOverrides(Profile* profile) {
std::unique_ptr<extensions::ExtensionSet> all_extensions =
extensions::ExtensionRegistry::Get(profile)
->GenerateInstalledExtensionsSet();
ForEachOverrideList(profile,
base::Bind(&ValidateOverridesList, all_extensions.get()));
}
// static
void ExtensionWebUI::RegisterOrActivateChromeURLOverrides(
Profile* profile,
const URLOverrides::URLOverrideMap& overrides) {
if (overrides.empty())
return;
PrefService* prefs = profile->GetPrefs();
DictionaryPrefUpdate update(prefs, kExtensionURLOverrides);
base::DictionaryValue* all_overrides = update.Get();
for (const auto& page_override_pair : overrides) {
base::ListValue* page_overrides_weak = nullptr;
if (!all_overrides->GetList(page_override_pair.first,
&page_overrides_weak)) {
auto page_overrides = std::make_unique<base::ListValue>();
page_overrides_weak = page_overrides.get();
all_overrides->Set(page_override_pair.first, std::move(page_overrides));
}
AddOverridesToList(page_overrides_weak, page_override_pair.second);
}
}
// static
void ExtensionWebUI::DeactivateChromeURLOverrides(
Profile* profile,
const URLOverrides::URLOverrideMap& overrides) {
UpdateOverridesLists(profile, overrides, UPDATE_DEACTIVATE);
}
// static
void ExtensionWebUI::UnregisterChromeURLOverrides(
Profile* profile,
const URLOverrides::URLOverrideMap& overrides) {
UpdateOverridesLists(profile, overrides, UPDATE_REMOVE);
}
// static
void ExtensionWebUI::GetFaviconForURL(
Profile* profile,
const GURL& page_url,
const favicon_base::FaviconResultsCallback& callback) {
const Extension* extension = extensions::ExtensionRegistry::Get(
profile)->enabled_extensions().GetByID(page_url.host());
if (!extension) {
RunFaviconCallbackAsync(callback, gfx::Image());
return;
}
// Fetch resources for all supported scale factors for which there are
// resources. Load image reps for all supported scale factors (in addition to
// 1x) immediately instead of in an as needed fashion to be consistent with
// how favicons are requested for chrome:// and page URLs.
const std::vector<float>& favicon_scales = favicon_base::GetFaviconScales();
std::vector<extensions::ImageLoader::ImageRepresentation> info_list;
for (size_t i = 0; i < favicon_scales.size(); ++i) {
float scale = favicon_scales[i];
int pixel_size = static_cast<int>(gfx::kFaviconSize * scale);
extensions::ExtensionResource icon_resource =
extensions::IconsInfo::GetIconResource(extension,
pixel_size,
ExtensionIconSet::MATCH_BIGGER);
ui::ScaleFactor resource_scale_factor = ui::GetSupportedScaleFactor(scale);
if (!icon_resource.empty()) {
info_list.push_back(extensions::ImageLoader::ImageRepresentation(
icon_resource,
extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE,
gfx::Size(pixel_size, pixel_size), resource_scale_factor));
}
}
if (info_list.empty()) {
// Use the placeholder image when no default icon is available.
gfx::Image placeholder_image =
extensions::ExtensionIconPlaceholder::CreateImage(
extension_misc::EXTENSION_ICON_SMALL, extension->name());
gfx::ImageSkia placeholder_skia(placeholder_image.AsImageSkia());
// Ensure the ImageSkia has representation at all scales we would use for
// favicons.
std::vector<ui::ScaleFactor> scale_factors = ui::GetSupportedScaleFactors();
for (const auto& scale_factor : scale_factors) {
placeholder_skia.GetRepresentation(
ui::GetScaleForScaleFactor(scale_factor));
}
RunFaviconCallbackAsync(callback, gfx::Image(placeholder_skia));
} else {
// LoadImagesAsync actually can run callback synchronously. We want to force
// async.
extensions::ImageLoader::Get(profile)->LoadImagesAsync(
extension, info_list,
base::BindOnce(&RunFaviconCallbackAsync, callback));
}
}
|