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 #include "googleurl/src/gurl.h" |
| 15 |
| 16 namespace mkeys = extension_manifest_keys; |
| 17 namespace merrors = extension_manifest_errors; |
| 18 |
| 19 // TODO(sergeygs): Use the same strategy that externally_connectable does for |
| 20 // parsing the manifest: declare a schema for the manifest entry in |
| 21 // manifest_types.json, then use it here. |
| 22 // |
| 23 // See: |
| 24 // chrome/common/extensions/api/manifest_types.json |
| 25 // chrome/common/extensions/manifest_handlers/externally_connectable.* |
| 26 // |
| 27 // Do the same in (at least) file_handlers_parser.cc as well. |
| 28 |
| 29 namespace extensions { |
| 30 |
| 31 UrlHandlerInfo::UrlHandlerInfo() { |
| 32 } |
| 33 |
| 34 UrlHandlerInfo::~UrlHandlerInfo() { |
| 35 } |
| 36 |
| 37 UrlHandlers::UrlHandlers() { |
| 38 } |
| 39 |
| 40 UrlHandlers::~UrlHandlers() { |
| 41 } |
| 42 |
| 43 // static |
| 44 const std::vector<UrlHandlerInfo>* UrlHandlers::GetUrlHandlers( |
| 45 const Extension* extension) { |
| 46 UrlHandlers* info = static_cast<UrlHandlers*>( |
| 47 extension->GetManifestData(mkeys::kUrlHandlers)); |
| 48 return info ? &info->handlers : NULL; |
| 49 } |
| 50 |
| 51 // static |
| 52 bool UrlHandlers::CanExtensionHandleUrl( |
| 53 const Extension* extension, |
| 54 const GURL& url) { |
| 55 return FindMatchingUrlHandler(extension, url) != NULL; |
| 56 } |
| 57 |
| 58 // static |
| 59 const UrlHandlerInfo* UrlHandlers::FindMatchingUrlHandler( |
| 60 const Extension* extension, |
| 61 const GURL& url) { |
| 62 const std::vector<UrlHandlerInfo>* handlers = GetUrlHandlers(extension); |
| 63 if (!handlers) |
| 64 return; |
| 65 |
| 66 for (std::vector<extensions::UrlHandlerInfo>::const_iterator it = |
| 67 handlers->begin(); it != handlers->end(); it++) { |
| 68 if (it->patterns.MatchesURL(url)) |
| 69 return &(*it); |
| 70 } |
| 71 |
| 72 return NULL; |
| 73 } |
| 74 |
| 75 UrlHandlersParser::UrlHandlersParser() { |
| 76 } |
| 77 |
| 78 UrlHandlersParser::~UrlHandlersParser() { |
| 79 } |
| 80 |
| 81 bool ParseUrlHandler(const std::string& handler_id, |
| 82 const DictionaryValue& handler_info, |
| 83 std::vector<UrlHandlerInfo>* url_handlers, |
| 84 string16* error) { |
| 85 DCHECK(error); |
| 86 UrlHandlerInfo handler; |
| 87 |
| 88 handler.id = handler_id; |
| 89 |
| 90 if (!handler_info.GetString(mkeys::kUrlHandlerTitle, &handler.title)) { |
| 91 *error = ASCIIToUTF16(merrors::kInvalidUrlHandlerTitle); |
| 92 return false; |
| 93 } |
| 94 |
| 95 const ListValue* manif_patterns = NULL; |
| 96 if (!handler_info.GetList(mkeys::kMatches, &manif_patterns) || |
| 97 manif_patterns->GetSize() == 0) { |
| 98 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 99 merrors::kInvalidUrlHandlerPattern, handler_id); |
| 100 return false; |
| 101 } |
| 102 |
| 103 for (ListValue::const_iterator it = manif_patterns->begin(); |
| 104 it != manif_patterns.end(); ++it) { |
| 105 std::string str_pattern; |
| 106 (*it)->GetAsString(&str_pattern); |
| 107 URLPattern pattern(URLPattern::SCHEME_HTTP | |
| 108 URLPattern::SCHEME_HTTPS); |
| 109 if (pattern.Parse(str_pattern) != URLPattern::PARSE_SUCCESS) { |
| 110 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 111 merrors::kInvalidUrlHandlerPatternElement, handler_id); |
| 112 return false; |
| 113 } |
| 114 handler.patterns.AddPattern(pattern); |
| 115 } |
| 116 |
| 117 url_handlers->push_back(handler); |
| 118 |
| 119 return true; |
| 120 } |
| 121 |
| 122 bool UrlHandlersParser::Parse(Extension* extension, string16* error) { |
| 123 scoped_ptr<UrlHandlers> info(new UrlHandlers); |
| 124 const DictionaryValue* all_handlers = NULL; |
| 125 if (!extension->manifest()->GetDictionary( |
| 126 mkeys::kUrlHandlers, &all_handlers)) { |
| 127 *error = ASCIIToUTF16(merrors::kInvalidUrlHandlers); |
| 128 return false; |
| 129 } |
| 130 |
| 131 DCHECK(extension->is_platform_app()); |
| 132 |
| 133 for (DictionaryValue::Iterator iter(*all_handlers); !iter.IsAtEnd(); |
| 134 iter.Advance()) { |
| 135 // A URL handler entry is a title and a list of URL patterns to handle. |
| 136 const DictionaryValue* handler = NULL; |
| 137 if (!iter.value().GetAsDictionary(&handler)) { |
| 138 *error = ASCIIToUTF16(merrors::kInvalidUrlHandlerPatternElement); |
| 139 return false; |
| 140 } |
| 141 |
| 142 if (!ParseUrlHandler(iter.key(), *handler, &info->handlers, error)) { |
| 143 // Text in |error| is set by ParseUrlHandler. |
| 144 return false; |
| 145 } |
| 146 } |
| 147 |
| 148 extension->SetManifestData(mkeys::kUrlHandlers, info.release()); |
| 149 |
| 150 return true; |
| 151 } |
| 152 |
| 153 const std::vector<std::string> UrlHandlersParser::Keys() const { |
| 154 return SingleKey(mkeys::kUrlHandlers); |
| 155 } |
| 156 |
| 157 } // namespace extensions |
OLD | NEW |