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.
5 #include "extensions/common/manifest_url_handlers.h"
7 #include "base/files/file_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "extensions/common/error_utils.h"
14 #include "extensions/common/extension_urls.h"
15 #include "extensions/common/file_util.h"
16 #include "extensions/common/manifest.h"
17 #include "extensions/common/manifest_constants.h"
18 #include "extensions/common/manifest_handlers/shared_module_info.h"
19 #include "extensions/strings/grit/extensions_strings.h"
20 #include "ui/base/l10n/l10n_util.h"
22 namespace extensions
{
24 namespace keys
= manifest_keys
;
25 namespace errors
= manifest_errors
;
28 const GURL
& ManifestURL::Get(const Extension
* extension
,
29 const std::string
& key
) {
30 ManifestURL
* manifest_url
=
31 static_cast<ManifestURL
*>(extension
->GetManifestData(key
));
32 return manifest_url
? manifest_url
->url_
: GURL::EmptyGURL();
36 const GURL
ManifestURL::GetHomepageURL(const Extension
* extension
) {
37 const GURL
& homepage_url
= Get(extension
, keys::kHomepageURL
);
38 if (homepage_url
.is_valid())
40 bool use_webstore_url
= UpdatesFromGallery(extension
) &&
41 !SharedModuleInfo::IsSharedModule(extension
);
42 return use_webstore_url
43 ? GURL(extension_urls::GetWebstoreItemDetailURLPrefix() +
49 bool ManifestURL::SpecifiedHomepageURL(const Extension
* extension
) {
50 return Get(extension
, keys::kHomepageURL
).is_valid();
54 const GURL
& ManifestURL::GetUpdateURL(const Extension
* extension
) {
55 return Get(extension
, keys::kUpdateURL
);
59 bool ManifestURL::UpdatesFromGallery(const Extension
* extension
) {
60 return extension_urls::IsWebstoreUpdateUrl(GetUpdateURL(extension
));
64 bool ManifestURL::UpdatesFromGallery(const base::DictionaryValue
* manifest
) {
66 if (!manifest
->GetString(keys::kUpdateURL
, &url
))
68 return extension_urls::IsWebstoreUpdateUrl(GURL(url
));
72 const GURL
& ManifestURL::GetAboutPage(const Extension
* extension
) {
73 return Get(extension
, keys::kAboutPage
);
77 const GURL
ManifestURL::GetDetailsURL(const Extension
* extension
) {
78 return extension
->from_webstore() ?
79 GURL(extension_urls::GetWebstoreItemDetailURLPrefix() + extension
->id()) :
83 HomepageURLHandler::HomepageURLHandler() {
86 HomepageURLHandler::~HomepageURLHandler() {
89 bool HomepageURLHandler::Parse(Extension
* extension
, base::string16
* error
) {
90 scoped_ptr
<ManifestURL
> manifest_url(new ManifestURL
);
91 std::string homepage_url_str
;
92 if (!extension
->manifest()->GetString(keys::kHomepageURL
,
94 *error
= ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidHomepageURL
,
98 manifest_url
->url_
= GURL(homepage_url_str
);
99 if (!manifest_url
->url_
.is_valid() ||
100 !manifest_url
->url_
.SchemeIsHTTPOrHTTPS()) {
101 *error
= ErrorUtils::FormatErrorMessageUTF16(
102 errors::kInvalidHomepageURL
, homepage_url_str
);
105 extension
->SetManifestData(keys::kHomepageURL
, manifest_url
.release());
109 const std::vector
<std::string
> HomepageURLHandler::Keys() const {
110 return SingleKey(keys::kHomepageURL
);
113 UpdateURLHandler::UpdateURLHandler() {
116 UpdateURLHandler::~UpdateURLHandler() {
119 bool UpdateURLHandler::Parse(Extension
* extension
, base::string16
* error
) {
120 scoped_ptr
<ManifestURL
> manifest_url(new ManifestURL
);
121 std::string tmp_update_url
;
123 if (!extension
->manifest()->GetString(keys::kUpdateURL
, &tmp_update_url
)) {
124 *error
= ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidUpdateURL
,
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
);
137 extension
->SetManifestData(keys::kUpdateURL
, manifest_url
.release());
141 const std::vector
<std::string
> UpdateURLHandler::Keys() const {
142 return SingleKey(keys::kUpdateURL
);
145 AboutPageHandler::AboutPageHandler() {
148 AboutPageHandler::~AboutPageHandler() {
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
);
159 GURL
absolute(about_str
);
160 if (absolute
.is_valid()) {
161 *error
= base::ASCIIToUTF16(errors::kInvalidAboutPageExpectRelativePath
);
164 manifest_url
->url_
= extension
->GetResourceURL(about_str
);
165 if (!manifest_url
->url_
.is_valid()) {
166 *error
= base::ASCIIToUTF16(errors::kInvalidAboutPage
);
169 extension
->SetManifestData(keys::kAboutPage
, manifest_url
.release());
173 bool AboutPageHandler::Validate(const Extension
* extension
,
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());
192 const std::vector
<std::string
> AboutPageHandler::Keys() const {
193 return SingleKey(keys::kAboutPage
);
196 } // namespace extensions