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

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: Added 1 more test + addressed comments 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 UrlHandlerInfo handler;
98 handler.id = handler_id;
99
100 if (!handler_info.GetString(mkeys::kUrlHandlerTitle, &handler.title)) {
101 *error = ASCIIToUTF16(merrors::kInvalidURLHandlerTitle);
102 return false;
103 }
104
105 const ListValue* manif_patterns = NULL;
106 if (!handler_info.GetList(mkeys::kMatches, &manif_patterns) ||
107 manif_patterns->GetSize() == 0) {
108 *error = ErrorUtils::FormatErrorMessageUTF16(
109 merrors::kInvalidURLHandlerPattern, handler_id);
110 return false;
111 }
112
113 for (ListValue::const_iterator it = manif_patterns->begin();
114 it != manif_patterns->end(); ++it) {
115 std::string str_pattern;
116 (*it)->GetAsString(&str_pattern);
117 URLPattern pattern(URLPattern::SCHEME_HTTP |
118 URLPattern::SCHEME_HTTPS);
119 if (pattern.Parse(str_pattern) != URLPattern::PARSE_SUCCESS) {
120 *error = ErrorUtils::FormatErrorMessageUTF16(
121 merrors::kInvalidURLHandlerPatternElement, handler_id);
122 return false;
123 }
124 handler.patterns.AddPattern(pattern);
not at google - send to devlin 2013/09/09 22:25:35 Please also add TODO(..) to restrict this feature
sergeygs 2013/09/09 23:41:39 Done.
125 }
126
127 url_handlers->push_back(handler);
128
129 return true;
130 }
131
132 bool UrlHandlersParser::Parse(Extension* extension, string16* error) {
133 scoped_ptr<UrlHandlers> info(new UrlHandlers);
134 const DictionaryValue* all_handlers = NULL;
135 if (!extension->manifest()->GetDictionary(
136 mkeys::kUrlHandlers, &all_handlers)) {
137 *error = ASCIIToUTF16(merrors::kInvalidURLHandlers);
138 return false;
139 }
140
141 DCHECK(extension->is_platform_app());
142
143 for (DictionaryValue::Iterator iter(*all_handlers); !iter.IsAtEnd();
144 iter.Advance()) {
145 // A URL handler entry is a title and a list of URL patterns to handle.
146 const DictionaryValue* handler = NULL;
147 if (!iter.value().GetAsDictionary(&handler)) {
148 *error = ASCIIToUTF16(merrors::kInvalidURLHandlerPatternElement);
149 return false;
150 }
151
152 if (!ParseUrlHandler(iter.key(), *handler, &info->handlers, error)) {
153 // Text in |error| is set by ParseUrlHandler.
154 return false;
155 }
156 }
157
158 extension->SetManifestData(mkeys::kUrlHandlers, info.release());
159
160 return true;
161 }
162
163 const std::vector<std::string> UrlHandlersParser::Keys() const {
164 return SingleKey(mkeys::kUrlHandlers);
165 }
166
167 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/common/extensions/api/url_handlers/url_handlers_parser.h ('k') | chrome/common/extensions/chrome_manifest_handlers.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698