Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 "extensions/common/manifest_handlers/options_page_info.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/lazy_instance.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "extensions/common/api/extensions_manifest_types.h" | |
| 12 #include "extensions/common/error_utils.h" | |
| 13 #include "extensions/common/feature_switch.h" | |
| 14 #include "extensions/common/file_util.h" | |
| 15 #include "extensions/common/manifest_constants.h" | |
| 16 #include "extensions/strings/grit/extensions_strings.h" | |
| 17 #include "ui/base/l10n/l10n_util.h" | |
| 18 | |
| 19 using base::ASCIIToUTF16; | |
| 20 using base::DictionaryValue; | |
| 21 | |
| 22 namespace extensions { | |
| 23 | |
| 24 namespace keys = manifest_keys; | |
| 25 namespace values = manifest_values; | |
| 26 namespace errors = manifest_errors; | |
| 27 | |
| 28 using core_api::extensions_manifest_types::OptionsUI; | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 OptionsPageInfo* GetOptionsPageInfo(const Extension* extension) { | |
| 33 return static_cast<OptionsPageInfo*>( | |
| 34 extension->GetManifestData(keys::kOptionsUI)); | |
| 35 } | |
| 36 | |
| 37 // Parses |url_string| into a GURL |result| if it is a valid options page for | |
| 38 // this app/extension. If not, it returns the reason in |error|. Because this | |
| 39 // handles URLs for both "options_page" and "options_ui.page", the name of the | |
| 40 // manifest field must be provided in |manifest_field_name|. | |
| 41 bool ParseOptionsUrl(Extension* extension, | |
| 42 const std::string& url_string, | |
| 43 const std::string& manifest_field_name, | |
| 44 base::string16* error, | |
| 45 GURL* result) { | |
| 46 if (extension->is_hosted_app()) { | |
| 47 // Hosted apps require an absolute URL. | |
| 48 GURL options_url(url_string); | |
| 49 if (!options_url.is_valid() || !options_url.SchemeIsHTTPOrHTTPS()) { | |
| 50 *error = base::ASCIIToUTF16(errors::kInvalidOptionsPageInHostedApp); | |
| 51 return false; | |
| 52 } | |
| 53 *result = options_url; | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 // Otherwise the options URL should be inside the extension. | |
| 58 if (GURL(url_string).is_valid()) { | |
| 59 *error = base::ASCIIToUTF16(errors::kInvalidOptionsPageExpectUrlInPackage); | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 GURL resource_url = extension->GetResourceURL(url_string); | |
| 64 if (!resource_url.is_valid()) { | |
| 65 *error = ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidOptionsPage, | |
| 66 manifest_field_name); | |
| 67 return false; | |
| 68 } | |
| 69 *result = resource_url; | |
| 70 return true; | |
| 71 } | |
| 72 | |
| 73 } // namespace | |
| 74 | |
| 75 OptionsPageInfo::OptionsPageInfo(const GURL& options_url, | |
| 76 bool chrome_styles, | |
| 77 bool open_in_tab) | |
| 78 : options_url_(options_url), | |
| 79 chrome_styles_(chrome_styles), | |
| 80 open_in_tab_(open_in_tab) { | |
| 81 } | |
| 82 | |
| 83 OptionsPageInfo::~OptionsPageInfo() { | |
| 84 } | |
| 85 | |
| 86 // static | |
| 87 const GURL& OptionsPageInfo::GetOptionsPage(const Extension* extension) { | |
| 88 OptionsPageInfo* info = GetOptionsPageInfo(extension); | |
| 89 if (!info) { | |
| 90 return GURL::EmptyGURL(); | |
| 91 } | |
| 92 return info->options_url_; | |
| 93 } | |
| 94 | |
| 95 // static | |
| 96 bool OptionsPageInfo::HasOptionsPage(const Extension* extension) { | |
| 97 return !OptionsPageInfo::GetOptionsPage(extension).is_empty(); | |
| 98 } | |
| 99 | |
| 100 // static | |
| 101 bool OptionsPageInfo::ShouldUseChromeStyle(const Extension* extension) { | |
| 102 OptionsPageInfo* info = GetOptionsPageInfo(extension); | |
| 103 return info && info->chrome_styles_; | |
| 104 } | |
| 105 | |
| 106 // static | |
| 107 bool OptionsPageInfo::ShouldOpenInTab(const Extension* extension) { | |
| 108 OptionsPageInfo* info = GetOptionsPageInfo(extension); | |
| 109 return info && info->open_in_tab_; | |
| 110 } | |
| 111 | |
| 112 scoped_ptr<OptionsPageInfo> OptionsPageInfo::Create( | |
| 113 Extension* extension, | |
| 114 const base::Value* options_ui_value, | |
| 115 const std::string& options_page_string, | |
| 116 std::vector<InstallWarning>* install_warnings, | |
| 117 base::string16* error) { | |
| 118 GURL options_page; | |
| 119 GURL options_ui_page; | |
| 120 bool chrome_style = false; | |
| 121 bool open_in_tab = true; | |
| 122 | |
| 123 // Parse the options_ui object. | |
| 124 if (options_ui_value && | |
| 125 FeatureSwitch::embedded_extension_options()->IsEnabled()) { | |
| 126 base::string16 options_ui_error; | |
| 127 | |
| 128 scoped_ptr<OptionsUI> options_ui = | |
| 129 OptionsUI::FromValue(*options_ui_value, &options_ui_error); | |
| 130 if (!options_ui_error.empty()) { | |
| 131 // OptionsUI::FromValue populates |error| both when there are | |
|
Yoyo Zhou
2014/09/02 21:54:35
|error| -> |options_ui_error|
This is somewhat su
not at google - send to devlin
2014/09/03 16:11:29
This is better for forwards compatibility. Say we
| |
| 132 // errors (in which case |options_ui| will be NULL) and warnings | |
| 133 // (in which case |options_ui| will be valid). Either way, show it | |
| 134 // as an install warning. | |
| 135 install_warnings->push_back( | |
| 136 InstallWarning(base::UTF16ToASCII(options_ui_error))); | |
| 137 } | |
| 138 | |
| 139 if (options_ui) { | |
| 140 base::string16 options_parse_error; | |
| 141 if (!ParseOptionsUrl(extension, | |
| 142 options_ui->page, | |
| 143 keys::kOptionsUI, | |
| 144 &options_parse_error, | |
| 145 &options_ui_page)) { | |
| 146 install_warnings->push_back( | |
| 147 InstallWarning(base::UTF16ToASCII(options_parse_error))); | |
| 148 } | |
| 149 chrome_style = | |
| 150 options_ui->chrome_style.get() && *options_ui->chrome_style; | |
| 151 open_in_tab = options_ui->open_in_tab.get() && *options_ui->open_in_tab; | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 // Parse the legacy options_page entry. | |
| 156 if (!options_page_string.empty()) { | |
| 157 if (!ParseOptionsUrl(extension, | |
| 158 options_page_string, | |
| 159 keys::kOptionsPage, | |
| 160 error, | |
| 161 &options_page)) { | |
| 162 return scoped_ptr<OptionsPageInfo>(); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 // Prefer the options_ui.page url over the options_page url. | |
|
Yoyo Zhou
2014/09/02 21:54:35
nit: URL
ericzeng
2014/09/03 17:52:13
This block was removed per Devlin's comments.
| |
| 167 GURL options_url; | |
| 168 if (options_ui_page.is_valid()) | |
| 169 options_url = options_ui_page; | |
| 170 else | |
| 171 options_url = options_page; | |
| 172 | |
| 173 return make_scoped_ptr( | |
| 174 new OptionsPageInfo(options_url, chrome_style, open_in_tab)); | |
| 175 } | |
| 176 | |
| 177 OptionsPageManifestHandler::OptionsPageManifestHandler() { | |
| 178 } | |
| 179 | |
| 180 OptionsPageManifestHandler::~OptionsPageManifestHandler() { | |
| 181 } | |
| 182 | |
| 183 bool OptionsPageManifestHandler::Parse(Extension* extension, | |
| 184 base::string16* error) { | |
| 185 std::vector<InstallWarning> install_warnings; | |
| 186 const Manifest* manifest = extension->manifest(); | |
| 187 | |
| 188 std::string options_page_string; | |
| 189 if (manifest->HasPath(keys::kOptionsPage) && | |
| 190 !manifest->GetString(keys::kOptionsPage, &options_page_string)) { | |
| 191 *error = ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidOptionsPage, | |
| 192 keys::kOptionsPage); | |
| 193 return false; | |
| 194 } | |
| 195 | |
| 196 const base::Value* options_ui_value = NULL; | |
| 197 ignore_result(manifest->Get(keys::kOptionsUI, &options_ui_value)); | |
| 198 | |
| 199 scoped_ptr<OptionsPageInfo> info = | |
| 200 OptionsPageInfo::Create(extension, | |
| 201 options_ui_value, | |
| 202 options_page_string, | |
| 203 &install_warnings, | |
| 204 error); | |
| 205 if (!info) | |
| 206 return false; | |
| 207 | |
| 208 extension->AddInstallWarnings(install_warnings); | |
| 209 extension->SetManifestData(keys::kOptionsUI, info.release()); | |
| 210 return true; | |
| 211 } | |
| 212 | |
| 213 bool OptionsPageManifestHandler::Validate( | |
| 214 const Extension* extension, | |
| 215 std::string* error, | |
| 216 std::vector<InstallWarning>* warnings) const { | |
| 217 // Validate path to the options page. Don't check the URL for hosted apps, | |
| 218 // because they are expected to refer to an external URL. | |
| 219 if (!OptionsPageInfo::HasOptionsPage(extension) || extension->is_hosted_app()) | |
| 220 return true; | |
| 221 | |
| 222 base::FilePath options_path = | |
| 223 extensions::file_util::ExtensionURLToRelativeFilePath( | |
| 224 OptionsPageInfo::GetOptionsPage(extension)); | |
| 225 base::FilePath path = extension->GetResource(options_path).GetFilePath(); | |
| 226 if (path.empty() || !base::PathExists(path)) { | |
| 227 *error = l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_OPTIONS_PAGE_FAILED, | |
| 228 options_path.LossyDisplayName()); | |
| 229 return false; | |
| 230 } | |
| 231 return true; | |
| 232 } | |
| 233 | |
| 234 const std::vector<std::string> OptionsPageManifestHandler::Keys() const { | |
| 235 static const char* keys[] = {keys::kOptionsPage, keys::kOptionsUI}; | |
| 236 return std::vector<std::string>(keys, keys + arraysize(keys)); | |
| 237 } | |
| 238 | |
| 239 } // namespace extensions | |
| OLD | NEW |