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/background_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/string_number_conversions.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/extensions/extension_constants.h" |
| 14 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 15 #include "chrome/common/extensions/permissions/api_permission_set.h" |
| 16 #include "extensions/common/error_utils.h" |
| 17 |
| 18 using base::DictionaryValue; |
| 19 namespace keys = extension_manifest_keys; |
| 20 namespace values = extension_manifest_values; |
| 21 namespace errors = extension_manifest_errors; |
| 22 |
| 23 namespace extensions { |
| 24 |
| 25 namespace { |
| 26 |
| 27 const char kBackground[] = "background"; |
| 28 |
| 29 static base::LazyInstance<BackgroundInfo> g_empty_background_info = |
| 30 LAZY_INSTANCE_INITIALIZER; |
| 31 |
| 32 const BackgroundInfo& GetBackgroundInfo(const Extension* extension) { |
| 33 BackgroundInfo* info = static_cast<BackgroundInfo*>( |
| 34 extension->GetManifestData(kBackground)); |
| 35 if (!info) |
| 36 return g_empty_background_info.Get(); |
| 37 return *info; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 BackgroundInfo::BackgroundInfo() |
| 43 : is_persistent_(true), |
| 44 allow_js_access_(true) { |
| 45 } |
| 46 |
| 47 BackgroundInfo::~BackgroundInfo() { |
| 48 } |
| 49 |
| 50 // static |
| 51 GURL BackgroundInfo::GetBackgroundURL(const Extension* extension) { |
| 52 const BackgroundInfo& info = GetBackgroundInfo(extension); |
| 53 if (info.background_scripts_.empty()) |
| 54 return info.background_url_; |
| 55 return extension->GetResourceURL( |
| 56 extension_filenames::kGeneratedBackgroundPageFilename); |
| 57 } |
| 58 |
| 59 // static |
| 60 const std::vector<std::string>& BackgroundInfo::GetBackgroundScripts( |
| 61 const Extension* extension) { |
| 62 return GetBackgroundInfo(extension).background_scripts_; |
| 63 } |
| 64 |
| 65 // static |
| 66 bool BackgroundInfo::HasBackgroundPage(const Extension* extension) { |
| 67 return GetBackgroundInfo(extension).has_background_page(); |
| 68 } |
| 69 |
| 70 // static |
| 71 bool BackgroundInfo::AllowJSAccess(const Extension* extension) { |
| 72 return GetBackgroundInfo(extension).allow_js_access_; |
| 73 } |
| 74 |
| 75 // static |
| 76 bool BackgroundInfo::HasPersistentBackgroundPage(const Extension* extension) { |
| 77 const BackgroundInfo& info = GetBackgroundInfo(extension); |
| 78 return info.has_background_page() && info.is_persistent_; |
| 79 } |
| 80 |
| 81 // static |
| 82 bool BackgroundInfo::HasLazyBackgroundPage(const Extension* extension) { |
| 83 const BackgroundInfo& info = GetBackgroundInfo(extension); |
| 84 return info.has_background_page() && !info.is_persistent_; |
| 85 } |
| 86 |
| 87 bool BackgroundInfo::Parse(const Extension* extension, string16* error) { |
| 88 const std::string& bg_scripts_key = extension->is_platform_app() ? |
| 89 keys::kPlatformAppBackgroundScripts : keys::kBackgroundScripts; |
| 90 if (!LoadBackgroundScripts(extension, bg_scripts_key, error) || |
| 91 !LoadBackgroundPage(extension, error) || |
| 92 !LoadBackgroundPersistent(extension, error) || |
| 93 !LoadAllowJSAccess(extension, error)) { |
| 94 return false; |
| 95 } |
| 96 return true; |
| 97 } |
| 98 |
| 99 bool BackgroundInfo::LoadBackgroundScripts(const Extension* extension, |
| 100 const std::string& key, |
| 101 string16* error) { |
| 102 const Value* background_scripts_value = NULL; |
| 103 if (!extension->manifest()->Get(key, &background_scripts_value)) |
| 104 return true; |
| 105 |
| 106 CHECK(background_scripts_value); |
| 107 if (background_scripts_value->GetType() != Value::TYPE_LIST) { |
| 108 *error = ASCIIToUTF16(errors::kInvalidBackgroundScripts); |
| 109 return false; |
| 110 } |
| 111 |
| 112 const ListValue* background_scripts = NULL; |
| 113 background_scripts_value->GetAsList(&background_scripts); |
| 114 for (size_t i = 0; i < background_scripts->GetSize(); ++i) { |
| 115 std::string script; |
| 116 if (!background_scripts->GetString(i, &script)) { |
| 117 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 118 errors::kInvalidBackgroundScript, base::IntToString(i)); |
| 119 return false; |
| 120 } |
| 121 background_scripts_.push_back(script); |
| 122 } |
| 123 |
| 124 return true; |
| 125 } |
| 126 |
| 127 bool BackgroundInfo::LoadBackgroundPage(const Extension* extension, |
| 128 const std::string& key, |
| 129 string16* error) { |
| 130 const base::Value* background_page_value = NULL; |
| 131 if (!extension->manifest()->Get(key, &background_page_value)) |
| 132 return true; |
| 133 |
| 134 if (!background_scripts_.empty()) { |
| 135 *error = ASCIIToUTF16(errors::kInvalidBackgroundCombination); |
| 136 return false; |
| 137 } |
| 138 |
| 139 std::string background_str; |
| 140 if (!background_page_value->GetAsString(&background_str)) { |
| 141 *error = ASCIIToUTF16(errors::kInvalidBackground); |
| 142 return false; |
| 143 } |
| 144 |
| 145 if (extension->is_hosted_app()) { |
| 146 background_url_ = GURL(background_str); |
| 147 |
| 148 if (!extension->initial_api_permissions()->count( |
| 149 APIPermission::kBackground)) { |
| 150 *error = ASCIIToUTF16(errors::kBackgroundPermissionNeeded); |
| 151 return false; |
| 152 } |
| 153 // Hosted apps require an absolute URL. |
| 154 if (!background_url_.is_valid()) { |
| 155 *error = ASCIIToUTF16(errors::kInvalidBackgroundInHostedApp); |
| 156 return false; |
| 157 } |
| 158 |
| 159 if (!(background_url_.SchemeIs("https") || |
| 160 (CommandLine::ForCurrentProcess()->HasSwitch( |
| 161 switches::kAllowHTTPBackgroundPage) && |
| 162 background_url_.SchemeIs("http")))) { |
| 163 *error = ASCIIToUTF16(errors::kInvalidBackgroundInHostedApp); |
| 164 return false; |
| 165 } |
| 166 } else { |
| 167 background_url_ = extension->GetResourceURL(background_str); |
| 168 } |
| 169 |
| 170 return true; |
| 171 } |
| 172 |
| 173 bool BackgroundInfo::LoadBackgroundPage(const Extension* extension, |
| 174 string16* error) { |
| 175 if (extension->is_platform_app()) { |
| 176 return LoadBackgroundPage( |
| 177 extension, keys::kPlatformAppBackgroundPage, error); |
| 178 } |
| 179 |
| 180 if (!LoadBackgroundPage(extension, keys::kBackgroundPage, error)) |
| 181 return false; |
| 182 if (background_url_.is_empty()) |
| 183 return LoadBackgroundPage(extension, keys::kBackgroundPageLegacy, error); |
| 184 return true; |
| 185 } |
| 186 |
| 187 bool BackgroundInfo::LoadBackgroundPersistent(const Extension* extension, |
| 188 string16* error) { |
| 189 if (extension->is_platform_app()) { |
| 190 is_persistent_ = false; |
| 191 return true; |
| 192 } |
| 193 |
| 194 const Value* background_persistent = NULL; |
| 195 if (!extension->manifest()->Get(keys::kBackgroundPersistent, |
| 196 &background_persistent)) |
| 197 return true; |
| 198 |
| 199 if (!background_persistent->GetAsBoolean(&is_persistent_)) { |
| 200 *error = ASCIIToUTF16(errors::kInvalidBackgroundPersistent); |
| 201 return false; |
| 202 } |
| 203 |
| 204 if (!has_background_page()) { |
| 205 *error = ASCIIToUTF16(errors::kInvalidBackgroundPersistentNoPage); |
| 206 return false; |
| 207 } |
| 208 |
| 209 return true; |
| 210 } |
| 211 |
| 212 bool BackgroundInfo::LoadAllowJSAccess(const Extension* extension, |
| 213 string16* error) { |
| 214 const Value* allow_js_access = NULL; |
| 215 if (!extension->manifest()->Get(keys::kBackgroundAllowJsAccess, |
| 216 &allow_js_access)) |
| 217 return true; |
| 218 |
| 219 if (!allow_js_access->IsType(Value::TYPE_BOOLEAN) || |
| 220 !allow_js_access->GetAsBoolean(&allow_js_access_)) { |
| 221 *error = ASCIIToUTF16(errors::kInvalidBackgroundAllowJsAccess); |
| 222 return false; |
| 223 } |
| 224 |
| 225 return true; |
| 226 } |
| 227 |
| 228 BackgroundManifestHandler::BackgroundManifestHandler() { |
| 229 } |
| 230 |
| 231 BackgroundManifestHandler::~BackgroundManifestHandler() { |
| 232 } |
| 233 |
| 234 bool BackgroundManifestHandler::Parse(Extension* extension, string16* error) { |
| 235 scoped_ptr<BackgroundInfo> info(new BackgroundInfo); |
| 236 if (!info->Parse(extension, error)) |
| 237 return false; |
| 238 extension->SetManifestData(kBackground, info.release()); |
| 239 return true; |
| 240 } |
| 241 |
| 242 const std::vector<std::string> BackgroundManifestHandler::Keys() const { |
| 243 static const char* keys[] = { |
| 244 keys::kBackgroundAllowJsAccess, |
| 245 keys::kBackgroundPage, |
| 246 keys::kBackgroundPageLegacy, |
| 247 keys::kBackgroundPersistent, |
| 248 keys::kBackgroundScripts, |
| 249 keys::kPlatformAppBackgroundPage, |
| 250 keys::kPlatformAppBackgroundScripts |
| 251 }; |
| 252 return std::vector<std::string>(keys, keys + arraysize(keys)); |
| 253 } |
| 254 |
| 255 } // extensions |
OLD | NEW |