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