OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/extensions/api/file_browser_handlers/file_browser_handle r.h" | 5 #include "chrome/common/extensions/api/file_browser_handlers/file_browser_handle r.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 } | 131 } |
132 | 132 |
133 FileBrowserHandlerParser::FileBrowserHandlerParser() { | 133 FileBrowserHandlerParser::FileBrowserHandlerParser() { |
134 } | 134 } |
135 | 135 |
136 FileBrowserHandlerParser::~FileBrowserHandlerParser() { | 136 FileBrowserHandlerParser::~FileBrowserHandlerParser() { |
137 } | 137 } |
138 | 138 |
139 namespace { | 139 namespace { |
140 | 140 |
141 FileBrowserHandler* LoadFileBrowserHandler( | 141 std::unique_ptr<FileBrowserHandler> LoadFileBrowserHandler( |
142 const std::string& extension_id, | 142 const std::string& extension_id, |
143 const base::DictionaryValue* file_browser_handler, | 143 const base::DictionaryValue* file_browser_handler, |
144 base::string16* error) { | 144 base::string16* error) { |
145 std::unique_ptr<FileBrowserHandler> result(new FileBrowserHandler()); | 145 std::unique_ptr<FileBrowserHandler> result(new FileBrowserHandler()); |
146 result->set_extension_id(extension_id); | 146 result->set_extension_id(extension_id); |
147 | 147 |
148 std::string handler_id; | 148 std::string handler_id; |
149 // Read the file action |id| (mandatory). | 149 // Read the file action |id| (mandatory). |
150 if (!file_browser_handler->HasKey(keys::kPageActionId) || | 150 if (!file_browser_handler->HasKey(keys::kPageActionId) || |
151 !file_browser_handler->GetString(keys::kPageActionId, &handler_id)) { | 151 !file_browser_handler->GetString(keys::kPageActionId, &handler_id)) { |
152 *error = base::ASCIIToUTF16(errors::kInvalidPageActionId); | 152 *error = base::ASCIIToUTF16(errors::kInvalidPageActionId); |
153 return NULL; | 153 return std::unique_ptr<FileBrowserHandler>(); |
Devlin
2017/03/02 16:04:10
does return nullptr work?
lazyboy
2017/03/03 02:40:38
Yes, thanks!
| |
154 } | 154 } |
155 result->set_id(handler_id); | 155 result->set_id(handler_id); |
156 | 156 |
157 // Read the page action title from |default_title| (mandatory). | 157 // Read the page action title from |default_title| (mandatory). |
158 std::string title; | 158 std::string title; |
159 if (!file_browser_handler->HasKey(keys::kPageActionDefaultTitle) || | 159 if (!file_browser_handler->HasKey(keys::kPageActionDefaultTitle) || |
160 !file_browser_handler->GetString(keys::kPageActionDefaultTitle, &title)) { | 160 !file_browser_handler->GetString(keys::kPageActionDefaultTitle, &title)) { |
161 *error = base::ASCIIToUTF16(errors::kInvalidPageActionDefaultTitle); | 161 *error = base::ASCIIToUTF16(errors::kInvalidPageActionDefaultTitle); |
162 return NULL; | 162 return std::unique_ptr<FileBrowserHandler>(); |
163 } | 163 } |
164 result->set_title(title); | 164 result->set_title(title); |
165 | 165 |
166 // Initialize access permissions (optional). | 166 // Initialize access permissions (optional). |
167 const base::ListValue* access_list_value = NULL; | 167 const base::ListValue* access_list_value = nullptr; |
168 if (file_browser_handler->HasKey(keys::kFileAccessList)) { | 168 if (file_browser_handler->HasKey(keys::kFileAccessList)) { |
169 if (!file_browser_handler->GetList(keys::kFileAccessList, | 169 if (!file_browser_handler->GetList(keys::kFileAccessList, |
170 &access_list_value) || | 170 &access_list_value) || |
171 access_list_value->empty()) { | 171 access_list_value->empty()) { |
172 *error = base::ASCIIToUTF16(errors::kInvalidFileAccessList); | 172 *error = base::ASCIIToUTF16(errors::kInvalidFileAccessList); |
173 return NULL; | 173 return std::unique_ptr<FileBrowserHandler>(); |
174 } | 174 } |
175 for (size_t i = 0; i < access_list_value->GetSize(); ++i) { | 175 for (size_t i = 0; i < access_list_value->GetSize(); ++i) { |
176 std::string access; | 176 std::string access; |
177 if (!access_list_value->GetString(i, &access) || | 177 if (!access_list_value->GetString(i, &access) || |
178 result->AddFileAccessPermission(access)) { | 178 result->AddFileAccessPermission(access)) { |
179 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( | 179 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( |
180 errors::kInvalidFileAccessValue, base::IntToString(i)); | 180 errors::kInvalidFileAccessValue, base::IntToString(i)); |
181 return NULL; | 181 return std::unique_ptr<FileBrowserHandler>(); |
182 } | 182 } |
183 } | 183 } |
184 } | 184 } |
185 if (!result->ValidateFileAccessPermissions()) { | 185 if (!result->ValidateFileAccessPermissions()) { |
186 *error = base::ASCIIToUTF16(errors::kInvalidFileAccessList); | 186 *error = base::ASCIIToUTF16(errors::kInvalidFileAccessList); |
187 return NULL; | 187 return std::unique_ptr<FileBrowserHandler>(); |
188 } | 188 } |
189 | 189 |
190 // Initialize file filters (mandatory, unless "create" access is specified, | 190 // Initialize file filters (mandatory, unless "create" access is specified, |
191 // in which case is ignored). The list can be empty. | 191 // in which case is ignored). The list can be empty. |
192 if (!result->HasCreateAccessPermission()) { | 192 if (!result->HasCreateAccessPermission()) { |
193 const base::ListValue* file_filters = NULL; | 193 const base::ListValue* file_filters = nullptr; |
194 if (!file_browser_handler->HasKey(keys::kFileFilters) || | 194 if (!file_browser_handler->HasKey(keys::kFileFilters) || |
195 !file_browser_handler->GetList(keys::kFileFilters, &file_filters)) { | 195 !file_browser_handler->GetList(keys::kFileFilters, &file_filters)) { |
196 *error = base::ASCIIToUTF16(errors::kInvalidFileFiltersList); | 196 *error = base::ASCIIToUTF16(errors::kInvalidFileFiltersList); |
197 return NULL; | 197 return std::unique_ptr<FileBrowserHandler>(); |
198 } | 198 } |
199 for (size_t i = 0; i < file_filters->GetSize(); ++i) { | 199 for (size_t i = 0; i < file_filters->GetSize(); ++i) { |
200 std::string filter; | 200 std::string filter; |
201 if (!file_filters->GetString(i, &filter)) { | 201 if (!file_filters->GetString(i, &filter)) { |
202 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( | 202 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( |
203 errors::kInvalidFileFilterValue, base::IntToString(i)); | 203 errors::kInvalidFileFilterValue, base::IntToString(i)); |
204 return NULL; | 204 return std::unique_ptr<FileBrowserHandler>(); |
205 } | 205 } |
206 filter = base::ToLowerASCII(filter); | 206 filter = base::ToLowerASCII(filter); |
207 if (!base::StartsWith(filter, std::string(url::kFileSystemScheme) + ':', | 207 if (!base::StartsWith(filter, std::string(url::kFileSystemScheme) + ':', |
208 base::CompareCase::SENSITIVE)) { | 208 base::CompareCase::SENSITIVE)) { |
209 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( | 209 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( |
210 errors::kInvalidURLPatternError, filter); | 210 errors::kInvalidURLPatternError, filter); |
211 return NULL; | 211 return std::unique_ptr<FileBrowserHandler>(); |
212 } | 212 } |
213 // The user inputs filesystem:*; we don't actually implement scheme | 213 // The user inputs filesystem:*; we don't actually implement scheme |
214 // wildcards in URLPattern, so transform to what will match correctly. | 214 // wildcards in URLPattern, so transform to what will match correctly. |
215 filter.replace(0, 11, "chrome-extension://*/"); | 215 filter.replace(0, 11, "chrome-extension://*/"); |
216 URLPattern pattern(URLPattern::SCHEME_EXTENSION); | 216 URLPattern pattern(URLPattern::SCHEME_EXTENSION); |
217 if (pattern.Parse(filter) != URLPattern::PARSE_SUCCESS) { | 217 if (pattern.Parse(filter) != URLPattern::PARSE_SUCCESS) { |
218 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( | 218 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( |
219 errors::kInvalidURLPatternError, filter); | 219 errors::kInvalidURLPatternError, filter); |
220 return NULL; | 220 return std::unique_ptr<FileBrowserHandler>(); |
221 } | 221 } |
222 std::string path = pattern.path(); | 222 std::string path = pattern.path(); |
223 bool allowed = path == "/*" || path == "/*.*" || | 223 bool allowed = path == "/*" || path == "/*.*" || |
224 (path.compare(0, 3, "/*.") == 0 && | 224 (path.compare(0, 3, "/*.") == 0 && |
225 path.find_first_of('*', 3) == std::string::npos); | 225 path.find_first_of('*', 3) == std::string::npos); |
226 if (!allowed) { | 226 if (!allowed) { |
227 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( | 227 *error = extensions::ErrorUtils::FormatErrorMessageUTF16( |
228 errors::kInvalidURLPatternError, filter); | 228 errors::kInvalidURLPatternError, filter); |
229 return NULL; | 229 return std::unique_ptr<FileBrowserHandler>(); |
230 } | 230 } |
231 result->AddPattern(pattern); | 231 result->AddPattern(pattern); |
232 } | 232 } |
233 } | 233 } |
234 | 234 |
235 std::string default_icon; | 235 std::string default_icon; |
236 // Read the file browser action |default_icon| (optional). | 236 // Read the file browser action |default_icon| (optional). |
237 if (file_browser_handler->HasKey(keys::kPageActionDefaultIcon)) { | 237 if (file_browser_handler->HasKey(keys::kPageActionDefaultIcon)) { |
238 if (!file_browser_handler->GetString( | 238 if (!file_browser_handler->GetString( |
239 keys::kPageActionDefaultIcon, &default_icon) || | 239 keys::kPageActionDefaultIcon, &default_icon) || |
240 default_icon.empty()) { | 240 default_icon.empty()) { |
241 *error = base::ASCIIToUTF16(errors::kInvalidPageActionIconPath); | 241 *error = base::ASCIIToUTF16(errors::kInvalidPageActionIconPath); |
242 return NULL; | 242 return std::unique_ptr<FileBrowserHandler>(); |
243 } | 243 } |
244 result->set_icon_path(default_icon); | 244 result->set_icon_path(default_icon); |
245 } | 245 } |
246 | 246 |
247 return result.release(); | 247 return result; |
248 } | 248 } |
249 | 249 |
250 // Loads FileBrowserHandlers from |extension_actions| into a list in |result|. | 250 // Loads FileBrowserHandlers from |extension_actions| into a list in |result|. |
251 bool LoadFileBrowserHandlers( | 251 bool LoadFileBrowserHandlers( |
252 const std::string& extension_id, | 252 const std::string& extension_id, |
253 const base::ListValue* extension_actions, | 253 const base::ListValue* extension_actions, |
254 FileBrowserHandler::List* result, | 254 FileBrowserHandler::List* result, |
255 base::string16* error) { | 255 base::string16* error) { |
256 for (const auto& entry : *extension_actions) { | 256 for (const auto& entry : *extension_actions) { |
257 base::DictionaryValue* dict; | 257 base::DictionaryValue* dict; |
258 if (!entry->GetAsDictionary(&dict)) { | 258 if (!entry->GetAsDictionary(&dict)) { |
259 *error = base::ASCIIToUTF16(errors::kInvalidFileBrowserHandler); | 259 *error = base::ASCIIToUTF16(errors::kInvalidFileBrowserHandler); |
260 return false; | 260 return false; |
261 } | 261 } |
262 std::unique_ptr<FileBrowserHandler> action( | 262 std::unique_ptr<FileBrowserHandler> action = |
263 LoadFileBrowserHandler(extension_id, dict, error)); | 263 LoadFileBrowserHandler(extension_id, dict, error); |
264 if (!action.get()) | 264 if (!action) |
265 return false; // Failed to parse file browser action definition. | 265 return false; // Failed to parse file browser action definition. |
266 result->push_back(linked_ptr<FileBrowserHandler>(action.release())); | 266 result->push_back(std::move(action)); |
267 } | 267 } |
268 return true; | 268 return true; |
269 } | 269 } |
270 | 270 |
271 } // namespace | 271 } // namespace |
272 | 272 |
273 bool FileBrowserHandlerParser::Parse(extensions::Extension* extension, | 273 bool FileBrowserHandlerParser::Parse(extensions::Extension* extension, |
274 base::string16* error) { | 274 base::string16* error) { |
275 const base::Value* file_browser_handlers_value = nullptr; | 275 const base::Value* file_browser_handlers_value = nullptr; |
276 if (!extension->manifest()->Get(keys::kFileBrowserHandlers, | 276 if (!extension->manifest()->Get(keys::kFileBrowserHandlers, |
(...skipping 22 matching lines...) Expand all Loading... | |
299 return false; // Failed to parse file browser actions definition. | 299 return false; // Failed to parse file browser actions definition. |
300 } | 300 } |
301 | 301 |
302 extension->SetManifestData(keys::kFileBrowserHandlers, std::move(info)); | 302 extension->SetManifestData(keys::kFileBrowserHandlers, std::move(info)); |
303 return true; | 303 return true; |
304 } | 304 } |
305 | 305 |
306 const std::vector<std::string> FileBrowserHandlerParser::Keys() const { | 306 const std::vector<std::string> FileBrowserHandlerParser::Keys() const { |
307 return SingleKey(keys::kFileBrowserHandlers); | 307 return SingleKey(keys::kFileBrowserHandlers); |
308 } | 308 } |
OLD | NEW |