| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 #import "net/base/mac/url_conversions.h" | 5 #import "net/base/mac/url_conversions.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 | 8 |
| 9 #include "base/mac/scoped_nsobject.h" | 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "net/base/escape.h" | 10 #include "net/base/escape.h" |
| 11 #include "url/gurl.h" | 11 #include "url/gurl.h" |
| 12 #include "url/url_canon.h" | 12 #include "url/url_canon.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 NSURL* NSURLWithGURL(const GURL& url) { | 16 NSURL* NSURLWithGURL(const GURL& url) { |
| 17 if (!url.is_valid()) | 17 if (!url.is_valid()) |
| 18 return nil; | 18 return nil; |
| 19 | 19 |
| 20 // NSURL strictly enforces RFC 1738 which requires that certain characters | 20 // NSURL strictly enforces RFC 1738 which requires that certain characters |
| 21 // are always encoded. These characters are: "<", ">", """, "#", "%", "{", | 21 // are always encoded. These characters are: "<", ">", """, "#", "%", "{", |
| 22 // "}", "|", "\", "^", "~", "[", "]", and "`". | 22 // "}", "|", "\", "^", "~", "[", "]", and "`". |
| 23 // | 23 // |
| 24 // GURL leaves some of these characters unencoded in the path, query, and | 24 // GURL leaves some of these characters unencoded in the path, query, and |
| 25 // ref. This function manually encodes those components, and then passes the | 25 // ref. This function manually encodes those components, and then passes the |
| 26 // result to NSURL. | 26 // result to NSURL. |
| 27 GURL::Replacements replacements; | 27 GURL::Replacements replacements; |
| 28 std::string escaped_path = EscapeNSURLPrecursor(url.path()); | 28 std::string escaped_path = EscapeNSURLPrecursor(url.path().as_string()); |
| 29 std::string escaped_query = EscapeNSURLPrecursor(url.query()); | 29 std::string escaped_query = EscapeNSURLPrecursor(url.query()); |
| 30 std::string escaped_ref = EscapeNSURLPrecursor(url.ref()); | 30 std::string escaped_ref = EscapeNSURLPrecursor(url.ref()); |
| 31 if (!escaped_path.empty()) { | 31 if (!escaped_path.empty()) { |
| 32 replacements.SetPath(escaped_path.c_str(), | 32 replacements.SetPath(escaped_path.c_str(), |
| 33 url::Component(0, escaped_path.size())); | 33 url::Component(0, escaped_path.size())); |
| 34 } | 34 } |
| 35 if (!escaped_query.empty()) { | 35 if (!escaped_query.empty()) { |
| 36 replacements.SetQuery(escaped_query.c_str(), | 36 replacements.SetQuery(escaped_query.c_str(), |
| 37 url::Component(0, escaped_query.size())); | 37 url::Component(0, escaped_query.size())); |
| 38 } | 38 } |
| 39 if (!escaped_ref.empty()) { | 39 if (!escaped_ref.empty()) { |
| 40 replacements.SetRef(escaped_ref.c_str(), | 40 replacements.SetRef(escaped_ref.c_str(), |
| 41 url::Component(0, escaped_ref.size())); | 41 url::Component(0, escaped_ref.size())); |
| 42 } | 42 } |
| 43 GURL escaped_url = url.ReplaceComponents(replacements); | 43 GURL escaped_url = url.ReplaceComponents(replacements); |
| 44 | 44 |
| 45 base::scoped_nsobject<NSString> escaped_url_string( | 45 base::scoped_nsobject<NSString> escaped_url_string( |
| 46 [[NSString alloc] initWithUTF8String:escaped_url.spec().c_str()]); | 46 [[NSString alloc] initWithUTF8String:escaped_url.spec().c_str()]); |
| 47 return [NSURL URLWithString:escaped_url_string]; | 47 return [NSURL URLWithString:escaped_url_string]; |
| 48 } | 48 } |
| 49 | 49 |
| 50 GURL GURLWithNSURL(NSURL* url) { | 50 GURL GURLWithNSURL(NSURL* url) { |
| 51 if (url) | 51 if (url) |
| 52 return GURL([[url absoluteString] UTF8String]); | 52 return GURL([[url absoluteString] UTF8String]); |
| 53 return GURL(); | 53 return GURL(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 } // namespace net | 56 } // namespace net |
| OLD | NEW |