1 // Copyright (c) 2011 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.
5 #include "ppapi/shared_impl/ppb_url_util_shared.h"
7 #include "ppapi/shared_impl/ppapi_globals.h"
8 #include "ppapi/shared_impl/proxy_lock.h"
9 #include "ppapi/shared_impl/var.h"
10 #include "ppapi/shared_impl/var_tracker.h"
17 void ConvertComponent(const url_parse::Component
& input
,
18 PP_URLComponent_Dev
* output
) {
19 output
->begin
= input
.begin
;
20 output
->len
= input
.len
;
23 // Converts components from a GoogleUrl parsed to a PPAPI parsed structure.
24 // Output can be NULL to specify "do nothing." This rule is followed by all
25 // the url util functions, so we implement it once here.
27 // Output can be NULL to specify "do nothing." This rule is followed by all the
28 // url util functions, so we implement it once here.
29 void ConvertComponents(const url_parse::Parsed
& input
,
30 PP_URLComponents_Dev
* output
) {
34 ConvertComponent(input
.scheme
, &output
->scheme
);
35 ConvertComponent(input
.username
, &output
->username
);
36 ConvertComponent(input
.password
, &output
->password
);
37 ConvertComponent(input
.host
, &output
->host
);
38 ConvertComponent(input
.port
, &output
->port
);
39 ConvertComponent(input
.path
, &output
->path
);
40 ConvertComponent(input
.query
, &output
->query
);
41 ConvertComponent(input
.ref
, &output
->ref
);
47 PP_Var
PPB_URLUtil_Shared::Canonicalize(PP_Var url
,
48 PP_URLComponents_Dev
* components
) {
50 StringVar
* url_string
= StringVar::FromPPVar(url
);
53 return GenerateURLReturn(GURL(url_string
->value()), components
);
57 PP_Var
PPB_URLUtil_Shared::ResolveRelativeToURL(
60 PP_URLComponents_Dev
* components
) {
62 StringVar
* base_url_string
= StringVar::FromPPVar(base_url
);
63 StringVar
* relative_string
= StringVar::FromPPVar(relative
);
64 if (!base_url_string
|| !relative_string
)
67 GURL
base_gurl(base_url_string
->value());
68 if (!base_gurl
.is_valid())
70 return GenerateURLReturn(base_gurl
.Resolve(relative_string
->value()),
75 PP_Bool
PPB_URLUtil_Shared::IsSameSecurityOrigin(PP_Var url_a
, PP_Var url_b
) {
77 StringVar
* url_a_string
= StringVar::FromPPVar(url_a
);
78 StringVar
* url_b_string
= StringVar::FromPPVar(url_b
);
79 if (!url_a_string
|| !url_b_string
)
82 GURL
gurl_a(url_a_string
->value());
83 GURL
gurl_b(url_b_string
->value());
84 if (!gurl_a
.is_valid() || !gurl_b
.is_valid())
87 return gurl_a
.GetOrigin() == gurl_b
.GetOrigin() ? PP_TRUE
: PP_FALSE
;
90 // Used for returning the given GURL from a PPAPI function, with an optional
91 // out param indicating the components.
92 PP_Var
PPB_URLUtil_Shared::GenerateURLReturn(const GURL
& url
,
93 PP_URLComponents_Dev
* components
) {
96 ConvertComponents(url
.parsed_for_possibly_invalid_spec(), components
);
97 return StringVar::StringToPPVar(url
.possibly_invalid_spec());
100 PP_Var
PPB_URLUtil_Shared::ConvertComponentsAndReturnURL(
102 PP_URLComponents_Dev
* components
) {
104 return url
; // Common case - plugin doesn't care about parsing.
106 StringVar
* url_string
= StringVar::FromPPVar(url
);
110 PP_Var result
= Canonicalize(url
, components
);
111 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(url
);