Upstream | d8543bb | 1970-01-12 13:46:40 +0000 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 9364f22 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | * Copyright (C) 2004 Apple Computer, Inc. All rights reserved. |
Upstream | d8543bb | 1970-01-12 13:46:40 +0000 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "KURL.h" |
| 28 | |
| 29 | #include <wtf/RetainPtr.h> |
The Android Open Source Project | 9364f22 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | #include <wtf/Vector.h> |
Upstream | d8543bb | 1970-01-12 13:46:40 +0000 | [diff] [blame] | 31 | #include <CoreFoundation/CFURL.h> |
| 32 | |
| 33 | using namespace std; |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | KURL::KURL(CFURLRef url) |
| 38 | { |
The Android Open Source Project | 9364f22 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 39 | if (url) { |
| 40 | CFIndex bytesLength = CFURLGetBytes(url, 0, 0); |
| 41 | Vector<char, 2048> buffer(bytesLength + 6); // 6 for "file:", 1 for NUL terminator |
| 42 | char* bytes = &buffer[5]; |
| 43 | CFURLGetBytes(url, (UInt8*)bytes, bytesLength); |
| 44 | bytes[bytesLength] = '\0'; |
| 45 | if (bytes[0] == '/') { |
| 46 | buffer[0] = 'f'; |
| 47 | buffer[1] = 'i'; |
| 48 | buffer[2] = 'l'; |
| 49 | buffer[3] = 'e'; |
| 50 | buffer[4] = ':'; |
| 51 | parse(buffer.data(), 0); |
| 52 | } else |
| 53 | parse(bytes, 0); |
| 54 | } else |
| 55 | parse("", 0); |
Upstream | d8543bb | 1970-01-12 13:46:40 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | CFURLRef KURL::createCFURL() const |
| 59 | { |
The Android Open Source Project | 9364f22 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | const UInt8 *bytes = (const UInt8 *)urlString.latin1(); |
Upstream | d8543bb | 1970-01-12 13:46:40 +0000 | [diff] [blame] | 61 | // NOTE: We use UTF-8 here since this encoding is used when computing strings when returning URL components |
| 62 | // (e.g calls to NSURL -path). However, this function is not tolerant of illegal UTF-8 sequences, which |
| 63 | // could either be a malformed string or bytes in a different encoding, like Shift-JIS, so we fall back |
| 64 | // onto using ISO Latin-1 in those cases. |
The Android Open Source Project | 9364f22 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 65 | CFURLRef result = CFURLCreateAbsoluteURLWithBytes(0, bytes, urlString.length(), kCFStringEncodingUTF8, 0, true); |
Upstream | d8543bb | 1970-01-12 13:46:40 +0000 | [diff] [blame] | 66 | if (!result) |
The Android Open Source Project | 9364f22 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 67 | result = CFURLCreateAbsoluteURLWithBytes(0, bytes, urlString.length(), kCFStringEncodingISOLatin1, 0, true); |
Upstream | d8543bb | 1970-01-12 13:46:40 +0000 | [diff] [blame] | 68 | return result; |
| 69 | } |
| 70 | |
| 71 | String KURL::fileSystemPath() const |
| 72 | { |
| 73 | RetainPtr<CFURLRef> cfURL(AdoptCF, createCFURL()); |
| 74 | if (!cfURL) |
| 75 | return String(); |
| 76 | |
| 77 | #if PLATFORM(WIN) |
| 78 | CFURLPathStyle pathStyle = kCFURLWindowsPathStyle; |
| 79 | #else |
| 80 | CFURLPathStyle pathStyle = kCFURLPOSIXPathStyle; |
| 81 | #endif |
The Android Open Source Project | 9364f22 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | |
| 83 | RetainPtr<CFStringRef> path(AdoptCF, CFURLCopyFileSystemPath(cfURL.get(), pathStyle)); |
| 84 | return path.get(); |
Upstream | d8543bb | 1970-01-12 13:46:40 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | } |