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" | |
13 #include "extensions/common/constants.h" | 14 #include "extensions/common/constants.h" |
14 #include "extensions/common/error_utils.h" | 15 #include "extensions/common/error_utils.h" |
15 #include "extensions/common/file_util.h" | 16 #include "extensions/common/file_util.h" |
16 #include "extensions/common/manifest_constants.h" | 17 #include "extensions/common/manifest_constants.h" |
17 #include "extensions/common/permissions/api_permission_set.h" | 18 #include "extensions/common/permissions/api_permission_set.h" |
18 #include "extensions/common/permissions/permissions_data.h" | 19 #include "extensions/common/permissions/permissions_data.h" |
19 #include "extensions/common/switches.h" | 20 #include "extensions/common/switches.h" |
20 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
21 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
22 | 23 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 | 57 |
57 // static | 58 // static |
58 GURL BackgroundInfo::GetBackgroundURL(const Extension* extension) { | 59 GURL BackgroundInfo::GetBackgroundURL(const Extension* extension) { |
59 const BackgroundInfo& info = GetBackgroundInfo(extension); | 60 const BackgroundInfo& info = GetBackgroundInfo(extension); |
60 if (info.background_scripts_.empty()) | 61 if (info.background_scripts_.empty()) |
61 return info.background_url_; | 62 return info.background_url_; |
62 return extension->GetResourceURL(kGeneratedBackgroundPageFilename); | 63 return extension->GetResourceURL(kGeneratedBackgroundPageFilename); |
63 } | 64 } |
64 | 65 |
65 // static | 66 // 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( | 67 const std::vector<std::string>& BackgroundInfo::GetBackgroundScripts( |
73 const Extension* extension) { | 68 const Extension* extension) { |
74 return GetBackgroundInfo(extension).background_scripts_; | 69 return GetBackgroundInfo(extension).background_scripts_; |
75 } | 70 } |
76 | 71 |
77 // static | 72 // static |
73 const std::string& BackgroundInfo::GetServiceWorkerScript( | |
74 const Extension* extension) { | |
75 return GetBackgroundInfo(extension).service_worker_script_; | |
76 } | |
77 | |
78 // static | |
78 bool BackgroundInfo::HasBackgroundPage(const Extension* extension) { | 79 bool BackgroundInfo::HasBackgroundPage(const Extension* extension) { |
79 return GetBackgroundInfo(extension).has_background_page(); | 80 return GetBackgroundInfo(extension).has_background_page(); |
80 } | 81 } |
81 | 82 |
82 // static | 83 // 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) { | 84 bool BackgroundInfo::HasPersistentBackgroundPage(const Extension* extension) { |
89 return GetBackgroundInfo(extension).has_persistent_background_page(); | 85 return GetBackgroundInfo(extension).has_persistent_background_page(); |
90 } | 86 } |
91 | 87 |
92 // static | 88 // static |
93 bool BackgroundInfo::HasLazyBackgroundPage(const Extension* extension) { | 89 bool BackgroundInfo::HasLazyBackgroundPage(const Extension* extension) { |
94 return GetBackgroundInfo(extension).has_lazy_background_page(); | 90 return GetBackgroundInfo(extension).has_lazy_background_page(); |
95 } | 91 } |
96 | 92 |
93 // static | |
94 bool BackgroundInfo::HasGeneratedBackgroundPage(const Extension* extension) { | |
95 const BackgroundInfo& info = GetBackgroundInfo(extension); | |
96 return !info.background_scripts_.empty(); | |
97 } | |
98 | |
99 // 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) { | |
106 return GetBackgroundInfo(extension).allow_js_access_; | |
107 } | |
108 | |
97 bool BackgroundInfo::Parse(const Extension* extension, base::string16* error) { | 109 bool BackgroundInfo::Parse(const Extension* extension, base::string16* error) { |
98 const std::string& bg_scripts_key = extension->is_platform_app() ? | 110 const std::string& bg_scripts_key = extension->is_platform_app() ? |
99 keys::kPlatformAppBackgroundScripts : keys::kBackgroundScripts; | 111 keys::kPlatformAppBackgroundScripts : keys::kBackgroundScripts; |
100 if (!LoadBackgroundScripts(extension, bg_scripts_key, error) || | 112 const std::string& sw_scripts_key = |
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) || | |
101 !LoadBackgroundPage(extension, error) || | 118 !LoadBackgroundPage(extension, error) || |
102 !LoadBackgroundPersistent(extension, error) || | 119 !LoadBackgroundPersistent(extension, error) || |
103 !LoadAllowJSAccess(extension, error)) { | 120 !LoadAllowJSAccess(extension, error)) { |
104 return false; | 121 return false; |
105 } | 122 } |
123 | |
124 int background_solution_sum = (background_url_.is_valid() ? 1 : 0) + | |
Jeffrey Yasskin
2014/03/04 01:41:48
Adding up the booleans will give you the same answ
scheib
2014/03/04 15:34:31
Done. Yes, but explicit conversion reduces human's
| |
125 (!background_scripts_.empty() ? 1 : 0) + | |
126 (has_service_worker() ? 1 : 0); | |
127 if (background_solution_sum > 1) { | |
128 *error = ASCIIToUTF16(errors::kInvalidBackgroundCombination); | |
129 return false; | |
130 } | |
131 | |
106 return true; | 132 return true; |
107 } | 133 } |
108 | 134 |
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::kEnableServiceWorker)) { | |
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 | |
109 bool BackgroundInfo::LoadBackgroundScripts(const Extension* extension, | 156 bool BackgroundInfo::LoadBackgroundScripts(const Extension* extension, |
110 const std::string& key, | 157 const std::string& key, |
111 base::string16* error) { | 158 base::string16* error) { |
112 const base::Value* background_scripts_value = NULL; | 159 const base::Value* background_scripts_value = NULL; |
113 if (!extension->manifest()->Get(key, &background_scripts_value)) | 160 if (!extension->manifest()->Get(key, &background_scripts_value)) |
114 return true; | 161 return true; |
115 | 162 |
116 CHECK(background_scripts_value); | 163 CHECK(background_scripts_value); |
117 if (background_scripts_value->GetType() != base::Value::TYPE_LIST) { | 164 if (background_scripts_value->GetType() != base::Value::TYPE_LIST) { |
118 *error = ASCIIToUTF16(errors::kInvalidBackgroundScripts); | 165 *error = ASCIIToUTF16(errors::kInvalidBackgroundScripts); |
(...skipping 15 matching lines...) Expand all Loading... | |
134 return true; | 181 return true; |
135 } | 182 } |
136 | 183 |
137 bool BackgroundInfo::LoadBackgroundPage(const Extension* extension, | 184 bool BackgroundInfo::LoadBackgroundPage(const Extension* extension, |
138 const std::string& key, | 185 const std::string& key, |
139 base::string16* error) { | 186 base::string16* error) { |
140 const base::Value* background_page_value = NULL; | 187 const base::Value* background_page_value = NULL; |
141 if (!extension->manifest()->Get(key, &background_page_value)) | 188 if (!extension->manifest()->Get(key, &background_page_value)) |
142 return true; | 189 return true; |
143 | 190 |
144 if (!background_scripts_.empty()) { | |
145 *error = ASCIIToUTF16(errors::kInvalidBackgroundCombination); | |
146 return false; | |
147 } | |
148 | |
149 std::string background_str; | 191 std::string background_str; |
150 if (!background_page_value->GetAsString(&background_str)) { | 192 if (!background_page_value->GetAsString(&background_str)) { |
151 *error = ASCIIToUTF16(errors::kInvalidBackground); | 193 *error = ASCIIToUTF16(errors::kInvalidBackground); |
152 return false; | 194 return false; |
153 } | 195 } |
154 | 196 |
155 if (extension->is_hosted_app()) { | 197 if (extension->is_hosted_app()) { |
156 background_url_ = GURL(background_str); | 198 background_url_ = GURL(background_str); |
157 | 199 |
158 if (!PermissionsData::GetInitialAPIPermissions(extension)->count( | 200 if (!PermissionsData::GetInitialAPIPermissions(extension)->count( |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 | 282 |
241 BackgroundManifestHandler::~BackgroundManifestHandler() { | 283 BackgroundManifestHandler::~BackgroundManifestHandler() { |
242 } | 284 } |
243 | 285 |
244 bool BackgroundManifestHandler::Parse(Extension* extension, | 286 bool BackgroundManifestHandler::Parse(Extension* extension, |
245 base::string16* error) { | 287 base::string16* error) { |
246 scoped_ptr<BackgroundInfo> info(new BackgroundInfo); | 288 scoped_ptr<BackgroundInfo> info(new BackgroundInfo); |
247 if (!info->Parse(extension, error)) | 289 if (!info->Parse(extension, error)) |
248 return false; | 290 return false; |
249 | 291 |
250 // Platform apps must have background pages. | 292 // Platform apps must have background pages or service workers. |
251 if (extension->is_platform_app() && !info->has_background_page()) { | 293 if (extension->is_platform_app() && !info->has_background_page() && |
294 !info->has_service_worker()) { | |
252 *error = ASCIIToUTF16(errors::kBackgroundRequiredForPlatformApps); | 295 *error = ASCIIToUTF16(errors::kBackgroundRequiredForPlatformApps); |
253 return false; | 296 return false; |
254 } | 297 } |
255 // Lazy background pages are incompatible with the webRequest API. | 298 // Lazy background pages are incompatible with the webRequest API. |
256 if (info->has_lazy_background_page() && | 299 if (info->has_lazy_background_page() && |
257 PermissionsData::GetInitialAPIPermissions(extension)->count( | 300 PermissionsData::GetInitialAPIPermissions(extension)->count( |
258 APIPermission::kWebRequest)) { | 301 APIPermission::kWebRequest)) { |
259 *error = ASCIIToUTF16(errors::kWebRequestConflictsWithLazyBackground); | 302 *error = ASCIIToUTF16(errors::kWebRequestConflictsWithLazyBackground); |
260 return false; | 303 return false; |
261 } | 304 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 } | 348 } |
306 | 349 |
307 const std::vector<std::string> BackgroundManifestHandler::Keys() const { | 350 const std::vector<std::string> BackgroundManifestHandler::Keys() const { |
308 static const char* keys[] = { | 351 static const char* keys[] = { |
309 keys::kBackgroundAllowJsAccess, | 352 keys::kBackgroundAllowJsAccess, |
310 keys::kBackgroundPage, | 353 keys::kBackgroundPage, |
311 keys::kBackgroundPageLegacy, | 354 keys::kBackgroundPageLegacy, |
312 keys::kBackgroundPersistent, | 355 keys::kBackgroundPersistent, |
313 keys::kBackgroundScripts, | 356 keys::kBackgroundScripts, |
314 keys::kPlatformAppBackgroundPage, | 357 keys::kPlatformAppBackgroundPage, |
315 keys::kPlatformAppBackgroundScripts | 358 keys::kPlatformAppBackgroundScripts, |
359 keys::kPlatformAppServiceWorkerScript | |
316 }; | 360 }; |
317 return std::vector<std::string>(keys, keys + arraysize(keys)); | 361 return std::vector<std::string>(keys, keys + arraysize(keys)); |
318 } | 362 } |
319 | 363 |
320 } // namespace extensions | 364 } // namespace extensions |
OLD | NEW |