Home | History | Annotate | Download | only in shared_impl
      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.
      4 
      5 #include "ppapi/shared_impl/ppb_url_util_shared.h"
      6 
      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"
     11 #include "url/gurl.h"
     12 
     13 namespace ppapi {
     14 
     15 namespace {
     16 
     17 void ConvertComponent(const url::Component& input,
     18                       PP_URLComponent_Dev* output) {
     19   output->begin = input.begin;
     20   output->len = input.len;
     21 }
     22 
     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.
     26 //
     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) {
     30   if (!output)
     31     return;
     32 
     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);
     41 }
     42 
     43 }  // namespace
     44 
     45 // static
     46 PP_Var PPB_URLUtil_Shared::Canonicalize(PP_Var url,
     47                                         PP_URLComponents_Dev* components) {
     48   ProxyAutoLock lock;
     49   StringVar* url_string = StringVar::FromPPVar(url);
     50   if (!url_string)
     51     return PP_MakeNull();
     52   return GenerateURLReturn(GURL(url_string->value()), components);
     53 }
     54 
     55 // static
     56 PP_Var PPB_URLUtil_Shared::ResolveRelativeToURL(
     57     PP_Var base_url,
     58     PP_Var relative,
     59     PP_URLComponents_Dev* components) {
     60   ProxyAutoLock lock;
     61   StringVar* base_url_string = StringVar::FromPPVar(base_url);
     62   StringVar* relative_string = StringVar::FromPPVar(relative);
     63   if (!base_url_string || !relative_string)
     64     return PP_MakeNull();
     65 
     66   GURL base_gurl(base_url_string->value());
     67   if (!base_gurl.is_valid())
     68     return PP_MakeNull();
     69   return GenerateURLReturn(base_gurl.Resolve(relative_string->value()),
     70                            components);
     71 }
     72 
     73 // static
     74 PP_Bool PPB_URLUtil_Shared::IsSameSecurityOrigin(PP_Var url_a, PP_Var url_b) {
     75   ProxyAutoLock lock;
     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)
     79     return PP_FALSE;
     80 
     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())
     84     return PP_FALSE;
     85 
     86   return gurl_a.GetOrigin() == gurl_b.GetOrigin() ? PP_TRUE : PP_FALSE;
     87 }
     88 
     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) {
     93   if (!url.is_valid())
     94     return PP_MakeNull();
     95   ConvertComponents(url.parsed_for_possibly_invalid_spec(), components);
     96   return StringVar::StringToPPVar(url.possibly_invalid_spec());
     97 }
     98 
     99 PP_Var PPB_URLUtil_Shared::ConvertComponentsAndReturnURL(
    100     const PP_Var& url,
    101     PP_URLComponents_Dev* components) {
    102   if (!components)
    103     return url;  // Common case - plugin doesn't care about parsing.
    104 
    105   StringVar* url_string = StringVar::FromPPVar(url);
    106   if (!url_string)
    107     return url;
    108 
    109   PP_Var result = Canonicalize(url, components);
    110   PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(url);
    111   return result;
    112 }
    113 
    114 }  // namespace ppapi
    115