| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/common/manifest_handlers/background_info.h" | 5 #include "extensions/common/manifest_handlers/background_info.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "content/public/common/content_switches.h" | |
| 14 #include "extensions/common/constants.h" | 13 #include "extensions/common/constants.h" |
| 15 #include "extensions/common/error_utils.h" | 14 #include "extensions/common/error_utils.h" |
| 16 #include "extensions/common/file_util.h" | 15 #include "extensions/common/file_util.h" |
| 17 #include "extensions/common/manifest_constants.h" | 16 #include "extensions/common/manifest_constants.h" |
| 18 #include "extensions/common/manifest_handlers/permissions_parser.h" | 17 #include "extensions/common/manifest_handlers/permissions_parser.h" |
| 19 #include "extensions/common/permissions/api_permission_set.h" | 18 #include "extensions/common/permissions/api_permission_set.h" |
| 20 #include "extensions/common/switches.h" | 19 #include "extensions/common/switches.h" |
| 21 #include "grit/extensions_strings.h" | 20 #include "grit/extensions_strings.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 23 | 22 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 return extension->GetResourceURL(kGeneratedBackgroundPageFilename); | 62 return extension->GetResourceURL(kGeneratedBackgroundPageFilename); |
| 64 } | 63 } |
| 65 | 64 |
| 66 // static | 65 // static |
| 67 const std::vector<std::string>& BackgroundInfo::GetBackgroundScripts( | 66 const std::vector<std::string>& BackgroundInfo::GetBackgroundScripts( |
| 68 const Extension* extension) { | 67 const Extension* extension) { |
| 69 return GetBackgroundInfo(extension).background_scripts_; | 68 return GetBackgroundInfo(extension).background_scripts_; |
| 70 } | 69 } |
| 71 | 70 |
| 72 // static | 71 // static |
| 73 const std::string& BackgroundInfo::GetServiceWorkerScript( | |
| 74 const Extension* extension) { | |
| 75 return GetBackgroundInfo(extension).service_worker_script_; | |
| 76 } | |
| 77 | |
| 78 // static | |
| 79 bool BackgroundInfo::HasBackgroundPage(const Extension* extension) { | 72 bool BackgroundInfo::HasBackgroundPage(const Extension* extension) { |
| 80 return GetBackgroundInfo(extension).has_background_page(); | 73 return GetBackgroundInfo(extension).has_background_page(); |
| 81 } | 74 } |
| 82 | 75 |
| 83 // static | 76 // static |
| 84 bool BackgroundInfo::HasPersistentBackgroundPage(const Extension* extension) { | 77 bool BackgroundInfo::HasPersistentBackgroundPage(const Extension* extension) { |
| 85 return GetBackgroundInfo(extension).has_persistent_background_page(); | 78 return GetBackgroundInfo(extension).has_persistent_background_page(); |
| 86 } | 79 } |
| 87 | 80 |
| 88 // static | 81 // static |
| 89 bool BackgroundInfo::HasLazyBackgroundPage(const Extension* extension) { | 82 bool BackgroundInfo::HasLazyBackgroundPage(const Extension* extension) { |
| 90 return GetBackgroundInfo(extension).has_lazy_background_page(); | 83 return GetBackgroundInfo(extension).has_lazy_background_page(); |
| 91 } | 84 } |
| 92 | 85 |
| 93 // static | 86 // static |
| 94 bool BackgroundInfo::HasGeneratedBackgroundPage(const Extension* extension) { | 87 bool BackgroundInfo::HasGeneratedBackgroundPage(const Extension* extension) { |
| 95 const BackgroundInfo& info = GetBackgroundInfo(extension); | 88 const BackgroundInfo& info = GetBackgroundInfo(extension); |
| 96 return !info.background_scripts_.empty(); | 89 return !info.background_scripts_.empty(); |
| 97 } | 90 } |
| 98 | 91 |
| 99 // static | 92 // static |
| 100 bool BackgroundInfo::HasServiceWorker(const Extension* extension) { | |
| 101 return GetBackgroundInfo(extension).has_service_worker(); | |
| 102 } | |
| 103 | |
| 104 // static | |
| 105 bool BackgroundInfo::AllowJSAccess(const Extension* extension) { | 93 bool BackgroundInfo::AllowJSAccess(const Extension* extension) { |
| 106 return GetBackgroundInfo(extension).allow_js_access_; | 94 return GetBackgroundInfo(extension).allow_js_access_; |
| 107 } | 95 } |
| 108 | 96 |
| 109 bool BackgroundInfo::Parse(const Extension* extension, base::string16* error) { | 97 bool BackgroundInfo::Parse(const Extension* extension, base::string16* error) { |
| 110 const std::string& bg_scripts_key = extension->is_platform_app() ? | 98 const std::string& bg_scripts_key = extension->is_platform_app() ? |
| 111 keys::kPlatformAppBackgroundScripts : keys::kBackgroundScripts; | 99 keys::kPlatformAppBackgroundScripts : keys::kBackgroundScripts; |
| 112 const std::string& sw_scripts_key = | 100 if (!LoadBackgroundScripts(extension, bg_scripts_key, error) || |
| 113 extension->is_platform_app() | |
| 114 ? keys::kPlatformAppServiceWorkerScript | |
| 115 : ""; // TODO(scheib): Support extensions crbug.com/346885 | |
| 116 if (!LoadServiceWorkerScript(extension, sw_scripts_key, error) || | |
| 117 !LoadBackgroundScripts(extension, bg_scripts_key, error) || | |
| 118 !LoadBackgroundPage(extension, error) || | 101 !LoadBackgroundPage(extension, error) || |
| 119 !LoadBackgroundPersistent(extension, error) || | 102 !LoadBackgroundPersistent(extension, error) || |
| 120 !LoadAllowJSAccess(extension, error)) { | 103 !LoadAllowJSAccess(extension, error)) { |
| 121 return false; | 104 return false; |
| 122 } | 105 } |
| 123 | 106 |
| 124 int background_solution_sum = (background_url_.is_valid() ? 1 : 0) + | 107 int background_solution_sum = (background_url_.is_valid() ? 1 : 0) + |
| 125 (!background_scripts_.empty() ? 1 : 0) + | 108 (!background_scripts_.empty() ? 1 : 0); |
| 126 (has_service_worker() ? 1 : 0); | |
| 127 if (background_solution_sum > 1) { | 109 if (background_solution_sum > 1) { |
| 128 *error = ASCIIToUTF16(errors::kInvalidBackgroundCombination); | 110 *error = ASCIIToUTF16(errors::kInvalidBackgroundCombination); |
| 129 return false; | 111 return false; |
| 130 } | 112 } |
| 131 | 113 |
| 132 return true; | 114 return true; |
| 133 } | 115 } |
| 134 | 116 |
| 135 bool BackgroundInfo::LoadServiceWorkerScript(const Extension* extension, | |
| 136 const std::string& key, | |
| 137 base::string16* error) { | |
| 138 const base::Value* service_worker_script_value = NULL; | |
| 139 if (!extension->manifest()->Get(key, &service_worker_script_value)) | |
| 140 return true; | |
| 141 | |
| 142 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 143 ::switches::kEnableExperimentalWebPlatformFeatures)) { | |
| 144 *error = ASCIIToUTF16(errors::kServiceWorkerRequiresFlag); | |
| 145 return false; | |
| 146 } | |
| 147 | |
| 148 CHECK(service_worker_script_value); | |
| 149 if (!service_worker_script_value->GetAsString(&service_worker_script_)) { | |
| 150 *error = ASCIIToUTF16(errors::kInvalidServiceWorkerScript); | |
| 151 return false; | |
| 152 } | |
| 153 return true; | |
| 154 } | |
| 155 | |
| 156 bool BackgroundInfo::LoadBackgroundScripts(const Extension* extension, | 117 bool BackgroundInfo::LoadBackgroundScripts(const Extension* extension, |
| 157 const std::string& key, | 118 const std::string& key, |
| 158 base::string16* error) { | 119 base::string16* error) { |
| 159 const base::Value* background_scripts_value = NULL; | 120 const base::Value* background_scripts_value = NULL; |
| 160 if (!extension->manifest()->Get(key, &background_scripts_value)) | 121 if (!extension->manifest()->Get(key, &background_scripts_value)) |
| 161 return true; | 122 return true; |
| 162 | 123 |
| 163 CHECK(background_scripts_value); | 124 CHECK(background_scripts_value); |
| 164 if (background_scripts_value->GetType() != base::Value::TYPE_LIST) { | 125 if (background_scripts_value->GetType() != base::Value::TYPE_LIST) { |
| 165 *error = ASCIIToUTF16(errors::kInvalidBackgroundScripts); | 126 *error = ASCIIToUTF16(errors::kInvalidBackgroundScripts); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 | 243 |
| 283 BackgroundManifestHandler::~BackgroundManifestHandler() { | 244 BackgroundManifestHandler::~BackgroundManifestHandler() { |
| 284 } | 245 } |
| 285 | 246 |
| 286 bool BackgroundManifestHandler::Parse(Extension* extension, | 247 bool BackgroundManifestHandler::Parse(Extension* extension, |
| 287 base::string16* error) { | 248 base::string16* error) { |
| 288 scoped_ptr<BackgroundInfo> info(new BackgroundInfo); | 249 scoped_ptr<BackgroundInfo> info(new BackgroundInfo); |
| 289 if (!info->Parse(extension, error)) | 250 if (!info->Parse(extension, error)) |
| 290 return false; | 251 return false; |
| 291 | 252 |
| 292 // Platform apps must have background pages or service workers. | 253 // Platform apps must have background pages. |
| 293 if (extension->is_platform_app() && !info->has_background_page() && | 254 if (extension->is_platform_app() && !info->has_background_page()) { |
| 294 !info->has_service_worker()) { | |
| 295 *error = ASCIIToUTF16(errors::kBackgroundRequiredForPlatformApps); | 255 *error = ASCIIToUTF16(errors::kBackgroundRequiredForPlatformApps); |
| 296 return false; | 256 return false; |
| 297 } | 257 } |
| 298 // Lazy background pages are incompatible with the webRequest API. | 258 // Lazy background pages are incompatible with the webRequest API. |
| 299 if (info->has_lazy_background_page() && | 259 if (info->has_lazy_background_page() && |
| 300 PermissionsParser::HasAPIPermission(extension, | 260 PermissionsParser::HasAPIPermission(extension, |
| 301 APIPermission::kWebRequest)) { | 261 APIPermission::kWebRequest)) { |
| 302 *error = ASCIIToUTF16(errors::kWebRequestConflictsWithLazyBackground); | 262 *error = ASCIIToUTF16(errors::kWebRequestConflictsWithLazyBackground); |
| 303 return false; | 263 return false; |
| 304 } | 264 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 } | 301 } |
| 342 } | 302 } |
| 343 return true; | 303 return true; |
| 344 } | 304 } |
| 345 | 305 |
| 346 bool BackgroundManifestHandler::AlwaysParseForType(Manifest::Type type) const { | 306 bool BackgroundManifestHandler::AlwaysParseForType(Manifest::Type type) const { |
| 347 return type == Manifest::TYPE_PLATFORM_APP; | 307 return type == Manifest::TYPE_PLATFORM_APP; |
| 348 } | 308 } |
| 349 | 309 |
| 350 const std::vector<std::string> BackgroundManifestHandler::Keys() const { | 310 const std::vector<std::string> BackgroundManifestHandler::Keys() const { |
| 351 static const char* keys[] = {keys::kBackgroundAllowJsAccess, | 311 static const char* keys[] = { |
| 352 keys::kBackgroundPage, | 312 keys::kBackgroundAllowJsAccess, keys::kBackgroundPage, |
| 353 keys::kBackgroundPageLegacy, | 313 keys::kBackgroundPageLegacy, keys::kBackgroundPersistent, |
| 354 keys::kBackgroundPersistent, | 314 keys::kBackgroundScripts, keys::kPlatformAppBackgroundPage, |
| 355 keys::kBackgroundScripts, | 315 keys::kPlatformAppBackgroundScripts}; |
| 356 keys::kPlatformAppBackgroundPage, | |
| 357 keys::kPlatformAppBackgroundScripts, | |
| 358 keys::kPlatformAppServiceWorkerScript}; | |
| 359 return std::vector<std::string>(keys, keys + arraysize(keys)); | 316 return std::vector<std::string>(keys, keys + arraysize(keys)); |
| 360 } | 317 } |
| 361 | 318 |
| 362 } // namespace extensions | 319 } // namespace extensions |
| OLD | NEW |