Chromium Code Reviews| 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" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 | 56 |
| 57 // static | 57 // static |
| 58 GURL BackgroundInfo::GetBackgroundURL(const Extension* extension) { | 58 GURL BackgroundInfo::GetBackgroundURL(const Extension* extension) { |
| 59 const BackgroundInfo& info = GetBackgroundInfo(extension); | 59 const BackgroundInfo& info = GetBackgroundInfo(extension); |
| 60 if (info.background_scripts_.empty()) | 60 if (info.background_scripts_.empty()) |
| 61 return info.background_url_; | 61 return info.background_url_; |
| 62 return extension->GetResourceURL(kGeneratedBackgroundPageFilename); | 62 return extension->GetResourceURL(kGeneratedBackgroundPageFilename); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // static | 65 // static |
| 66 bool BackgroundInfo::HasGeneratedBackgroundPage(const Extension* extension) { | |
| 67 const BackgroundInfo& info = GetBackgroundInfo(extension); | |
| 68 return !info.background_scripts_.empty(); | |
| 69 } | |
| 70 | |
| 71 // static | |
| 72 const std::vector<std::string>& BackgroundInfo::GetBackgroundScripts( | 66 const std::vector<std::string>& BackgroundInfo::GetBackgroundScripts( |
| 73 const Extension* extension) { | 67 const Extension* extension) { |
| 74 return GetBackgroundInfo(extension).background_scripts_; | 68 return GetBackgroundInfo(extension).background_scripts_; |
| 75 } | 69 } |
| 76 | 70 |
| 77 // static | 71 // static |
| 72 std::string BackgroundInfo::GetServiceWorkerScript(const Extension* extension) { | |
| 73 return GetBackgroundInfo(extension).service_worker_script_; | |
| 74 } | |
| 75 | |
| 76 // static | |
| 78 bool BackgroundInfo::HasBackgroundPage(const Extension* extension) { | 77 bool BackgroundInfo::HasBackgroundPage(const Extension* extension) { |
| 79 return GetBackgroundInfo(extension).has_background_page(); | 78 return GetBackgroundInfo(extension).has_background_page(); |
| 80 } | 79 } |
| 81 | 80 |
| 82 // static | 81 // static |
| 83 bool BackgroundInfo::AllowJSAccess(const Extension* extension) { | |
| 84 return GetBackgroundInfo(extension).allow_js_access_; | |
| 85 } | |
| 86 | |
| 87 // static | |
| 88 bool BackgroundInfo::HasPersistentBackgroundPage(const Extension* extension) { | 82 bool BackgroundInfo::HasPersistentBackgroundPage(const Extension* extension) { |
| 89 return GetBackgroundInfo(extension).has_persistent_background_page(); | 83 return GetBackgroundInfo(extension).has_persistent_background_page(); |
| 90 } | 84 } |
| 91 | 85 |
| 92 // static | 86 // static |
| 93 bool BackgroundInfo::HasLazyBackgroundPage(const Extension* extension) { | 87 bool BackgroundInfo::HasLazyBackgroundPage(const Extension* extension) { |
| 94 return GetBackgroundInfo(extension).has_lazy_background_page(); | 88 return GetBackgroundInfo(extension).has_lazy_background_page(); |
| 95 } | 89 } |
| 96 | 90 |
| 91 // static | |
| 92 bool BackgroundInfo::HasGeneratedBackgroundPage(const Extension* extension) { | |
| 93 const BackgroundInfo& info = GetBackgroundInfo(extension); | |
| 94 return !info.background_scripts_.empty(); | |
| 95 } | |
| 96 | |
| 97 // static | |
| 98 bool BackgroundInfo::HasServiceWorker(const Extension* extension) { | |
| 99 return GetBackgroundInfo(extension).has_service_worker(); | |
| 100 } | |
| 101 | |
| 102 // static | |
| 103 bool BackgroundInfo::AllowJSAccess(const Extension* extension) { | |
| 104 return GetBackgroundInfo(extension).allow_js_access_; | |
| 105 } | |
| 106 | |
| 97 bool BackgroundInfo::Parse(const Extension* extension, base::string16* error) { | 107 bool BackgroundInfo::Parse(const Extension* extension, base::string16* error) { |
| 98 const std::string& bg_scripts_key = extension->is_platform_app() ? | 108 const std::string& bg_scripts_key = extension->is_platform_app() ? |
| 99 keys::kPlatformAppBackgroundScripts : keys::kBackgroundScripts; | 109 keys::kPlatformAppBackgroundScripts : keys::kBackgroundScripts; |
| 100 if (!LoadBackgroundScripts(extension, bg_scripts_key, error) || | 110 const std::string& sw_scripts_key = |
| 111 extension->is_platform_app() | |
| 112 ? keys::kPlatformAppServiceWorkerScript | |
| 113 : ""; // TODO(scheib): Support extensions crbug.com/346885 | |
| 114 if (!LoadServiceWorkerScript(extension, sw_scripts_key, error) || | |
| 115 !LoadBackgroundScripts(extension, bg_scripts_key, error) || | |
| 101 !LoadBackgroundPage(extension, error) || | 116 !LoadBackgroundPage(extension, error) || |
| 102 !LoadBackgroundPersistent(extension, error) || | 117 !LoadBackgroundPersistent(extension, error) || |
| 103 !LoadAllowJSAccess(extension, error)) { | 118 !LoadAllowJSAccess(extension, error)) { |
| 104 return false; | 119 return false; |
| 105 } | 120 } |
| 106 return true; | 121 return true; |
| 107 } | 122 } |
| 108 | 123 |
| 124 bool BackgroundInfo::LoadServiceWorkerScript(const Extension* extension, | |
|
Jeffrey Yasskin
2014/02/28 01:54:37
The Service Worker is only going to actually work
Yoyo Zhou
2014/02/28 17:59:09
Set *error to a relevant message.
By the way, you
scheib
2014/03/03 19:54:00
Done.
| |
| 125 const std::string& key, | |
| 126 base::string16* error) { | |
| 127 const base::Value* service_worker_script_value = NULL; | |
| 128 if (!extension->manifest()->Get(key, &service_worker_script_value)) | |
| 129 return true; | |
| 130 | |
| 131 CHECK(service_worker_script_value); | |
| 132 if (!service_worker_script_value->GetAsString(&service_worker_script_)) { | |
| 133 *error = ASCIIToUTF16(errors::kInvalidServiceWorkerScript); | |
| 134 return false; | |
| 135 } | |
| 136 return true; | |
| 137 } | |
| 138 | |
| 109 bool BackgroundInfo::LoadBackgroundScripts(const Extension* extension, | 139 bool BackgroundInfo::LoadBackgroundScripts(const Extension* extension, |
| 110 const std::string& key, | 140 const std::string& key, |
| 111 base::string16* error) { | 141 base::string16* error) { |
| 112 const base::Value* background_scripts_value = NULL; | 142 const base::Value* background_scripts_value = NULL; |
| 113 if (!extension->manifest()->Get(key, &background_scripts_value)) | 143 if (!extension->manifest()->Get(key, &background_scripts_value)) |
| 114 return true; | 144 return true; |
| 115 | 145 |
| 146 if (has_service_worker()) { | |
|
Jeffrey Yasskin
2014/02/28 01:54:37
Could you put this in BgInfo::Parse so that its co
scheib
2014/03/03 19:54:00
Done.
| |
| 147 *error = ASCIIToUTF16(errors::kInvalidBackgroundCombination); | |
| 148 return false; | |
| 149 } | |
| 150 | |
| 116 CHECK(background_scripts_value); | 151 CHECK(background_scripts_value); |
| 117 if (background_scripts_value->GetType() != base::Value::TYPE_LIST) { | 152 if (background_scripts_value->GetType() != base::Value::TYPE_LIST) { |
| 118 *error = ASCIIToUTF16(errors::kInvalidBackgroundScripts); | 153 *error = ASCIIToUTF16(errors::kInvalidBackgroundScripts); |
| 119 return false; | 154 return false; |
| 120 } | 155 } |
| 121 | 156 |
| 122 const base::ListValue* background_scripts = NULL; | 157 const base::ListValue* background_scripts = NULL; |
| 123 background_scripts_value->GetAsList(&background_scripts); | 158 background_scripts_value->GetAsList(&background_scripts); |
| 124 for (size_t i = 0; i < background_scripts->GetSize(); ++i) { | 159 for (size_t i = 0; i < background_scripts->GetSize(); ++i) { |
| 125 std::string script; | 160 std::string script; |
| 126 if (!background_scripts->GetString(i, &script)) { | 161 if (!background_scripts->GetString(i, &script)) { |
| 127 *error = ErrorUtils::FormatErrorMessageUTF16( | 162 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 128 errors::kInvalidBackgroundScript, base::IntToString(i)); | 163 errors::kInvalidBackgroundScript, base::IntToString(i)); |
| 129 return false; | 164 return false; |
| 130 } | 165 } |
| 131 background_scripts_.push_back(script); | 166 background_scripts_.push_back(script); |
| 132 } | 167 } |
| 133 | 168 |
| 134 return true; | 169 return true; |
| 135 } | 170 } |
| 136 | 171 |
| 137 bool BackgroundInfo::LoadBackgroundPage(const Extension* extension, | 172 bool BackgroundInfo::LoadBackgroundPage(const Extension* extension, |
| 138 const std::string& key, | 173 const std::string& key, |
| 139 base::string16* error) { | 174 base::string16* error) { |
| 140 const base::Value* background_page_value = NULL; | 175 const base::Value* background_page_value = NULL; |
| 141 if (!extension->manifest()->Get(key, &background_page_value)) | 176 if (!extension->manifest()->Get(key, &background_page_value)) |
| 142 return true; | 177 return true; |
| 143 | 178 |
| 144 if (!background_scripts_.empty()) { | 179 if (!background_scripts_.empty() || has_service_worker()) { |
| 145 *error = ASCIIToUTF16(errors::kInvalidBackgroundCombination); | 180 *error = ASCIIToUTF16(errors::kInvalidBackgroundCombination); |
| 146 return false; | 181 return false; |
| 147 } | 182 } |
| 148 | 183 |
| 149 std::string background_str; | 184 std::string background_str; |
| 150 if (!background_page_value->GetAsString(&background_str)) { | 185 if (!background_page_value->GetAsString(&background_str)) { |
| 151 *error = ASCIIToUTF16(errors::kInvalidBackground); | 186 *error = ASCIIToUTF16(errors::kInvalidBackground); |
| 152 return false; | 187 return false; |
| 153 } | 188 } |
| 154 | 189 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 | 275 |
| 241 BackgroundManifestHandler::~BackgroundManifestHandler() { | 276 BackgroundManifestHandler::~BackgroundManifestHandler() { |
| 242 } | 277 } |
| 243 | 278 |
| 244 bool BackgroundManifestHandler::Parse(Extension* extension, | 279 bool BackgroundManifestHandler::Parse(Extension* extension, |
| 245 base::string16* error) { | 280 base::string16* error) { |
| 246 scoped_ptr<BackgroundInfo> info(new BackgroundInfo); | 281 scoped_ptr<BackgroundInfo> info(new BackgroundInfo); |
| 247 if (!info->Parse(extension, error)) | 282 if (!info->Parse(extension, error)) |
| 248 return false; | 283 return false; |
| 249 | 284 |
| 250 // Platform apps must have background pages. | 285 // Platform apps must have background pages or service workers. |
| 251 if (extension->is_platform_app() && !info->has_background_page()) { | 286 if (extension->is_platform_app() && !info->has_background_page() && |
| 287 !info->has_service_worker()) { | |
| 252 *error = ASCIIToUTF16(errors::kBackgroundRequiredForPlatformApps); | 288 *error = ASCIIToUTF16(errors::kBackgroundRequiredForPlatformApps); |
| 253 return false; | 289 return false; |
| 254 } | 290 } |
| 255 // Lazy background pages are incompatible with the webRequest API. | 291 // Lazy background pages are incompatible with the webRequest API. |
| 256 if (info->has_lazy_background_page() && | 292 if (info->has_lazy_background_page() && |
| 257 PermissionsData::GetInitialAPIPermissions(extension)->count( | 293 PermissionsData::GetInitialAPIPermissions(extension)->count( |
| 258 APIPermission::kWebRequest)) { | 294 APIPermission::kWebRequest)) { |
| 259 *error = ASCIIToUTF16(errors::kWebRequestConflictsWithLazyBackground); | 295 *error = ASCIIToUTF16(errors::kWebRequestConflictsWithLazyBackground); |
| 260 return false; | 296 return false; |
| 261 } | 297 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 } | 341 } |
| 306 | 342 |
| 307 const std::vector<std::string> BackgroundManifestHandler::Keys() const { | 343 const std::vector<std::string> BackgroundManifestHandler::Keys() const { |
| 308 static const char* keys[] = { | 344 static const char* keys[] = { |
| 309 keys::kBackgroundAllowJsAccess, | 345 keys::kBackgroundAllowJsAccess, |
| 310 keys::kBackgroundPage, | 346 keys::kBackgroundPage, |
| 311 keys::kBackgroundPageLegacy, | 347 keys::kBackgroundPageLegacy, |
| 312 keys::kBackgroundPersistent, | 348 keys::kBackgroundPersistent, |
| 313 keys::kBackgroundScripts, | 349 keys::kBackgroundScripts, |
| 314 keys::kPlatformAppBackgroundPage, | 350 keys::kPlatformAppBackgroundPage, |
| 315 keys::kPlatformAppBackgroundScripts | 351 keys::kPlatformAppBackgroundScripts |
|
not at google - send to devlin
2014/02/28 17:48:14
(partly as a response to Jeffrey's comment) I woul
Yoyo Zhou
2014/02/28 17:59:09
This is the list of keys which, if any is present,
scheib
2014/03/03 19:54:00
Done.
| |
| 316 }; | 352 }; |
| 317 return std::vector<std::string>(keys, keys + arraysize(keys)); | 353 return std::vector<std::string>(keys, keys + arraysize(keys)); |
| 318 } | 354 } |
| 319 | 355 |
| 320 } // namespace extensions | 356 } // namespace extensions |
| OLD | NEW |