| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/extensions/url_pattern.h" | 5 #include "chrome/common/extensions/url_pattern.h" |
| 6 | 6 |
| 7 #include "base/scoped_ptr.h" | |
| 8 #include "base/string_piece.h" | 7 #include "base/string_piece.h" |
| 9 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 10 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
| 11 | 10 |
| 12 // TODO(aa): Consider adding chrome-extension? What about more obscure ones | 11 // TODO(aa): Consider adding chrome-extension? What about more obscure ones |
| 13 // like data: and javascript: ? | 12 // like data: and javascript: ? |
| 14 static const char* kValidSchemes[] = { | 13 static const char* kValidSchemes[] = { |
| 15 chrome::kHttpScheme, | 14 chrome::kHttpScheme, |
| 16 chrome::kHttpsScheme, | 15 chrome::kHttpsScheme, |
| 17 chrome::kFileScheme, | 16 chrome::kFileScheme, |
| 18 chrome::kFtpScheme, | 17 chrome::kFtpScheme, |
| 19 chrome::kChromeUIScheme, | 18 chrome::kChromeUIScheme, |
| 20 }; | 19 }; |
| 21 | 20 |
| 22 static const char kPathSeparator[] = "/"; | 21 static const char kPathSeparator[] = "/"; |
| 23 | 22 |
| 24 // static | 23 // static |
| 25 URLPattern* URLPattern::CreateFromString(const std::string& pattern_string) { | |
| 26 scoped_ptr<URLPattern> pattern(new URLPattern); | |
| 27 if (pattern->Parse(pattern_string)) | |
| 28 return pattern.release(); | |
| 29 else | |
| 30 return NULL; | |
| 31 } | |
| 32 | |
| 33 // static | |
| 34 bool URLPattern::IsValidScheme(const std::string& scheme) { | 24 bool URLPattern::IsValidScheme(const std::string& scheme) { |
| 35 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) { | 25 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) { |
| 36 if (scheme == kValidSchemes[i]) | 26 if (scheme == kValidSchemes[i]) |
| 37 return true; | 27 return true; |
| 38 } | 28 } |
| 39 | 29 |
| 40 return false; | 30 return false; |
| 41 } | 31 } |
| 42 | 32 |
| 33 URLPattern::URLPattern() |
| 34 : match_subdomains_(false) {} |
| 35 |
| 36 URLPattern::URLPattern(const std::string& pattern) |
| 37 : match_subdomains_(false) { |
| 38 if (!Parse(pattern)) |
| 39 NOTREACHED() << "URLPattern is invalid: " << pattern; |
| 40 } |
| 41 |
| 43 bool URLPattern::Parse(const std::string& pattern) { | 42 bool URLPattern::Parse(const std::string& pattern) { |
| 44 size_t scheme_end_pos = pattern.find(chrome::kStandardSchemeSeparator); | 43 size_t scheme_end_pos = pattern.find(chrome::kStandardSchemeSeparator); |
| 45 if (scheme_end_pos == std::string::npos) | 44 if (scheme_end_pos == std::string::npos) |
| 46 return false; | 45 return false; |
| 47 | 46 |
| 48 scheme_ = pattern.substr(0, scheme_end_pos); | 47 scheme_ = pattern.substr(0, scheme_end_pos); |
| 49 if (!IsValidScheme(scheme_)) | 48 if (!IsValidScheme(scheme_)) |
| 50 return false; | 49 return false; |
| 51 | 50 |
| 52 size_t host_start_pos = scheme_end_pos + | 51 size_t host_start_pos = scheme_end_pos + |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 } | 166 } |
| 168 | 167 |
| 169 if (!host_.empty()) | 168 if (!host_.empty()) |
| 170 spec += host_; | 169 spec += host_; |
| 171 | 170 |
| 172 if (!path_.empty()) | 171 if (!path_.empty()) |
| 173 spec += path_; | 172 spec += path_; |
| 174 | 173 |
| 175 return spec; | 174 return spec; |
| 176 } | 175 } |
| OLD | NEW |