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 GURL absolute(url_string); | |
not at google - send to devlin
2014/08/29 22:33:37
Inline this?
if (GURL(url_string).is_valid()) { .
Yoyo Zhou
2014/08/29 23:06:27
Add a comment here along the lines of: Otherwise,
ericzeng
2014/08/29 23:54:04
Done.
| |
58 if (absolute.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_page, | |
76 const GURL& options_ui_page, | |
77 bool chrome_styles, | |
78 bool open_in_tab) | |
79 : options_page_(options_page), | |
Yoyo Zhou
2014/08/29 23:06:27
Do we really need to store both options_page and o
ericzeng
2014/09/02 18:23:37
As is I don't think both need to be stored. I thou
| |
80 options_ui_page_(options_ui_page), | |
81 chrome_styles_(chrome_styles), | |
82 open_in_tab_(open_in_tab) { | |
83 } | |
84 | |
85 OptionsPageInfo::~OptionsPageInfo() { | |
86 } | |
87 | |
88 // static | |
89 const GURL& OptionsPageInfo::GetOptionsPage(const Extension* extension) { | |
90 OptionsPageInfo* info = GetOptionsPageInfo(extension); | |
91 if (!info) { | |
92 return GURL::EmptyGURL(); | |
93 } | |
94 | |
95 if (info->options_ui_page_.is_valid()) { | |
96 return info->options_ui_page_; | |
97 } | |
98 return info->options_page_; | |
99 } | |
100 | |
101 // static | |
102 bool OptionsPageInfo::HasOptionsPage(const Extension* extension) { | |
103 return !OptionsPageInfo::GetOptionsPage(extension).is_empty(); | |
104 } | |
105 | |
106 // static | |
107 bool OptionsPageInfo::ShouldUseChromeStyle(const Extension* extension) { | |
108 OptionsPageInfo* info = GetOptionsPageInfo(extension); | |
109 if (!info) | |
110 return false; | |
111 return info->chrome_styles_; | |
not at google - send to devlin
2014/08/29 22:33:37
You could do
return info && info->chrome_styles_;
ericzeng
2014/08/29 23:54:04
Done.
| |
112 } | |
113 | |
114 // static | |
115 bool OptionsPageInfo::ShouldOpenInTab(const Extension* extension) { | |
116 OptionsPageInfo* info = GetOptionsPageInfo(extension); | |
117 if (!info) | |
118 return false; | |
119 return info->open_in_tab_; | |
not at google - send to devlin
2014/08/29 22:33:37
return info && info->open_in_tab_;
ericzeng
2014/08/29 23:54:03
Done.
| |
120 } | |
121 | |
122 scoped_ptr<OptionsPageInfo> OptionsPageInfo::Parse( | |
123 Extension* extension, | |
124 const base::Value* options_ui_value, | |
125 const std::string& options_page_string, | |
126 std::vector<InstallWarning>* install_warnings, | |
127 base::string16* error) { | |
not at google - send to devlin
2014/08/29 22:33:37
I wouldn't even pass |error| in here, to avoid the
ericzeng
2014/08/29 23:54:04
I would do that but since options_page is also han
| |
128 GURL options_page; | |
129 GURL options_ui_page; | |
130 bool chrome_style = false; | |
131 bool open_in_tab = !FeatureSwitch::embedded_extension_options()->IsEnabled(); | |
not at google - send to devlin
2014/08/29 22:33:37
I think it would be simpler to initialise this to
ericzeng
2014/08/29 23:54:03
Done.
ericzeng
2014/09/02 21:08:46
I just realized that this doesn't work, because it
| |
132 | |
133 // Parse the options_ui object | |
134 if (options_ui_value) { | |
135 scoped_ptr<OptionsUI> options_ui = | |
136 OptionsUI::FromValue(*options_ui_value, error); | |
137 if (options_ui) { | |
138 if (!ParseOptionsUrl(extension, | |
139 options_ui->page, | |
140 keys::kOptionsUI, | |
141 error, | |
Yoyo Zhou
2014/08/29 23:06:27
Why not pass in a different string from error, so
ericzeng
2014/09/02 18:23:37
Done.
| |
142 &options_ui_page)) { | |
143 // Use an install warning instead of an error for options_ui.page | |
Yoyo Zhou
2014/08/29 23:06:27
nit: comments end in .
| |
144 install_warnings->push_back(InstallWarning(base::UTF16ToASCII(*error))); | |
145 *error = base::string16(); | |
146 } | |
147 chrome_style = options_ui->chrome_style.get() && | |
148 *options_ui->chrome_style && | |
149 FeatureSwitch::embedded_extension_options()->IsEnabled(); | |
150 open_in_tab |= options_ui->open_in_tab.get() && *options_ui->open_in_tab; | |
not at google - send to devlin
2014/08/29 22:33:37
All this logic can be simplified if open_in_tab we
Yoyo Zhou
2014/08/29 23:06:27
Why does this look different from the chrome_style
ericzeng
2014/08/29 23:54:04
Done.
ericzeng
2014/09/02 18:23:37
I changed how both booleans are handled, but they
| |
151 } else { | |
152 install_warnings->push_back(InstallWarning(ErrorUtils::FormatErrorMessage( | |
153 errors::kInvalidOptionsPage, keys::kOptionsUI))); | |
not at google - send to devlin
2014/08/29 22:33:37
I think that the OptionsUI::FromValue call generat
ericzeng
2014/08/29 23:54:03
Done.
| |
154 } | |
155 } | |
156 | |
157 // Parse the options_page entry | |
Yoyo Zhou
2014/08/29 23:06:27
Say "legacy" in here
ericzeng
2014/09/02 18:23:37
Done.
| |
158 if (!options_page_string.empty()) { | |
159 if (!ParseOptionsUrl(extension, | |
160 options_page_string, | |
161 keys::kOptionsPage, | |
162 error, | |
163 &options_page)) { | |
164 return scoped_ptr<OptionsPageInfo>(); | |
165 } | |
166 } | |
167 return make_scoped_ptr(new OptionsPageInfo( | |
168 options_page, options_ui_page, chrome_style, open_in_tab)); | |
169 } | |
170 | |
171 OptionsPageManifestHandler::OptionsPageManifestHandler() { | |
172 } | |
173 | |
174 OptionsPageManifestHandler::~OptionsPageManifestHandler() { | |
175 } | |
176 | |
177 bool OptionsPageManifestHandler::Parse(Extension* extension, | |
178 base::string16* error) { | |
179 std::vector<InstallWarning> install_warnings; | |
180 const Manifest* manifest = extension->manifest(); | |
181 | |
182 std::string options_page_string; | |
183 if (manifest->HasPath(keys::kOptionsPage) && | |
184 !manifest->GetString(keys::kOptionsPage, &options_page_string)) { | |
185 *error = ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidOptionsPage, | |
186 keys::kOptionsPage); | |
187 return false; | |
188 } | |
189 | |
190 const base::Value* options_ui_value = NULL; | |
191 if (manifest->HasPath(keys::kOptionsUI) && | |
192 !manifest->Get(keys::kOptionsUI, &options_ui_value)) { | |
not at google - send to devlin
2014/08/29 22:33:37
Like we discussed (i.e. like you noticed) the HasP
ericzeng
2014/08/29 23:54:04
Done.
| |
193 install_warnings.push_back(InstallWarning(ErrorUtils::FormatErrorMessage( | |
194 errors::kInvalidOptionsPage, keys::kOptionsUI))); | |
195 } | |
196 | |
197 scoped_ptr<OptionsPageInfo> info = OptionsPageInfo::Parse(extension, | |
198 options_ui_value, | |
199 options_page_string, | |
200 &install_warnings, | |
201 error); | |
202 if (!info) | |
203 return false; | |
204 | |
205 extension->AddInstallWarnings(install_warnings); | |
206 extension->SetManifestData(keys::kOptionsUI, info.release()); | |
207 return true; | |
208 } | |
209 | |
210 bool OptionsPageManifestHandler::Validate( | |
211 const Extension* extension, | |
212 std::string* error, | |
213 std::vector<InstallWarning>* warnings) const { | |
214 // Validate path to the options page. Don't check the URL for hosted apps, | |
215 // because they are expected to refer to an external URL. | |
216 if (!OptionsPageInfo::HasOptionsPage(extension) || extension->is_hosted_app()) | |
217 return true; | |
218 | |
219 base::FilePath options_path = | |
220 extensions::file_util::ExtensionURLToRelativeFilePath( | |
221 OptionsPageInfo::GetOptionsPage(extension)); | |
222 base::FilePath path = extension->GetResource(options_path).GetFilePath(); | |
223 if (path.empty() || !base::PathExists(path)) { | |
224 *error = l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_OPTIONS_PAGE_FAILED, | |
225 options_path.LossyDisplayName()); | |
226 return false; | |
227 } | |
228 return true; | |
229 } | |
230 | |
231 const std::vector<std::string> OptionsPageManifestHandler::Keys() const { | |
232 static const char* keys[] = {keys::kOptionsPage, keys::kOptionsUI}; | |
233 return std::vector<std::string>(keys, keys + arraysize(keys)); | |
234 } | |
235 | |
236 } // namespace extensions | |
OLD | NEW |