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_parse::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_parse::Parsed& input,
     30                        PP_URLComponents_Dev* output) {
     31   if (!output)
     32     return;
     33 
     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);
     42 }
     43 
     44 }  // namespace
     45 
     46 // static
     47 PP_Var PPB_URLUtil_Shared::Canonicalize(PP_Var url,
     48                                         PP_URLComponents_Dev* components) {
     49   ProxyAutoLock lock;
     50   StringVar* url_string = StringVar::FromPPVar(url);
     51   if (!url_string)
     52     return PP_MakeNull();
     53   return GenerateURLReturn(GURL(url_string->value()), components);
     54 }
     55 
     56 // static
     57 PP_Var PPB_URLUtil_Shared::ResolveRelativeToURL(
     58     PP_Var base_url,
     59     PP_Var relative,
     60     PP_URLComponents_Dev* components) {
     61   ProxyAutoLock lock;
     62   StringVar* base_url_string = StringVar::FromPPVar(base_url);
     63   StringVar* relative_string = StringVar::FromPPVar(relative);
     64   if (!base_url_string || !relative_string)
     65     return PP_MakeNull();
     66 
     67   GURL base_gurl(base_url_string->value());
     68   if (!base_gurl.is_valid())
     69     return PP_MakeNull();
     70   return GenerateURLReturn(base_gurl.Resolve(relative_string->value()),
     71                            components);
     72 }
     73 
     74 // static
     75 PP_Bool PPB_URLUtil_Shared::IsSameSecurityOrigin(PP_Var url_a, PP_Var url_b) {
     76   ProxyAutoLock lock;
     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)
     80     return PP_FALSE;
     81 
     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())
     85     return PP_FALSE;
     86 
     87   return gurl_a.GetOrigin() == gurl_b.GetOrigin() ? PP_TRUE : PP_FALSE;
     88 }
     89 
     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) {
     94   if (!url.is_valid())
     95     return PP_MakeNull();
     96   ConvertComponents(url.parsed_for_possibly_invalid_spec(), components);
     97   return StringVar::StringToPPVar(url.possibly_invalid_spec());
     98 }
     99 
    100 PP_Var PPB_URLUtil_Shared::ConvertComponentsAndReturnURL(
    101     const PP_Var& url,
    102     PP_URLComponents_Dev* components) {
    103   if (!components)
    104     return url;  // Common case - plugin doesn't care about parsing.
    105 
    106   StringVar* url_string = StringVar::FromPPVar(url);
    107   if (!url_string)
    108     return url;
    109 
    110   PP_Var result = Canonicalize(url, components);
    111   PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(url);
    112   return result;
    113 }
    114 
    115 }  // namespace ppapi
    116