OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/common/extensions/api/url_handlers/url_handlers_parser.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 12 #include "chrome/common/extensions/manifest.h" |
| 13 #include "extensions/common/error_utils.h" |
| 14 |
| 15 namespace mkeys = extension_manifest_keys; |
| 16 namespace merrors = extension_manifest_errors; |
| 17 |
| 18 namespace extensions { |
| 19 |
| 20 UrlHandlerInfo::UrlHandlerInfo() { |
| 21 } |
| 22 |
| 23 UrlHandlerInfo::~UrlHandlerInfo() { |
| 24 } |
| 25 |
| 26 UrlHandlers::UrlHandlers() { |
| 27 } |
| 28 |
| 29 UrlHandlers::~UrlHandlers() { |
| 30 } |
| 31 |
| 32 // static |
| 33 const std::vector<UrlHandlerInfo>* UrlHandlers::GetUrlHandlers( |
| 34 const Extension* extension) { |
| 35 UrlHandlers* info = static_cast<UrlHandlers*>( |
| 36 extension->GetManifestData(mkeys::kUrlHandlers)); |
| 37 return info ? &info->handlers : NULL; |
| 38 } |
| 39 |
| 40 UrlHandlersParser::UrlHandlersParser() { |
| 41 } |
| 42 |
| 43 UrlHandlersParser::~UrlHandlersParser() { |
| 44 } |
| 45 |
| 46 bool ParseUrlHandler(const Extension* app, |
| 47 const std::string& handler_id, |
| 48 const DictionaryValue& handler_info, |
| 49 std::vector<UrlHandlerInfo>* url_handlers, |
| 50 string16* error) { |
| 51 DCHECK(error); |
| 52 UrlHandlerInfo handler; |
| 53 |
| 54 handler.app = app; |
| 55 handler.id = handler_id; |
| 56 |
| 57 if (!handler_info.GetString(mkeys::kUrlHandlerTitle, &handler.title)) { |
| 58 *error = ASCIIToUTF16(merrors::kInvalidUrlHandlerTitle); |
| 59 return false; |
| 60 } |
| 61 |
| 62 const ListValue* manif_patterns = NULL; |
| 63 if (!handler_info.GetList(mkeys::kMatches, &manif_patterns) || |
| 64 manif_patterns->GetSize() == 0) { |
| 65 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 66 merrors::kInvalidUrlHandlerPattern, handler_id); |
| 67 return false; |
| 68 } |
| 69 |
| 70 for (const auto& manif_pattern: *manif_patterns) { |
| 71 std::string str_pattern; |
| 72 manif_pattern->GetAsString(&str_pattern); |
| 73 URLPattern pattern(URLPattern::SCHEME_HTTP | |
| 74 URLPattern::SCHEME_HTTPS); |
| 75 if (pattern.Parse(str_pattern) != URLPattern::PARSE_SUCCESS) { |
| 76 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 77 merrors::kInvalidUrlHandlerPatternElement, handler_id); |
| 78 return false; |
| 79 } |
| 80 handler.patterns.AddPattern(pattern); |
| 81 } |
| 82 |
| 83 url_handlers->push_back(handler); |
| 84 |
| 85 return true; |
| 86 } |
| 87 |
| 88 bool UrlHandlersParser::Parse(Extension* extension, string16* error) { |
| 89 scoped_ptr<UrlHandlers> info(new UrlHandlers); |
| 90 const DictionaryValue* all_handlers = NULL; |
| 91 if (!extension->manifest()->GetDictionary( |
| 92 mkeys::kUrlHandlers, &all_handlers)) { |
| 93 *error = ASCIIToUTF16(merrors::kInvalidUrlHandlers); |
| 94 return false; |
| 95 } |
| 96 |
| 97 // TODO(sergeygs): Should be is_platform_app? |
| 98 DCHECK(extension->is_app()); |
| 99 |
| 100 for (DictionaryValue::Iterator iter(*all_handlers); !iter.IsAtEnd(); |
| 101 iter.Advance()) { |
| 102 // A URL handler entry is a title and a list of URL patterns to handle. |
| 103 const DictionaryValue* handler = NULL; |
| 104 if (!iter.value().GetAsDictionary(&handler)) { |
| 105 *error = ASCIIToUTF16(merrors::kInvalidUrlHandlerPatternElement); |
| 106 return false; |
| 107 } |
| 108 |
| 109 if (!ParseUrlHandler( |
| 110 extension, iter.key(), *handler, &info->handlers, error)) { |
| 111 // Text in |error| set by LoadUrlHandler. |
| 112 return false; |
| 113 } |
| 114 } |
| 115 |
| 116 extension->SetManifestData(mkeys::kUrlHandlers, info.release()); |
| 117 |
| 118 return true; |
| 119 } |
| 120 |
| 121 const std::vector<std::string> UrlHandlersParser::Keys() const { |
| 122 return SingleKey(mkeys::kUrlHandlers); |
| 123 } |
| 124 |
| 125 } // namespace extensions |
OLD | NEW |