Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: chrome/common/extensions/api/url_handlers/url_handlers_parser.cc

Issue 23847004: "Redirecting URLs to Packaged Apps" implementation: revised (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Prevent interception for POSTs sooner; prevent when prerendering. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
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 "chrome/common/extensions/manifest_handlers/offline_enabled_info.h"
14 #include "extensions/common/error_utils.h"
15 #include "extensions/common/switches.h"
16 #include "net/base/network_change_notifier.h"
17 #include "url/gurl.h"
18
19 using base::ASCIIToUTF16;
20 using net::NetworkChangeNotifier;
21
22 namespace mkeys = extensions::manifest_keys;
23 namespace merrors = extension_manifest_errors;
24
25 // TODO(sergeygs): Use the same strategy that externally_connectable does for
26 // parsing the manifest: declare a schema for the manifest entry in
27 // manifest_types.json, then use it here.
28 //
29 // See:
30 // chrome/common/extensions/api/manifest_types.json
31 // chrome/common/extensions/manifest_handlers/externally_connectable.*
32 //
33 // Do the same in (at least) file_handlers_parser.cc as well.
34
35 namespace extensions {
36
37 UrlHandlerInfo::UrlHandlerInfo() {
38 }
39
40 UrlHandlerInfo::~UrlHandlerInfo() {
41 }
42
43 UrlHandlers::UrlHandlers() {
44 }
45
46 UrlHandlers::~UrlHandlers() {
47 }
48
49 // static
50 const std::vector<UrlHandlerInfo>* UrlHandlers::GetUrlHandlers(
51 const Extension* extension) {
52 UrlHandlers* info = static_cast<UrlHandlers*>(
53 extension->GetManifestData(mkeys::kUrlHandlers));
54 return info ? &info->handlers : NULL;
55 }
56
57 // static
58 bool UrlHandlers::CanExtensionHandleUrl(
59 const Extension* extension,
60 const GURL& url) {
61 return FindMatchingUrlHandler(extension, url) != NULL;
62 }
63
64 // static
65 const UrlHandlerInfo* UrlHandlers::FindMatchingUrlHandler(
66 const Extension* extension,
67 const GURL& url) {
68 const std::vector<UrlHandlerInfo>* handlers = GetUrlHandlers(extension);
69 if (!handlers)
70 return NULL;
71
72 if (NetworkChangeNotifier::IsOffline() &&
73 !OfflineEnabledInfo::IsOfflineEnabled(extension))
74 return NULL;
75
76 for (std::vector<extensions::UrlHandlerInfo>::const_iterator it =
77 handlers->begin(); it != handlers->end(); it++) {
78 if (it->patterns.MatchesURL(url))
79 return &(*it);
80 }
81
82 return NULL;
83 }
84
85 UrlHandlersParser::UrlHandlersParser() {
86 }
87
88 UrlHandlersParser::~UrlHandlersParser() {
89 }
90
91 bool ParseUrlHandler(const std::string& handler_id,
92 const DictionaryValue& handler_info,
93 std::vector<UrlHandlerInfo>* url_handlers,
94 string16* error) {
95 DCHECK(error);
96
97 // TODO(sergeygs): Remove this and update includes once url_handlers are
98 // moved out of experimental.
99 if (!CommandLine::ForCurrentProcess()->HasSwitch(
100 extensions::switches::kEnableExperimentalExtensionApis)) {
101 *error = ASCIIToUTF16(merrors::kExperimentalFlagRequired);
102 return false;
103 }
104
105 UrlHandlerInfo handler;
106 handler.id = handler_id;
107
108 if (!handler_info.GetString(mkeys::kUrlHandlerTitle, &handler.title)) {
109 *error = ASCIIToUTF16(merrors::kInvalidURLHandlerTitle);
110 return false;
111 }
112
113 const ListValue* manif_patterns = NULL;
114 if (!handler_info.GetList(mkeys::kMatches, &manif_patterns) ||
115 manif_patterns->GetSize() == 0) {
116 *error = ErrorUtils::FormatErrorMessageUTF16(
117 merrors::kInvalidURLHandlerPattern, handler_id);
118 return false;
119 }
120
121 for (ListValue::const_iterator it = manif_patterns->begin();
122 it != manif_patterns->end(); ++it) {
123 std::string str_pattern;
124 (*it)->GetAsString(&str_pattern);
125 URLPattern pattern(URLPattern::SCHEME_HTTP |
126 URLPattern::SCHEME_HTTPS);
127 if (pattern.Parse(str_pattern) != URLPattern::PARSE_SUCCESS) {
128 *error = ErrorUtils::FormatErrorMessageUTF16(
129 merrors::kInvalidURLHandlerPatternElement, handler_id);
130 return false;
131 }
132 handler.patterns.AddPattern(pattern);
133 }
134
135 url_handlers->push_back(handler);
136
137 return true;
138 }
139
140 bool UrlHandlersParser::Parse(Extension* extension, string16* error) {
141 scoped_ptr<UrlHandlers> info(new UrlHandlers);
142 const DictionaryValue* all_handlers = NULL;
143 if (!extension->manifest()->GetDictionary(
144 mkeys::kUrlHandlers, &all_handlers)) {
145 *error = ASCIIToUTF16(merrors::kInvalidURLHandlers);
146 return false;
147 }
148
149 DCHECK(extension->is_platform_app());
150
151 for (DictionaryValue::Iterator iter(*all_handlers); !iter.IsAtEnd();
152 iter.Advance()) {
153 // A URL handler entry is a title and a list of URL patterns to handle.
154 const DictionaryValue* handler = NULL;
155 if (!iter.value().GetAsDictionary(&handler)) {
156 *error = ASCIIToUTF16(merrors::kInvalidURLHandlerPatternElement);
157 return false;
158 }
159
160 if (!ParseUrlHandler(iter.key(), *handler, &info->handlers, error)) {
161 // Text in |error| is set by ParseUrlHandler.
162 return false;
163 }
164 }
165
166 extension->SetManifestData(mkeys::kUrlHandlers, info.release());
167
168 return true;
169 }
170
171 const std::vector<std::string> UrlHandlersParser::Keys() const {
172 return SingleKey(mkeys::kUrlHandlers);
173 }
174
175 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698