[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
rockot | d554614 | 2014-10-15 00:29:08 | [diff] [blame] | 5 | #include "extensions/common/manifest_url_handlers.h" |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 6 | |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 7 | #include "base/files/file_util.h" |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
[email protected] | d2e754f | 2013-06-11 01:41:47 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
| 10 | #include "base/strings/stringprintf.h" |
[email protected] | 12bfb61 | 2013-06-07 19:54:02 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 12 | #include "base/values.h" |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 13 | #include "extensions/common/error_utils.h" |
rockot | 9065985 | 2014-09-18 19:31:52 | [diff] [blame] | 14 | #include "extensions/common/extension_urls.h" |
[email protected] | 558878cc8 | 2013-11-09 01:25:51 | [diff] [blame] | 15 | #include "extensions/common/file_util.h" |
[email protected] | d42c1115 | 2013-08-22 19:36:32 | [diff] [blame] | 16 | #include "extensions/common/manifest.h" |
[email protected] | 0c3c973 | 2013-09-16 08:53:41 | [diff] [blame] | 17 | #include "extensions/common/manifest_constants.h" |
elijahtaylor | 58dac22 | 2014-09-03 21:42:35 | [diff] [blame] | 18 | #include "extensions/common/manifest_handlers/shared_module_info.h" |
rockot | 3b99b8e | 2014-10-14 21:54:35 | [diff] [blame] | 19 | #include "extensions/strings/grit/extensions_strings.h" |
[email protected] | 989ae86 | 2013-03-13 04:22:58 | [diff] [blame] | 20 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 21 | |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 22 | namespace extensions { |
| 23 | |
[email protected] | 0c3c973 | 2013-09-16 08:53:41 | [diff] [blame] | 24 | namespace keys = manifest_keys; |
| 25 | namespace errors = manifest_errors; |
| 26 | |
rockot | 3b99b8e | 2014-10-14 21:54:35 | [diff] [blame] | 27 | // static |
| 28 | const GURL& ManifestURL::Get(const Extension* extension, |
| 29 | const std::string& key) { |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 30 | ManifestURL* manifest_url = |
| 31 | static_cast<ManifestURL*>(extension->GetManifestData(key)); |
| 32 | return manifest_url ? manifest_url->url_ : GURL::EmptyGURL(); |
| 33 | } |
| 34 | |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 35 | // static |
| 36 | const GURL ManifestURL::GetHomepageURL(const Extension* extension) { |
rockot | 3b99b8e | 2014-10-14 21:54:35 | [diff] [blame] | 37 | const GURL& homepage_url = Get(extension, keys::kHomepageURL); |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 38 | if (homepage_url.is_valid()) |
| 39 | return homepage_url; |
elijahtaylor | 58dac22 | 2014-09-03 21:42:35 | [diff] [blame] | 40 | bool use_webstore_url = UpdatesFromGallery(extension) && |
| 41 | !SharedModuleInfo::IsSharedModule(extension); |
| 42 | return use_webstore_url |
| 43 | ? GURL(extension_urls::GetWebstoreItemDetailURLPrefix() + |
| 44 | extension->id()) |
| 45 | : GURL::EmptyGURL(); |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 6534806 | 2013-01-15 07:27:22 | [diff] [blame] | 48 | // static |
rdevlin.cronin | e9a6260 | 2014-09-08 16:16:16 | [diff] [blame] | 49 | bool ManifestURL::SpecifiedHomepageURL(const Extension* extension) { |
rockot | 3b99b8e | 2014-10-14 21:54:35 | [diff] [blame] | 50 | return Get(extension, keys::kHomepageURL).is_valid(); |
rdevlin.cronin | e9a6260 | 2014-09-08 16:16:16 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // static |
[email protected] | 6534806 | 2013-01-15 07:27:22 | [diff] [blame] | 54 | const GURL& ManifestURL::GetUpdateURL(const Extension* extension) { |
rockot | 3b99b8e | 2014-10-14 21:54:35 | [diff] [blame] | 55 | return Get(extension, keys::kUpdateURL); |
[email protected] | 6534806 | 2013-01-15 07:27:22 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // static |
[email protected] | 4a5fe3e2 | 2013-06-04 07:06:38 | [diff] [blame] | 59 | bool ManifestURL::UpdatesFromGallery(const Extension* extension) { |
| 60 | return extension_urls::IsWebstoreUpdateUrl(GetUpdateURL(extension)); |
| 61 | } |
| 62 | |
| 63 | // static |
[email protected] | ffd2f79e | 2013-11-14 00:11:46 | [diff] [blame] | 64 | bool ManifestURL::UpdatesFromGallery(const base::DictionaryValue* manifest) { |
| 65 | std::string url; |
| 66 | if (!manifest->GetString(keys::kUpdateURL, &url)) |
| 67 | return false; |
| 68 | return extension_urls::IsWebstoreUpdateUrl(GURL(url)); |
| 69 | } |
| 70 | |
| 71 | // static |
[email protected] | 03bbb69 | 2014-06-12 22:57:14 | [diff] [blame] | 72 | const GURL& ManifestURL::GetAboutPage(const Extension* extension) { |
rockot | 3b99b8e | 2014-10-14 21:54:35 | [diff] [blame] | 73 | return Get(extension, keys::kAboutPage); |
[email protected] | 03bbb69 | 2014-06-12 22:57:14 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // static |
[email protected] | 3077f99 | 2013-01-17 00:40:12 | [diff] [blame] | 77 | const GURL ManifestURL::GetDetailsURL(const Extension* extension) { |
| 78 | return extension->from_webstore() ? |
| 79 | GURL(extension_urls::GetWebstoreItemDetailURLPrefix() + extension->id()) : |
| 80 | GURL::EmptyGURL(); |
| 81 | } |
| 82 | |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 83 | HomepageURLHandler::HomepageURLHandler() { |
| 84 | } |
| 85 | |
| 86 | HomepageURLHandler::~HomepageURLHandler() { |
| 87 | } |
| 88 | |
[email protected] | 428fac1 | 2013-12-05 21:38:49 | [diff] [blame] | 89 | bool HomepageURLHandler::Parse(Extension* extension, base::string16* error) { |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 90 | scoped_ptr<ManifestURL> manifest_url(new ManifestURL); |
| 91 | std::string homepage_url_str; |
[email protected] | ce613ba | 2013-01-30 00:01:18 | [diff] [blame] | 92 | if (!extension->manifest()->GetString(keys::kHomepageURL, |
| 93 | &homepage_url_str)) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 94 | *error = ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidHomepageURL, |
| 95 | std::string()); |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | manifest_url->url_ = GURL(homepage_url_str); |
| 99 | if (!manifest_url->url_.is_valid() || |
[email protected] | 91f568903 | 2013-08-22 01:43:33 | [diff] [blame] | 100 | !manifest_url->url_.SchemeIsHTTPOrHTTPS()) { |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 101 | *error = ErrorUtils::FormatErrorMessageUTF16( |
| 102 | errors::kInvalidHomepageURL, homepage_url_str); |
| 103 | return false; |
| 104 | } |
| 105 | extension->SetManifestData(keys::kHomepageURL, manifest_url.release()); |
| 106 | return true; |
| 107 | } |
| 108 | |
[email protected] | 9367eabc | 2013-03-01 01:29:29 | [diff] [blame] | 109 | const std::vector<std::string> HomepageURLHandler::Keys() const { |
| 110 | return SingleKey(keys::kHomepageURL); |
| 111 | } |
| 112 | |
[email protected] | 6534806 | 2013-01-15 07:27:22 | [diff] [blame] | 113 | UpdateURLHandler::UpdateURLHandler() { |
| 114 | } |
| 115 | |
| 116 | UpdateURLHandler::~UpdateURLHandler() { |
| 117 | } |
| 118 | |
[email protected] | 428fac1 | 2013-12-05 21:38:49 | [diff] [blame] | 119 | bool UpdateURLHandler::Parse(Extension* extension, base::string16* error) { |
[email protected] | 6534806 | 2013-01-15 07:27:22 | [diff] [blame] | 120 | scoped_ptr<ManifestURL> manifest_url(new ManifestURL); |
| 121 | std::string tmp_update_url; |
| 122 | |
[email protected] | ce613ba | 2013-01-30 00:01:18 | [diff] [blame] | 123 | if (!extension->manifest()->GetString(keys::kUpdateURL, &tmp_update_url)) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 124 | *error = ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidUpdateURL, |
| 125 | std::string()); |
[email protected] | 6534806 | 2013-01-15 07:27:22 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
| 129 | manifest_url->url_ = GURL(tmp_update_url); |
| 130 | if (!manifest_url->url_.is_valid() || |
| 131 | manifest_url->url_.has_ref()) { |
| 132 | *error = ErrorUtils::FormatErrorMessageUTF16( |
| 133 | errors::kInvalidUpdateURL, tmp_update_url); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | extension->SetManifestData(keys::kUpdateURL, manifest_url.release()); |
| 138 | return true; |
| 139 | } |
| 140 | |
[email protected] | 9367eabc | 2013-03-01 01:29:29 | [diff] [blame] | 141 | const std::vector<std::string> UpdateURLHandler::Keys() const { |
| 142 | return SingleKey(keys::kUpdateURL); |
| 143 | } |
| 144 | |
[email protected] | 03bbb69 | 2014-06-12 22:57:14 | [diff] [blame] | 145 | AboutPageHandler::AboutPageHandler() { |
| 146 | } |
| 147 | |
| 148 | AboutPageHandler::~AboutPageHandler() { |
| 149 | } |
| 150 | |
| 151 | bool AboutPageHandler::Parse(Extension* extension, base::string16* error) { |
| 152 | scoped_ptr<ManifestURL> manifest_url(new ManifestURL); |
| 153 | std::string about_str; |
| 154 | if (!extension->manifest()->GetString(keys::kAboutPage, &about_str)) { |
| 155 | *error = base::ASCIIToUTF16(errors::kInvalidAboutPage); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | GURL absolute(about_str); |
| 160 | if (absolute.is_valid()) { |
| 161 | *error = base::ASCIIToUTF16(errors::kInvalidAboutPageExpectRelativePath); |
| 162 | return false; |
| 163 | } |
| 164 | manifest_url->url_ = extension->GetResourceURL(about_str); |
| 165 | if (!manifest_url->url_.is_valid()) { |
| 166 | *error = base::ASCIIToUTF16(errors::kInvalidAboutPage); |
| 167 | return false; |
| 168 | } |
| 169 | extension->SetManifestData(keys::kAboutPage, manifest_url.release()); |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | bool AboutPageHandler::Validate(const Extension* extension, |
| 174 | std::string* error, |
| 175 | std::vector<InstallWarning>* warnings) const { |
| 176 | // Validate path to the options page. |
| 177 | if (!extensions::ManifestURL::GetAboutPage(extension).is_empty()) { |
| 178 | const base::FilePath about_path = |
| 179 | extensions::file_util::ExtensionURLToRelativeFilePath( |
| 180 | extensions::ManifestURL::GetAboutPage(extension)); |
| 181 | const base::FilePath path = |
| 182 | extension->GetResource(about_path).GetFilePath(); |
| 183 | if (path.empty() || !base::PathExists(path)) { |
| 184 | *error = l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_ABOUT_PAGE_FAILED, |
| 185 | about_path.LossyDisplayName()); |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | const std::vector<std::string> AboutPageHandler::Keys() const { |
| 193 | return SingleKey(keys::kAboutPage); |
| 194 | } |
| 195 | |
[email protected] | a405f6d | 2013-01-11 11:50:13 | [diff] [blame] | 196 | } // namespace extensions |