1 // Copyright 2007, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #import <WebKit/WebKit.h>
28 #import "gears/base/common/security_model.h"
29 #import "gears/base/safari/string_utils.h"
30 #import "gears/base/safari/browser_utils.h"
31 #import "gears/base/safari/scoped_cf.h"
33 @interface NSString(GearsUsedWebKitPrivate)
34 // WebKit/Misc/WebNSURLExtras.m
35 - (NSString *)_web_mapHostNameWithRange:(NSRange)range encode:(BOOL)encode
36 makeString:(BOOL)makeString;
39 @implementation GearsURLUtilities
40 //------------------------------------------------------------------------------
41 + (NSString *)resolveURLString:(NSString *)relativeURLStr
42 usingPluginArgs:(NSDictionary *)args {
43 NSURL *baseURL = [args objectForKey:WebPlugInBaseURLKey];
45 return [GearsURLUtilities resolveURLString:relativeURLStr
46 baseURLStr:[baseURL absoluteString]];
49 //------------------------------------------------------------------------------
50 + (NSString *)resolveURLString:(NSString *)relativeURLStr
51 baseURLStr:(NSString *)baseURLStr {
52 NSURL *baseURL = [NSURL URLWithString:baseURLStr];
53 NSURL *url = [NSURL URLWithString:relativeURLStr relativeToURL:baseURL];
54 NSString *fragment = [url fragment];
55 NSString *urlStr = [url absoluteString];
58 NSMutableString *urlStrCopy = [urlStr mutableCopy];
61 fragment = [NSString stringWithFormat:@"#%@", fragment];
62 [urlStrCopy replaceOccurrencesOfString:fragment withString:@"" options:0
63 range:NSMakeRange(0, [urlStrCopy length])];
64 urlStr = [NSString stringWithString:urlStrCopy];
72 //------------------------------------------------------------------------------
73 bool CFURLRefToString16(CFURLRef url, std::string16 *out16) {
77 scoped_CFURL absolute(CFURLCopyAbsoluteURL(url));
78 CFStringRef absoluteStr = CFURLGetString(absolute.get());
80 return CFStringRefToString16(absoluteStr, out16);
83 //------------------------------------------------------------------------------
84 CFURLRef CFURLCreateWithString16(const char16 *url_str) {
85 scoped_CFString url(CFStringCreateWithString16(url_str));
87 return CFURLCreateWithString(kCFAllocatorDefault, url.get(), NULL);
90 //------------------------------------------------------------------------------
91 bool SafariURLUtilities::GetPageOrigin(const char *url_str,
92 SecurityOrigin *security_origin) {
93 std::string16 location;
94 UTF8ToString16(url_str, &location);
96 return security_origin->InitFromUrl(location.c_str());
99 //------------------------------------------------------------------------------
100 bool SafariURLUtilities::GetPageOriginFromArguments(
101 CFDictionaryRef dict,
102 SecurityOrigin *security_origin) {
103 NSURL *baseURL = [(NSDictionary *)dict objectForKey:WebPlugInBaseURLKey];
106 return GetPageOrigin([[baseURL absoluteString] UTF8String],