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