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 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 const GURL& options_ui_page, | |
76 bool chrome_styles, | |
77 bool open_in_tab) | |
78 : options_page_(options_page), | |
79 options_ui_page_(options_ui_page), | |
80 chrome_styles_(chrome_styles), | |
81 open_in_tab_(open_in_tab) { | |
82 } | |
83 | |
84 OptionsPageInfo::~OptionsPageInfo() { | |
85 } | |
86 | |
87 // static | |
88 const GURL& OptionsPageInfo::GetOptionsPage(const Extension* extension) { | |
89 OptionsPageInfo* info = GetOptionsPageInfo(extension); | |
90 if (!info) { | |
91 return GURL::EmptyGURL(); | |
92 } | |
93 | |
94 if (info->options_ui_page_.is_valid()) { | |
95 return info->options_ui_page_; | |
96 } | |
97 return info->options_page_; | |
98 } | |
99 | |
100 // static | |
101 bool OptionsPageInfo::HasOptionsPage(const Extension* extension) { | |
102 return !OptionsPageInfo::GetOptionsPage(extension).is_empty(); | |
103 } | |
104 | |
105 // static | |
106 bool OptionsPageInfo::ShouldUseChromeStyle(const Extension* extension) { | |
107 OptionsPageInfo* info = GetOptionsPageInfo(extension); | |
108 return info && info->chrome_styles_; | |
109 } | |
110 | |
111 // static | |
112 bool OptionsPageInfo::ShouldOpenInTab(const Extension* extension) { | |
113 OptionsPageInfo* info = GetOptionsPageInfo(extension); | |
114 return info && info->open_in_tab_; | |
115 } | |
116 | |
117 scoped_ptr<OptionsPageInfo> OptionsPageInfo::Create( | |
118 Extension* extension, | |
119 const base::Value* options_ui_value, | |
120 const std::string& options_page_string, | |
121 std::vector<InstallWarning>* install_warnings, | |
122 base::string16* error) { | |
123 GURL options_page; | |
124 GURL options_ui_page; | |
125 bool chrome_style = false; | |
126 bool open_in_tab = true; | |
127 | |
128 // Parse the options_ui object | |
129 if (options_ui_value && | |
130 FeatureSwitch::embedded_extension_options()->IsEnabled()) { | |
131 base::string16 options_ui_error; | |
132 | |
133 scoped_ptr<OptionsUI> options_ui = | |
134 OptionsUI::FromValue(*options_ui_value, &options_ui_error); | |
135 if (options_ui) { | |
136 if (!ParseOptionsUrl(extension, | |
137 options_ui->page, | |
138 keys::kOptionsUI, | |
139 &options_ui_error, | |
not at google - send to devlin
2014/08/30 00:48:11
This overrides any existing value for |options_ui_
ericzeng
2014/09/02 18:23:37
Done.
| |
140 &options_ui_page)) { | |
141 install_warnings->push_back( | |
142 InstallWarning(base::UTF16ToASCII(options_ui_error))); | |
143 } | |
144 chrome_style = | |
145 options_ui->chrome_style.get() && *options_ui->chrome_style; | |
146 open_in_tab = options_ui->open_in_tab.get() && *options_ui->open_in_tab; | |
147 } else { | |
148 install_warnings->push_back( | |
149 InstallWarning(base::UTF16ToASCII(options_ui_error))); | |
150 } | |
151 } | |
152 | |
153 // Parse the options_page entry | |
154 if (!options_page_string.empty()) { | |
155 if (!ParseOptionsUrl(extension, | |
156 options_page_string, | |
157 keys::kOptionsPage, | |
158 error, | |
159 &options_page)) { | |
160 return scoped_ptr<OptionsPageInfo>(); | |
161 } | |
162 } | |
163 return make_scoped_ptr(new OptionsPageInfo( | |
164 options_page, options_ui_page, chrome_style, open_in_tab)); | |
165 } | |
166 | |
167 OptionsPageManifestHandler::OptionsPageManifestHandler() { | |
168 } | |
169 | |
170 OptionsPageManifestHandler::~OptionsPageManifestHandler() { | |
171 } | |
172 | |
173 bool OptionsPageManifestHandler::Parse(Extension* extension, | |
174 base::string16* error) { | |
175 std::vector<InstallWarning> install_warnings; | |
176 const Manifest* manifest = extension->manifest(); | |
177 | |
178 std::string options_page_string; | |
179 if (manifest->HasPath(keys::kOptionsPage) && | |
180 !manifest->GetString(keys::kOptionsPage, &options_page_string)) { | |
181 *error = ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidOptionsPage, | |
182 keys::kOptionsPage); | |
183 return false; | |
184 } | |
185 | |
186 const base::Value* options_ui_value = NULL; | |
187 ignore_result(manifest->Get(keys::kOptionsUI, &options_ui_value)); | |
188 | |
189 scoped_ptr<OptionsPageInfo> info = | |
190 OptionsPageInfo::Create(extension, | |
191 options_ui_value, | |
192 options_page_string, | |
193 &install_warnings, | |
194 error); | |
195 if (!info) | |
196 return false; | |
197 | |
198 extension->AddInstallWarnings(install_warnings); | |
199 extension->SetManifestData(keys::kOptionsUI, info.release()); | |
200 return true; | |
201 } | |
202 | |
203 bool OptionsPageManifestHandler::Validate( | |
204 const Extension* extension, | |
205 std::string* error, | |
206 std::vector<InstallWarning>* warnings) const { | |
207 // Validate path to the options page. Don't check the URL for hosted apps, | |
208 // because they are expected to refer to an external URL. | |
209 if (!OptionsPageInfo::HasOptionsPage(extension) || extension->is_hosted_app()) | |
210 return true; | |
211 | |
212 base::FilePath options_path = | |
213 extensions::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 |