| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 "webkit/glue/plugins/pepper_url_util.h" | |
| 6 | |
| 7 #include "googleurl/src/gurl.h" | |
| 8 #include "ppapi/c/dev/ppb_url_util_dev.h" | |
| 9 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" | |
| 10 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h" | |
| 11 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | |
| 12 #include "third_party/WebKit/WebKit/chromium/public/WebNode.h" | |
| 13 #include "third_party/WebKit/WebKit/chromium/public/WebPluginContainer.h" | |
| 14 #include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h" | |
| 15 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" | |
| 16 #include "webkit/glue/plugins/pepper_common.h" | |
| 17 #include "webkit/glue/plugins/pepper_plugin_instance.h" | |
| 18 #include "webkit/glue/plugins/pepper_string.h" | |
| 19 #include "webkit/glue/plugins/pepper_var.h" | |
| 20 | |
| 21 namespace pepper { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 void ConvertComponent(const url_parse::Component& input, | |
| 26 PP_UrlComponent_Dev* output) { | |
| 27 output->begin = input.begin; | |
| 28 output->len = input.len; | |
| 29 } | |
| 30 | |
| 31 // Output can be NULL to specify "do nothing." This rule is followed by all the | |
| 32 // url util functions, so we implement it once here. | |
| 33 void ConvertComponents(const url_parse::Parsed& input, | |
| 34 PP_UrlComponents_Dev* output) { | |
| 35 if (!output) | |
| 36 return; | |
| 37 | |
| 38 ConvertComponent(input.scheme, &output->scheme); | |
| 39 ConvertComponent(input.username, &output->username); | |
| 40 ConvertComponent(input.password, &output->password); | |
| 41 ConvertComponent(input.host, &output->host); | |
| 42 ConvertComponent(input.port, &output->port); | |
| 43 ConvertComponent(input.path, &output->path); | |
| 44 ConvertComponent(input.query, &output->query); | |
| 45 ConvertComponent(input.ref, &output->ref); | |
| 46 } | |
| 47 | |
| 48 // Used for returning the given GURL from a PPAPI function, with an optional | |
| 49 // out param indicating the components. | |
| 50 PP_Var GenerateUrlReturn(PluginModule* module, const GURL& url, | |
| 51 PP_UrlComponents_Dev* components) { | |
| 52 if (!url.is_valid()) | |
| 53 return PP_MakeNull(); | |
| 54 ConvertComponents(url.parsed_for_possibly_invalid_spec(), components); | |
| 55 return StringVar::StringToPPVar(module, url.possibly_invalid_spec()); | |
| 56 } | |
| 57 | |
| 58 // Sets |*security_origin| to be the WebKit security origin associated with the | |
| 59 // document containing the given plugin instance. On success, returns true. If | |
| 60 // the instance is invalid, returns false and |*security_origin| will be | |
| 61 // unchanged. | |
| 62 bool SecurityOriginForInstance(PP_Instance instance_id, | |
| 63 WebKit::WebSecurityOrigin* security_origin) { | |
| 64 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | |
| 65 if (!instance) | |
| 66 return false; | |
| 67 | |
| 68 WebKit::WebElement plugin_element = instance->container()->element(); | |
| 69 WebKit::WebFrame* plugin_frame = plugin_element.document().frame(); | |
| 70 if (!plugin_frame) | |
| 71 return false; | |
| 72 | |
| 73 *security_origin = plugin_frame->securityOrigin(); | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 PP_Var Canonicalize(PP_Var url, PP_UrlComponents_Dev* components) { | |
| 78 scoped_refptr<StringVar> url_string(StringVar::FromPPVar(url)); | |
| 79 if (!url_string) | |
| 80 return PP_MakeNull(); | |
| 81 return GenerateUrlReturn(url_string->module(), | |
| 82 GURL(url_string->value()), components); | |
| 83 } | |
| 84 | |
| 85 PP_Var ResolveRelativeToUrl(PP_Var base_url, | |
| 86 PP_Var relative, | |
| 87 PP_UrlComponents_Dev* components) { | |
| 88 scoped_refptr<StringVar> base_url_string(StringVar::FromPPVar(base_url)); | |
| 89 scoped_refptr<StringVar> relative_string(StringVar::FromPPVar(relative)); | |
| 90 if (!base_url_string || !relative_string) | |
| 91 return PP_MakeNull(); | |
| 92 | |
| 93 GURL base_gurl(base_url_string->value()); | |
| 94 if (!base_gurl.is_valid()) | |
| 95 return PP_MakeNull(); | |
| 96 return GenerateUrlReturn(base_url_string->module(), | |
| 97 base_gurl.Resolve(relative_string->value()), | |
| 98 components); | |
| 99 } | |
| 100 | |
| 101 PP_Var ResolveRelativeToDocument(PP_Instance instance_id, | |
| 102 PP_Var relative, | |
| 103 PP_UrlComponents_Dev* components) { | |
| 104 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | |
| 105 if (!instance) | |
| 106 return PP_MakeNull(); | |
| 107 | |
| 108 scoped_refptr<StringVar> relative_string(StringVar::FromPPVar(relative)); | |
| 109 if (!relative_string) | |
| 110 return PP_MakeNull(); | |
| 111 | |
| 112 WebKit::WebElement plugin_element = instance->container()->element(); | |
| 113 GURL document_url = plugin_element.document().baseURL(); | |
| 114 return GenerateUrlReturn(instance->module(), | |
| 115 document_url.Resolve(relative_string->value()), | |
| 116 components); | |
| 117 } | |
| 118 | |
| 119 PP_Bool IsSameSecurityOrigin(PP_Var url_a, PP_Var url_b) { | |
| 120 scoped_refptr<StringVar> url_a_string(StringVar::FromPPVar(url_a)); | |
| 121 scoped_refptr<StringVar> url_b_string(StringVar::FromPPVar(url_b)); | |
| 122 if (!url_a_string || !url_b_string) | |
| 123 return PP_FALSE; | |
| 124 | |
| 125 GURL gurl_a(url_a_string->value()); | |
| 126 GURL gurl_b(url_b_string->value()); | |
| 127 if (!gurl_a.is_valid() || !gurl_b.is_valid()) | |
| 128 return PP_FALSE; | |
| 129 | |
| 130 return BoolToPPBool(gurl_a.GetOrigin() == gurl_b.GetOrigin()); | |
| 131 } | |
| 132 | |
| 133 PP_Bool DocumentCanRequest(PP_Instance instance, PP_Var url) { | |
| 134 scoped_refptr<StringVar> url_string(StringVar::FromPPVar(url)); | |
| 135 if (!url_string) | |
| 136 return PP_FALSE; | |
| 137 | |
| 138 WebKit::WebSecurityOrigin security_origin; | |
| 139 if (!SecurityOriginForInstance(instance, &security_origin)) | |
| 140 return PP_FALSE; | |
| 141 | |
| 142 GURL gurl(url_string->value()); | |
| 143 if (!gurl.is_valid()) | |
| 144 return PP_FALSE; | |
| 145 | |
| 146 return BoolToPPBool(security_origin.canRequest(gurl)); | |
| 147 } | |
| 148 | |
| 149 PP_Bool DocumentCanAccessDocument(PP_Instance active, PP_Instance target) { | |
| 150 WebKit::WebSecurityOrigin active_origin; | |
| 151 if (!SecurityOriginForInstance(active, &active_origin)) | |
| 152 return PP_FALSE; | |
| 153 | |
| 154 WebKit::WebSecurityOrigin target_origin; | |
| 155 if (!SecurityOriginForInstance(active, &target_origin)) | |
| 156 return PP_FALSE; | |
| 157 | |
| 158 return BoolToPPBool(active_origin.canAccess(target_origin)); | |
| 159 } | |
| 160 | |
| 161 } // namespace | |
| 162 | |
| 163 const PPB_UrlUtil_Dev ppb_url_util = { | |
| 164 &Canonicalize, | |
| 165 &ResolveRelativeToUrl, | |
| 166 &ResolveRelativeToDocument, | |
| 167 &IsSameSecurityOrigin, | |
| 168 &DocumentCanRequest, | |
| 169 &DocumentCanAccessDocument | |
| 170 }; | |
| 171 | |
| 172 // static | |
| 173 const PPB_UrlUtil_Dev* UrlUtil::GetInterface() { | |
| 174 return &ppb_url_util; | |
| 175 } | |
| 176 | |
| 177 } // namespace pepper | |
| OLD | NEW |