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::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::Parsed
& input
, PP_URLComponents_Dev
* output
) {
33 ConvertComponent(input
.scheme
, &output
->scheme
);
34 ConvertComponent(input
.username
, &output
->username
);
35 ConvertComponent(input
.password
, &output
->password
);
36 ConvertComponent(input
.host
, &output
->host
);
37 ConvertComponent(input
.port
, &output
->port
);
38 ConvertComponent(input
.path
, &output
->path
);
39 ConvertComponent(input
.query
, &output
->query
);
40 ConvertComponent(input
.ref
, &output
->ref
);
46 PP_Var
PPB_URLUtil_Shared::Canonicalize(PP_Var url
,
47 PP_URLComponents_Dev
* components
) {
49 StringVar
* url_string
= StringVar::FromPPVar(url
);
52 return GenerateURLReturn(GURL(url_string
->value()), components
);
56 PP_Var
PPB_URLUtil_Shared::ResolveRelativeToURL(
59 PP_URLComponents_Dev
* components
) {
61 StringVar
* base_url_string
= StringVar::FromPPVar(base_url
);
62 StringVar
* relative_string
= StringVar::FromPPVar(relative
);
63 if (!base_url_string
|| !relative_string
)
66 GURL
base_gurl(base_url_string
->value());
67 if (!base_gurl
.is_valid())
69 return GenerateURLReturn(base_gurl
.Resolve(relative_string
->value()),
74 PP_Bool
PPB_URLUtil_Shared::IsSameSecurityOrigin(PP_Var url_a
, PP_Var url_b
) {
76 StringVar
* url_a_string
= StringVar::FromPPVar(url_a
);
77 StringVar
* url_b_string
= StringVar::FromPPVar(url_b
);
78 if (!url_a_string
|| !url_b_string
)
81 GURL
gurl_a(url_a_string
->value());
82 GURL
gurl_b(url_b_string
->value());
83 if (!gurl_a
.is_valid() || !gurl_b
.is_valid())
86 return gurl_a
.GetOrigin() == gurl_b
.GetOrigin() ? PP_TRUE
: PP_FALSE
;
89 // Used for returning the given GURL from a PPAPI function, with an optional
90 // out param indicating the components.
91 PP_Var
PPB_URLUtil_Shared::GenerateURLReturn(const GURL
& url
,
92 PP_URLComponents_Dev
* components
) {
95 ConvertComponents(url
.parsed_for_possibly_invalid_spec(), components
);
96 return StringVar::StringToPPVar(url
.possibly_invalid_spec());
99 PP_Var
PPB_URLUtil_Shared::ConvertComponentsAndReturnURL(
101 PP_URLComponents_Dev
* components
) {
103 return url
; // Common case - plugin doesn't care about parsing.
105 StringVar
* url_string
= StringVar::FromPPVar(url
);
109 PP_Var result
= Canonicalize(url
, components
);
110 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(url
);