Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/common/extensions/app_launcher_info.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/lazy_instance.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/common/chrome_switches.h" | |
| 13 #include "chrome/common/extensions/extension_manifest_constants.h" | |
| 14 #include "chrome/common/url_constants.h" | |
| 15 #include "extensions/common/error_utils.h" | |
| 16 | |
| 17 namespace keys = extension_manifest_keys; | |
| 18 namespace values = extension_manifest_values; | |
| 19 namespace errors = extension_manifest_errors; | |
| 20 | |
| 21 namespace extensions { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 bool ReadLaunchDimension(const extensions::Manifest* manifest, | |
| 26 const char* key, | |
| 27 int* target, | |
| 28 bool is_valid_container, | |
| 29 string16* error) { | |
| 30 const Value* temp = NULL; | |
| 31 if (manifest->Get(key, &temp)) { | |
| 32 if (!is_valid_container) { | |
| 33 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 34 errors::kInvalidLaunchValueContainer, | |
| 35 key); | |
| 36 return false; | |
| 37 } | |
| 38 if (!temp->GetAsInteger(target) || *target < 0) { | |
| 39 *target = 0; | |
| 40 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 41 errors::kInvalidLaunchValue, | |
| 42 key); | |
| 43 return false; | |
| 44 } | |
| 45 } | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 49 static base::LazyInstance<AppLauncherInfo> g_empty_app_launch_info = | |
| 50 LAZY_INSTANCE_INITIALIZER; | |
| 51 | |
| 52 const AppLauncherInfo& GetAppLauncherInfo(const Extension* extension) { | |
| 53 AppLauncherInfo* info = static_cast<AppLauncherInfo*>( | |
| 54 extension->GetManifestData(keys::kLaunch)); | |
| 55 return info ? *info : g_empty_app_launch_info.Get(); | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 AppLauncherInfo::AppLauncherInfo() | |
| 61 : launch_container_(extension_misc::LAUNCH_TAB), | |
| 62 launch_width_(0), | |
| 63 launch_height_(0) { | |
| 64 } | |
| 65 | |
| 66 AppLauncherInfo::~AppLauncherInfo() { | |
| 67 } | |
| 68 | |
| 69 // static | |
| 70 const std::string& AppLauncherInfo::GetLaunchLocalPath( | |
| 71 const Extension* extension) { | |
| 72 return GetAppLauncherInfo(extension).launch_local_path_; | |
| 73 } | |
| 74 | |
| 75 // static | |
| 76 const std::string& AppLauncherInfo::GetLaunchWebURL( | |
| 77 const Extension* extension) { | |
| 78 return GetAppLauncherInfo(extension).launch_web_url_; | |
| 79 } | |
| 80 | |
| 81 // static | |
| 82 extension_misc::LaunchContainer AppLauncherInfo::GetLaunchContainer( | |
| 83 const Extension* extension) { | |
| 84 return GetAppLauncherInfo(extension).launch_container_; | |
| 85 } | |
| 86 | |
| 87 // static | |
| 88 int AppLauncherInfo::GetLaunchWidth(const Extension* extension) { | |
| 89 return GetAppLauncherInfo(extension).launch_width_; | |
| 90 } | |
| 91 | |
| 92 // static | |
| 93 int AppLauncherInfo::GetLaunchHeight(const Extension* extension) { | |
| 94 return GetAppLauncherInfo(extension).launch_height_; | |
| 95 } | |
| 96 | |
| 97 // static | |
| 98 GURL AppLauncherInfo::GetFullLaunchURL(const Extension* extension) { | |
| 99 AppLauncherInfo info = GetAppLauncherInfo(extension); | |
|
Devlin
2013/03/08 17:44:21
If this isn't const &, it'll construct a new AppLa
Joe Thomas
2013/03/09 01:59:52
Done.
| |
| 100 if (info.launch_local_path_.empty()) | |
| 101 return GURL(info.launch_web_url_); | |
| 102 else | |
| 103 return extension->url().Resolve(info.launch_local_path_); | |
| 104 } | |
| 105 | |
| 106 bool AppLauncherInfo::Parse(Extension* extension, string16* error) { | |
| 107 if (!LoadLaunchURL(extension, error) || | |
| 108 !LoadLaunchContainer(extension, error)) | |
| 109 return false; | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 bool AppLauncherInfo::LoadLaunchURL(Extension* extension, string16* error) { | |
| 114 const Value* temp = NULL; | |
| 115 | |
| 116 // Launch URL can be either local (to chrome-extension:// root) or an absolute | |
| 117 // web URL. | |
| 118 if (extension->manifest()->Get(keys::kLaunchLocalPath, &temp)) { | |
| 119 if (extension->manifest()->Get(keys::kLaunchWebURL, NULL)) { | |
| 120 *error = ASCIIToUTF16(errors::kLaunchPathAndURLAreExclusive); | |
| 121 return false; | |
| 122 } | |
| 123 | |
| 124 if (extension->manifest()->Get(keys::kWebURLs, NULL)) { | |
| 125 *error = ASCIIToUTF16(errors::kLaunchPathAndExtentAreExclusive); | |
| 126 return false; | |
| 127 } | |
| 128 | |
| 129 std::string launch_path; | |
| 130 if (!temp->GetAsString(&launch_path)) { | |
| 131 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 132 errors::kInvalidLaunchValue, | |
| 133 keys::kLaunchLocalPath); | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 // Ensure the launch path is a valid relative URL. | |
| 138 GURL resolved = extension->url().Resolve(launch_path); | |
| 139 if (!resolved.is_valid() || resolved.GetOrigin() != extension->url()) { | |
| 140 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 141 errors::kInvalidLaunchValue, | |
| 142 keys::kLaunchLocalPath); | |
| 143 return false; | |
| 144 } | |
| 145 | |
| 146 launch_local_path_ = launch_path; | |
| 147 } else if (extension->manifest()->Get(keys::kLaunchWebURL, &temp)) { | |
| 148 std::string launch_url; | |
| 149 if (!temp->GetAsString(&launch_url)) { | |
| 150 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 151 errors::kInvalidLaunchValue, | |
| 152 keys::kLaunchWebURL); | |
| 153 return false; | |
| 154 } | |
| 155 | |
| 156 // Ensure the launch URL is a valid absolute URL and web extent scheme. | |
| 157 GURL url(launch_url); | |
| 158 URLPattern pattern(Extension::kValidWebExtentSchemes); | |
| 159 if (!url.is_valid() || !pattern.SetScheme(url.scheme())) { | |
| 160 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 161 errors::kInvalidLaunchValue, | |
| 162 keys::kLaunchWebURL); | |
| 163 return false; | |
| 164 } | |
| 165 | |
| 166 launch_web_url_ = launch_url; | |
| 167 } else if (extension->is_legacy_packaged_app()) { | |
| 168 *error = ASCIIToUTF16(errors::kLaunchURLRequired); | |
| 169 return false; | |
| 170 } | |
| 171 | |
| 172 // If there is no extent, we default the extent based on the launch URL. | |
| 173 if (extension->web_extent().is_empty() && !launch_web_url_.empty()) { | |
| 174 GURL launch_url(launch_web_url_); | |
| 175 URLPattern pattern(Extension::kValidWebExtentSchemes); | |
| 176 if (!pattern.SetScheme("*")) { | |
| 177 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 178 errors::kInvalidLaunchValue, | |
| 179 keys::kLaunchWebURL); | |
| 180 return false; | |
| 181 } | |
| 182 pattern.SetHost(launch_url.host()); | |
| 183 pattern.SetPath("/*"); | |
| 184 extension->AddWebExtentPattern(pattern); | |
| 185 } | |
| 186 | |
| 187 // In order for the --apps-gallery-url switch to work with the gallery | |
| 188 // process isolation, we must insert any provided value into the component | |
| 189 // app's launch url and web extent. | |
| 190 if (extension->id() == extension_misc::kWebStoreAppId) { | |
| 191 std::string gallery_url_str = CommandLine::ForCurrentProcess()-> | |
| 192 GetSwitchValueASCII(switches::kAppsGalleryURL); | |
| 193 | |
| 194 // Empty string means option was not used. | |
| 195 if (!gallery_url_str.empty()) { | |
| 196 GURL gallery_url(gallery_url_str); | |
| 197 OverrideLaunchURL(extension, gallery_url); | |
| 198 } | |
| 199 } else if (extension->id() == extension_misc::kCloudPrintAppId) { | |
| 200 // In order for the --cloud-print-service switch to work, we must update | |
| 201 // the launch URL and web extent. | |
| 202 // TODO(sanjeevr): Ideally we want to use CloudPrintURL here but that is | |
| 203 // currently under chrome/browser. | |
| 204 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 205 GURL cloud_print_service_url = GURL(command_line.GetSwitchValueASCII( | |
| 206 switches::kCloudPrintServiceURL)); | |
| 207 if (!cloud_print_service_url.is_empty()) { | |
| 208 std::string path( | |
| 209 cloud_print_service_url.path() + "/enable_chrome_connector"); | |
| 210 GURL::Replacements replacements; | |
| 211 replacements.SetPathStr(path); | |
| 212 GURL cloud_print_enable_connector_url = | |
| 213 cloud_print_service_url.ReplaceComponents(replacements); | |
| 214 OverrideLaunchURL(extension, cloud_print_enable_connector_url); | |
| 215 } | |
| 216 } else if (extension->id() == extension_misc::kChromeAppId) { | |
| 217 // Override launch url to new tab. | |
| 218 launch_web_url_ = chrome::kChromeUINewTabURL; | |
| 219 extension->ClearWebExtentPatterns(); | |
| 220 } | |
| 221 | |
| 222 return true; | |
| 223 } | |
| 224 | |
| 225 bool AppLauncherInfo::LoadLaunchContainer(Extension* extension, | |
| 226 string16* error) { | |
| 227 const Value* tmp_launcher_container = NULL; | |
| 228 if (!extension->manifest()->Get(keys::kLaunchContainer, | |
| 229 &tmp_launcher_container)) | |
| 230 return true; | |
| 231 | |
| 232 std::string launch_container_string; | |
| 233 if (!tmp_launcher_container->GetAsString(&launch_container_string)) { | |
| 234 *error = ASCIIToUTF16(errors::kInvalidLaunchContainer); | |
| 235 return false; | |
| 236 } | |
| 237 | |
| 238 if (launch_container_string == values::kLaunchContainerPanel) { | |
| 239 launch_container_ = extension_misc::LAUNCH_PANEL; | |
| 240 } else if (launch_container_string == values::kLaunchContainerTab) { | |
| 241 launch_container_ = extension_misc::LAUNCH_TAB; | |
| 242 } else { | |
| 243 *error = ASCIIToUTF16(errors::kInvalidLaunchContainer); | |
| 244 return false; | |
| 245 } | |
| 246 | |
| 247 bool can_specify_initial_size = | |
| 248 launch_container_ == extension_misc::LAUNCH_PANEL || | |
| 249 launch_container_ == extension_misc::LAUNCH_WINDOW; | |
| 250 | |
| 251 // Validate the container width if present. | |
| 252 if (!ReadLaunchDimension(extension->manifest(), | |
| 253 keys::kLaunchWidth, | |
| 254 &launch_width_, | |
| 255 can_specify_initial_size, | |
| 256 error)) { | |
| 257 return false; | |
| 258 } | |
| 259 | |
| 260 // Validate container height if present. | |
| 261 if (!ReadLaunchDimension(extension->manifest(), | |
| 262 keys::kLaunchHeight, | |
| 263 &launch_height_, | |
| 264 can_specify_initial_size, | |
| 265 error)) { | |
| 266 return false; | |
| 267 } | |
| 268 | |
| 269 return true; | |
| 270 } | |
| 271 | |
| 272 void AppLauncherInfo::OverrideLaunchURL(Extension* extension, | |
| 273 GURL override_url) { | |
| 274 if (!override_url.is_valid()) { | |
| 275 DLOG(WARNING) << "Invalid override url given for " << extension->name(); | |
| 276 } else { | |
| 277 if (override_url.has_port()) { | |
| 278 DLOG(WARNING) << "Override URL passed for " << extension->name() | |
| 279 << " should not contain a port. Removing it."; | |
| 280 | |
| 281 GURL::Replacements remove_port; | |
| 282 remove_port.ClearPort(); | |
| 283 override_url = override_url.ReplaceComponents(remove_port); | |
| 284 } | |
| 285 | |
| 286 launch_web_url_ = override_url.spec(); | |
| 287 | |
| 288 URLPattern pattern(Extension::kValidWebExtentSchemes); | |
| 289 URLPattern::ParseResult result = pattern.Parse(override_url.spec()); | |
| 290 DCHECK_EQ(result, URLPattern::PARSE_SUCCESS); | |
| 291 pattern.SetPath(pattern.path() + '*'); | |
| 292 extension->AddWebExtentPattern(pattern); | |
| 293 } | |
| 294 } | |
| 295 | |
| 296 AppLaunchManifestHandler::AppLaunchManifestHandler() { | |
| 297 } | |
| 298 | |
| 299 AppLaunchManifestHandler::~AppLaunchManifestHandler() { | |
| 300 } | |
| 301 | |
| 302 bool AppLaunchManifestHandler::Parse(Extension* extension, string16* error) { | |
| 303 scoped_ptr<AppLauncherInfo> info(new AppLauncherInfo); | |
| 304 if (!info->Parse(extension, error)) | |
| 305 return false; | |
| 306 extension->SetManifestData(keys::kLaunch, info.release()); | |
| 307 return true; | |
| 308 } | |
| 309 | |
| 310 bool AppLaunchManifestHandler::AlwaysParseForType(Manifest::Type type) { | |
| 311 return type == Manifest::TYPE_LEGACY_PACKAGED_APP; | |
| 312 } | |
| 313 | |
| 314 const std::vector<std::string> AppLaunchManifestHandler::Keys() const { | |
| 315 static const char* keys[] = { | |
| 316 keys::kLaunchLocalPath, | |
| 317 keys::kLaunchWebURL, | |
| 318 keys::kLaunchContainer, | |
| 319 keys::kLaunchHeight, | |
| 320 keys::kLaunchWidth | |
| 321 }; | |
| 322 return std::vector<std::string>(keys, keys + arraysize(keys)); | |
| 323 } | |
| 324 | |
| 325 } // namespace extensions | |
| OLD | NEW |