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_launch_manifest_handler.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/extensions/manifest.h" | |
| 15 #include "chrome/common/url_constants.h" | |
| 16 #include "extensions/common/error_utils.h" | |
| 17 | |
| 18 namespace extensions { | |
| 19 | |
| 20 namespace keys = extension_manifest_keys; | |
| 21 namespace values = extension_manifest_values; | |
| 22 namespace errors = extension_manifest_errors; | |
|
Yoyo Zhou
2013/02/27 02:43:33
nit: I think it's more common for these to be decl
Joe Thomas
2013/03/01 23:26:57
Done.
| |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 bool ReadLaunchDimension(const extensions::Manifest* manifest, | |
| 27 const char* key, | |
| 28 int* target, | |
| 29 bool is_valid_container, | |
| 30 string16* error) { | |
| 31 Value* temp = NULL; | |
| 32 if (manifest->Get(key, &temp)) { | |
| 33 if (!is_valid_container) { | |
| 34 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 35 errors::kInvalidLaunchValueContainer, | |
| 36 key); | |
| 37 return false; | |
| 38 } | |
| 39 if (!temp->GetAsInteger(target) || *target < 0) { | |
| 40 *target = 0; | |
| 41 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 42 errors::kInvalidLaunchValue, | |
| 43 key); | |
| 44 return false; | |
| 45 } | |
| 46 } | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 static base::LazyInstance<AppLaunchInfo> g_empty_app_launch_info = | |
| 51 LAZY_INSTANCE_INITIALIZER; | |
| 52 | |
| 53 const AppLaunchInfo& GetAppLaunchInfo(const Extension* extension) { | |
| 54 AppLaunchInfo* info = static_cast<AppLaunchInfo*>( | |
| 55 extension->GetManifestData(keys::kLaunch)); | |
| 56 return info ? *info : g_empty_app_launch_info.Get(); | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 AppLaunchInfo::AppLaunchInfo() | |
| 62 : launch_container_(extension_misc::LAUNCH_TAB), | |
| 63 launch_width_(0), | |
| 64 launch_height_(0) { | |
| 65 } | |
| 66 | |
| 67 AppLaunchInfo::~AppLaunchInfo() { | |
| 68 } | |
| 69 | |
| 70 // static | |
| 71 const std::string& AppLaunchInfo::GetLaunchLocalPath( | |
| 72 const Extension* extension) { | |
| 73 return GetAppLaunchInfo(extension).launch_local_path_; | |
| 74 } | |
| 75 | |
| 76 // static | |
| 77 const std::string& AppLaunchInfo::GetLaunchWebUrl(const Extension* extension) { | |
| 78 return GetAppLaunchInfo(extension).launch_web_url_; | |
| 79 } | |
| 80 | |
| 81 // static | |
| 82 extension_misc::LaunchContainer AppLaunchInfo::GetLaunchContainer( | |
| 83 const Extension* extension) { | |
| 84 return GetAppLaunchInfo(extension).launch_container_; | |
| 85 } | |
| 86 | |
| 87 // static | |
| 88 int AppLaunchInfo::GetLaunchWidth(const Extension* extension) { | |
| 89 return GetAppLaunchInfo(extension).launch_width_; | |
| 90 } | |
| 91 | |
| 92 // static | |
| 93 int AppLaunchInfo::GetLaunchHeight(const Extension* extension) { | |
| 94 return GetAppLaunchInfo(extension).launch_height_; | |
| 95 } | |
| 96 | |
| 97 // static | |
| 98 GURL AppLaunchInfo::GetFullLaunchURL(const Extension* extension) { | |
| 99 AppLaunchInfo info = GetAppLaunchInfo(extension); | |
| 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 AppLaunchInfo::Parse(Extension* extension, string16* error) { | |
| 107 if (!LoadLaunchURL(extension, error) || | |
| 108 !LoadLaunchContainer(extension, error)) | |
| 109 return false; | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 bool AppLaunchInfo::LoadLaunchURL(Extension* extension, string16* error) { | |
| 114 Value* temp = NULL; | |
| 115 | |
| 116 // launch URL can be either local (to chrome-extension:// root) or an absolute | |
|
Devlin
2013/02/27 19:12:48
nit: capitalize 'l' at the beginning of the senten
Joe Thomas
2013/03/01 23:26:57
Done.
| |
| 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 extension->is_hosted_app()) { | |
| 169 *error = ASCIIToUTF16(errors::kLaunchURLRequired); | |
| 170 return false; | |
| 171 } | |
| 172 | |
| 173 // If there is no extent, we default the extent based on the launch URL. | |
| 174 if (extension->web_extent().is_empty() && !launch_web_url_.empty()) { | |
| 175 GURL launch_url(launch_web_url_); | |
| 176 URLPattern pattern(Extension::kValidWebExtentSchemes); | |
| 177 if (!pattern.SetScheme("*")) { | |
| 178 *error = ErrorUtils::FormatErrorMessageUTF16( | |
| 179 errors::kInvalidLaunchValue, | |
| 180 keys::kLaunchWebURL); | |
| 181 return false; | |
| 182 } | |
| 183 pattern.SetHost(launch_url.host()); | |
| 184 pattern.SetPath("/*"); | |
| 185 extension->AddWebExtentPattern(pattern); | |
|
Yoyo Zhou
2013/02/27 02:43:33
Web extents should also be ManifestData. You can a
Joe Thomas
2013/03/01 23:26:57
I will do it in a separate CL. Added a TODO in ext
| |
| 186 } | |
| 187 | |
| 188 // In order for the --apps-gallery-url switch to work with the gallery | |
| 189 // process isolation, we must insert any provided value into the component | |
| 190 // app's launch url and web extent. | |
| 191 if (extension->id() == extension_misc::kWebStoreAppId) { | |
| 192 std::string gallery_url_str = CommandLine::ForCurrentProcess()-> | |
| 193 GetSwitchValueASCII(switches::kAppsGalleryURL); | |
| 194 | |
| 195 // Empty string means option was not used. | |
| 196 if (!gallery_url_str.empty()) { | |
| 197 GURL gallery_url(gallery_url_str); | |
| 198 OverrideLaunchUrl(extension, gallery_url); | |
| 199 } | |
| 200 } else if (extension->id() == extension_misc::kCloudPrintAppId) { | |
| 201 // In order for the --cloud-print-service switch to work, we must update | |
| 202 // the launch URL and web extent. | |
| 203 // TODO(sanjeevr): Ideally we want to use CloudPrintURL here but that is | |
| 204 // currently under chrome/browser. | |
| 205 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 206 GURL cloud_print_service_url = GURL(command_line.GetSwitchValueASCII( | |
| 207 switches::kCloudPrintServiceURL)); | |
| 208 if (!cloud_print_service_url.is_empty()) { | |
| 209 std::string path( | |
| 210 cloud_print_service_url.path() + "/enable_chrome_connector"); | |
| 211 GURL::Replacements replacements; | |
| 212 replacements.SetPathStr(path); | |
| 213 GURL cloud_print_enable_connector_url = | |
| 214 cloud_print_service_url.ReplaceComponents(replacements); | |
| 215 OverrideLaunchUrl(extension, cloud_print_enable_connector_url); | |
| 216 } | |
| 217 } else if (extension->id() == extension_misc::kChromeAppId) { | |
| 218 // Override launch url to new tab. | |
| 219 launch_web_url_ = chrome::kChromeUINewTabURL; | |
| 220 extension->ClearWebExtentPatterns(); | |
| 221 } | |
| 222 | |
| 223 return true; | |
| 224 } | |
| 225 | |
| 226 bool AppLaunchInfo::LoadLaunchContainer(Extension* extension, | |
| 227 string16* error) { | |
| 228 Value* tmp_launcher_container = NULL; | |
| 229 if (!extension->manifest()->Get(keys::kLaunchContainer, | |
| 230 &tmp_launcher_container)) | |
| 231 return true; | |
| 232 | |
| 233 std::string launch_container_string; | |
| 234 if (!tmp_launcher_container->GetAsString(&launch_container_string)) { | |
| 235 *error = ASCIIToUTF16(errors::kInvalidLaunchContainer); | |
| 236 return false; | |
| 237 } | |
| 238 | |
| 239 if (launch_container_string == values::kLaunchContainerPanel) { | |
| 240 launch_container_ = extension_misc::LAUNCH_PANEL; | |
| 241 } else if (launch_container_string == values::kLaunchContainerTab) { | |
| 242 launch_container_ = extension_misc::LAUNCH_TAB; | |
| 243 } else { | |
| 244 *error = ASCIIToUTF16(errors::kInvalidLaunchContainer); | |
| 245 return false; | |
| 246 } | |
| 247 | |
| 248 bool can_specify_initial_size = | |
| 249 launch_container_ == extension_misc::LAUNCH_PANEL || | |
| 250 launch_container_ == extension_misc::LAUNCH_WINDOW; | |
| 251 | |
| 252 // Validate the container width if present. | |
| 253 if (!ReadLaunchDimension(extension->manifest(), | |
| 254 keys::kLaunchWidth, | |
| 255 &launch_width_, | |
| 256 can_specify_initial_size, | |
| 257 error)) { | |
| 258 return false; | |
| 259 } | |
| 260 | |
| 261 // Validate container height if present. | |
| 262 if (!ReadLaunchDimension(extension->manifest(), | |
| 263 keys::kLaunchHeight, | |
| 264 &launch_height_, | |
| 265 can_specify_initial_size, | |
| 266 error)) { | |
| 267 return false; | |
| 268 } | |
| 269 | |
| 270 return true; | |
| 271 } | |
| 272 | |
| 273 void AppLaunchInfo::OverrideLaunchUrl(Extension* extension, | |
| 274 const GURL& override_url) { | |
| 275 GURL new_url(override_url); | |
|
Devlin
2013/02/27 19:12:48
It strikes me that if want a copy of override url
Joe Thomas
2013/03/01 23:26:57
Done.
| |
| 276 if (!new_url.is_valid()) { | |
| 277 DLOG(WARNING) << "Invalid override url given for " << extension->name(); | |
| 278 } else { | |
| 279 if (new_url.has_port()) { | |
| 280 DLOG(WARNING) << "Override URL passed for " << extension->name() | |
| 281 << " should not contain a port. Removing it."; | |
| 282 | |
| 283 GURL::Replacements remove_port; | |
| 284 remove_port.ClearPort(); | |
| 285 new_url = new_url.ReplaceComponents(remove_port); | |
| 286 } | |
| 287 | |
| 288 launch_web_url_ = new_url.spec(); | |
| 289 | |
| 290 URLPattern pattern(Extension::kValidWebExtentSchemes); | |
| 291 URLPattern::ParseResult result = pattern.Parse(new_url.spec()); | |
| 292 DCHECK_EQ(result, URLPattern::PARSE_SUCCESS); | |
| 293 pattern.SetPath(pattern.path() + '*'); | |
| 294 extension->AddWebExtentPattern(pattern); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 AppLaunchManifestHandler::AppLaunchManifestHandler() { | |
| 299 } | |
| 300 | |
| 301 AppLaunchManifestHandler::~AppLaunchManifestHandler() { | |
| 302 } | |
| 303 | |
| 304 bool AppLaunchManifestHandler::Parse(Extension* extension, string16* error) { | |
| 305 scoped_ptr<AppLaunchInfo> info(new AppLaunchInfo); | |
| 306 if (!info->Parse(extension, error)) | |
| 307 return false; | |
| 308 extension->SetManifestData(keys::kLaunch, info.release()); | |
| 309 return true; | |
| 310 } | |
| 311 | |
| 312 bool AppLaunchManifestHandler::AlwaysParseForType(Manifest::Type type) { | |
|
Yoyo Zhou
2013/02/27 02:43:33
I'm not sure this is necessary.
Joe Thomas
2013/03/01 23:26:57
This is necessary for TYPE_LEGACY_PACKAGED_APP. Wh
Yoyo Zhou
2013/03/05 20:17:10
I see. Slightly wacky but ok.
| |
| 313 return type == Manifest::TYPE_HOSTED_APP || | |
| 314 type == Manifest::TYPE_LEGACY_PACKAGED_APP; | |
| 315 } | |
| 316 | |
| 317 // static | |
| 318 const std::vector<std::string> AppLaunchManifestHandler::keys() { | |
| 319 static const char* keys[] = { | |
| 320 keys::kLaunchLocalPath, | |
| 321 keys::kLaunchWebURL, | |
| 322 keys::kLaunchContainer, | |
| 323 keys::kLaunchHeight, | |
| 324 keys::kLaunchWidth | |
|
Yoyo Zhou
2013/02/27 02:43:33
kWebURLs should probably be added to this list.
Joe Thomas
2013/03/01 23:26:57
I will do it in a separate CL.
Yoyo Zhou
2013/03/05 20:17:10
Ah, never mind. It should be associated with exten
| |
| 325 }; | |
| 326 return std::vector<std::string>(keys, keys + arraysize(keys)); | |
| 327 } | |
| 328 | |
| 329 } // namespace extensions | |
| OLD | NEW |