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
|
// 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/common/extensions/manifest_handlers/app_launch_info.h"
#include <memory>
#include "base/lazy_instance.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/url_constants.h"
#include "components/app_constants/constants.h"
#include "extensions/common/constants.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/manifest_constants.h"
namespace extensions {
namespace keys = manifest_keys;
namespace values = manifest_values;
namespace errors = manifest_errors;
namespace {
bool ReadLaunchDimension(const extensions::Manifest* manifest,
const char* key,
int* target,
bool is_valid_container,
std::u16string* error) {
if (const base::Value* temp = manifest->FindPath(key)) {
if (!is_valid_container) {
*error = ErrorUtils::FormatErrorMessageUTF16(
errors::kInvalidLaunchValueContainer,
key);
return false;
}
if (!temp->is_int() || temp->GetInt() < 0) {
*target = 0;
*error = ErrorUtils::FormatErrorMessageUTF16(
errors::kInvalidLaunchValue,
key);
return false;
}
*target = temp->GetInt();
}
return true;
}
static base::LazyInstance<AppLaunchInfo>::DestructorAtExit
g_empty_app_launch_info = LAZY_INSTANCE_INITIALIZER;
const AppLaunchInfo& GetAppLaunchInfo(const Extension* extension) {
AppLaunchInfo* info = static_cast<AppLaunchInfo*>(
extension->GetManifestData(keys::kLaunch));
return info ? *info : g_empty_app_launch_info.Get();
}
} // namespace
AppLaunchInfo::AppLaunchInfo() = default;
AppLaunchInfo::~AppLaunchInfo() = default;
// static
const std::string& AppLaunchInfo::GetLaunchLocalPath(
const Extension* extension) {
return GetAppLaunchInfo(extension).launch_local_path_;
}
// static
const GURL& AppLaunchInfo::GetLaunchWebURL(const Extension* extension) {
return GetAppLaunchInfo(extension).launch_web_url_;
}
// static
apps::LaunchContainer AppLaunchInfo::GetLaunchContainer(
const Extension* extension) {
return GetAppLaunchInfo(extension).launch_container_;
}
// static
int AppLaunchInfo::GetLaunchWidth(const Extension* extension) {
return GetAppLaunchInfo(extension).launch_width_;
}
// static
int AppLaunchInfo::GetLaunchHeight(const Extension* extension) {
return GetAppLaunchInfo(extension).launch_height_;
}
// static
GURL AppLaunchInfo::GetFullLaunchURL(const Extension* extension) {
const AppLaunchInfo& info = GetAppLaunchInfo(extension);
if (info.launch_local_path_.empty()) {
return info.launch_web_url_;
} else {
return extension->ResolveExtensionURL(info.launch_local_path_);
}
}
bool AppLaunchInfo::Parse(Extension* extension, std::u16string* error) {
if (!LoadLaunchURL(extension, error) ||
!LoadLaunchContainer(extension, error))
return false;
return true;
}
bool AppLaunchInfo::LoadLaunchURL(Extension* extension, std::u16string* error) {
// Launch URL can be either local (to chrome-extension:// root) or an absolute
// web URL.
if (const base::Value* temp =
extension->manifest()->FindPath(keys::kLaunchLocalPath);
temp) {
if (extension->manifest()->FindPath(keys::kLaunchWebURL)) {
*error = errors::kLaunchPathAndURLAreExclusive;
return false;
}
if (extension->manifest()->FindPath(keys::kWebURLs)) {
*error = errors::kLaunchPathAndExtentAreExclusive;
return false;
}
if (!temp->is_string()) {
*error = ErrorUtils::FormatErrorMessageUTF16(
errors::kInvalidLaunchValue,
keys::kLaunchLocalPath);
return false;
}
const std::string launch_path = temp->GetString();
// Ensure the launch path is a valid relative URL.
GURL resolved = extension->ResolveExtensionURL(launch_path);
if (!resolved.is_valid()) {
*error = ErrorUtils::FormatErrorMessageUTF16(
errors::kInvalidLaunchValue,
keys::kLaunchLocalPath);
return false;
}
launch_local_path_ = launch_path;
} else if (temp = extension->manifest()->FindPath(keys::kLaunchWebURL);
temp) {
if (!temp->is_string()) {
*error = ErrorUtils::FormatErrorMessageUTF16(
errors::kInvalidLaunchValue,
keys::kLaunchWebURL);
return false;
}
auto set_launch_web_url_error = [&]() {
*error = ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidLaunchValue,
keys::kLaunchWebURL);
};
// Ensure the launch web URL is a valid absolute URL and web extent scheme.
GURL url(temp->GetString());
if (!url.is_valid()) {
set_launch_web_url_error();
return false;
}
URLPattern pattern(Extension::kValidWebExtentSchemes);
if (!pattern.IsValidScheme(url.scheme())) {
set_launch_web_url_error();
return false;
}
launch_web_url_ = url;
} else if (extension->is_legacy_packaged_app()) {
*error = errors::kLaunchURLRequired;
return false;
}
// For the Chrome component app, override launch url to new tab.
if (extension->id() == app_constants::kChromeAppId) {
launch_web_url_ = GURL(chrome::kChromeUINewTabURL);
return true;
}
// If there is no extent, we default the extent based on the launch URL.
if (extension->web_extent().is_empty() && !launch_web_url_.is_empty()) {
URLPattern pattern(Extension::kValidWebExtentSchemes);
if (!pattern.SetScheme("*")) {
*error = ErrorUtils::FormatErrorMessageUTF16(
errors::kInvalidLaunchValue,
keys::kLaunchWebURL);
return false;
}
pattern.SetHost(launch_web_url_.host());
pattern.SetPath("/*");
extension->AddWebExtentPattern(pattern);
}
return true;
}
bool AppLaunchInfo::LoadLaunchContainer(Extension* extension,
std::u16string* error) {
const base::Value* tmp_launcher_container =
extension->manifest()->FindPath(keys::kLaunchContainer);
if (tmp_launcher_container == nullptr)
return true;
if (!tmp_launcher_container->is_string()) {
*error = errors::kInvalidLaunchContainer;
return false;
}
const std::string launch_container_string =
tmp_launcher_container->GetString();
if (launch_container_string == values::kLaunchContainerPanelDeprecated) {
launch_container_ = apps::LaunchContainer::kLaunchContainerPanelDeprecated;
} else if (launch_container_string == values::kLaunchContainerTab) {
launch_container_ = apps::LaunchContainer::kLaunchContainerTab;
} else {
*error = errors::kInvalidLaunchContainer;
return false;
}
// TODO(manucornet): Remove this special behavior now that panels are
// deprecated.
bool can_specify_initial_size =
launch_container_ ==
apps::LaunchContainer::kLaunchContainerPanelDeprecated;
// Validate the container width if present.
if (!ReadLaunchDimension(extension->manifest(),
keys::kLaunchWidth,
&launch_width_,
can_specify_initial_size,
error)) {
return false;
}
// Validate container height if present.
if (!ReadLaunchDimension(extension->manifest(),
keys::kLaunchHeight,
&launch_height_,
can_specify_initial_size,
error)) {
return false;
}
return true;
}
void AppLaunchInfo::OverrideLaunchURL(Extension* extension,
GURL override_url) {
if (!override_url.is_valid()) {
DLOG(WARNING) << "Invalid override url given for " << extension->name();
return;
}
if (override_url.has_port()) {
DLOG(WARNING) << "Override URL passed for " << extension->name()
<< " should not contain a port. Removing it.";
GURL::Replacements remove_port;
remove_port.ClearPort();
override_url = override_url.ReplaceComponents(remove_port);
}
launch_web_url_ = override_url;
URLPattern pattern(Extension::kValidWebExtentSchemes);
URLPattern::ParseResult result = pattern.Parse(override_url.spec());
DCHECK_EQ(result, URLPattern::ParseResult::kSuccess);
pattern.SetPath(pattern.path() + '*');
extension->AddWebExtentPattern(pattern);
}
AppLaunchManifestHandler::AppLaunchManifestHandler() = default;
AppLaunchManifestHandler::~AppLaunchManifestHandler() = default;
bool AppLaunchManifestHandler::Parse(Extension* extension,
std::u16string* error) {
std::unique_ptr<AppLaunchInfo> info(new AppLaunchInfo);
if (!info->Parse(extension, error))
return false;
extension->SetManifestData(keys::kLaunch, std::move(info));
return true;
}
bool AppLaunchManifestHandler::AlwaysParseForType(Manifest::Type type) const {
return type == Manifest::TYPE_LEGACY_PACKAGED_APP;
}
base::span<const char* const> AppLaunchManifestHandler::Keys() const {
static constexpr const char* kKeys[] = {
keys::kLaunchLocalPath, keys::kLaunchWebURL, keys::kLaunchContainer,
keys::kLaunchHeight, keys::kLaunchWidth};
return kKeys;
}
} // namespace extensions
|