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) { | |
not at google - send to devlin
2013/08/29 17:35:56
nit: early return in the exceptional case:
if (!h
sergeygs
2013/08/30 00:39:44
Done.
| |
64 for (std::vector<extensions::UrlHandlerInfo>::const_iterator it = | |
65 handlers->begin(); it != handlers->end(); it++) { | |
not at google - send to devlin
2013/08/29 17:35:56
indent--
sergeygs
2013/08/30 00:39:44
Done.
| |
66 if (it->patterns.MatchesURL(url)) | |
67 return &(*it); | |
68 } | |
69 } | |
70 return NULL; | |
71 } | |
72 | |
73 UrlHandlersParser::UrlHandlersParser() { | |
74 } | |
75 | |
76 UrlHandlersParser::~UrlHandlersParser() { | |
77 } | |
78 | |
79 bool ParseUrlHandler(const std::string& handler_id, | |
80 const DictionaryValue& handler_info, | |
81 std::vector<UrlHandlerInfo>* url_handlers, | |
82 string16* error) { | |
83 DCHECK(error); | |
84 UrlHandlerInfo handler; | |
85 | |
86 handler.id = handler_id; | |
87 | |
88 if (!handler_info.GetString(mkeys::kUrlHandlerTitle, &handler.title)) { | |
89 *error = ASCIIToUTF16(merrors::kInvalidUrlHandlerTitle); | |
90 return false; | |
91 } | |
92 | |
93 const ListValue* manif_patterns = NULL; | |
94 if (!handler_info.GetList(mkeys::kMatches, &manif_patterns) || | |
95 manif_patterns->GetSize() == 0) { | |
96 *error = ErrorUtils::FormatErrorMessageUTF16( | |
97 merrors::kInvalidUrlHandlerPattern, handler_id); | |
98 return false; | |
99 } | |
100 | |
101 for (const auto& manif_pattern: *manif_patterns) { | |
102 std::string str_pattern; | |
103 manif_pattern->GetAsString(&str_pattern); | |
104 URLPattern pattern(URLPattern::SCHEME_HTTP | | |
105 URLPattern::SCHEME_HTTPS); | |
106 if (pattern.Parse(str_pattern) != URLPattern::PARSE_SUCCESS) { | |
107 *error = ErrorUtils::FormatErrorMessageUTF16( | |
108 merrors::kInvalidUrlHandlerPatternElement, handler_id); | |
109 return false; | |
110 } | |
111 handler.patterns.AddPattern(pattern); | |
112 } | |
113 | |
114 url_handlers->push_back(handler); | |
115 | |
116 return true; | |
117 } | |
118 | |
119 bool UrlHandlersParser::Parse(Extension* extension, string16* error) { | |
120 scoped_ptr<UrlHandlers> info(new UrlHandlers); | |
121 const DictionaryValue* all_handlers = NULL; | |
122 if (!extension->manifest()->GetDictionary( | |
123 mkeys::kUrlHandlers, &all_handlers)) { | |
124 *error = ASCIIToUTF16(merrors::kInvalidUrlHandlers); | |
125 return false; | |
126 } | |
127 | |
128 DCHECK(extension->is_platform_app()); | |
129 | |
130 for (DictionaryValue::Iterator iter(*all_handlers); !iter.IsAtEnd(); | |
131 iter.Advance()) { | |
132 // A URL handler entry is a title and a list of URL patterns to handle. | |
133 const DictionaryValue* handler = NULL; | |
134 if (!iter.value().GetAsDictionary(&handler)) { | |
135 *error = ASCIIToUTF16(merrors::kInvalidUrlHandlerPatternElement); | |
136 return false; | |
137 } | |
138 | |
139 if (!ParseUrlHandler(iter.key(), *handler, &info->handlers, error)) { | |
140 // Text in |error| is set by ParseUrlHandler. | |
141 return false; | |
142 } | |
143 } | |
144 | |
145 extension->SetManifestData(mkeys::kUrlHandlers, info.release()); | |
146 | |
147 return true; | |
148 } | |
149 | |
150 const std::vector<std::string> UrlHandlersParser::Keys() const { | |
151 return SingleKey(mkeys::kUrlHandlers); | |
152 } | |
153 | |
154 } // namespace extensions | |
OLD | NEW |