summaryrefslogtreecommitdiffstats
path: root/ppapi/shared_impl
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-18 01:50:31 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-18 01:50:31 +0000
commit9ca245e3b2d0e2271c7124d3aceb8179903718e2 (patch)
treeaa5fdbad23bd9a91cb117c1610064242d8a00985 /ppapi/shared_impl
parenta2f013ebcd62eae5238ca4ee84b9d21e34197fd8 (diff)
downloadchromium_src-9ca245e3b2d0e2271c7124d3aceb8179903718e2.zip
chromium_src-9ca245e3b2d0e2271c7124d3aceb8179903718e2.tar.gz
chromium_src-9ca245e3b2d0e2271c7124d3aceb8179903718e2.tar.bz2
Implement a proxy for URL util. Some of the implementation that doesn't need to
be proxied moved into a new shared_impl file, which required a decent amount of glue to make it callable from both the implementation and the proxy. The payoff here is only marginal since the code is fairly simple, but I decided this is still better than duplication. This also includes some comments from my audio patch that I forgot in that CL. BUG=none TEST=ppapi_tests run out of process pass Review URL: http://codereview.chromium.org/6676045 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78646 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/shared_impl')
-rw-r--r--ppapi/shared_impl/url_util_impl.cc106
-rw-r--r--ppapi/shared_impl/url_util_impl.h67
2 files changed, 173 insertions, 0 deletions
diff --git a/ppapi/shared_impl/url_util_impl.cc b/ppapi/shared_impl/url_util_impl.cc
new file mode 100644
index 0000000..32aab48
--- /dev/null
+++ b/ppapi/shared_impl/url_util_impl.cc
@@ -0,0 +1,106 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ppapi/shared_impl/url_util_impl.h"
+
+#include "googleurl/src/gurl.h"
+
+namespace pp {
+namespace shared_impl {
+
+namespace {
+
+void ConvertComponent(const url_parse::Component& input,
+ PP_URLComponent_Dev* output) {
+ output->begin = input.begin;
+ output->len = input.len;
+}
+
+// Converts components from a GoogleUrl parsed to a PPAPI parsed structure.
+// Output can be NULL to specify "do nothing." This rule is followed by all
+// the url util functions, so we implement it once here.
+//
+// Output can be NULL to specify "do nothing." This rule is followed by all the
+// url util functions, so we implement it once here.
+void ConvertComponents(const url_parse::Parsed& input,
+ PP_URLComponents_Dev* output) {
+ if (!output)
+ return;
+
+ ConvertComponent(input.scheme, &output->scheme);
+ ConvertComponent(input.username, &output->username);
+ ConvertComponent(input.password, &output->password);
+ ConvertComponent(input.host, &output->host);
+ ConvertComponent(input.port, &output->port);
+ ConvertComponent(input.path, &output->path);
+ ConvertComponent(input.query, &output->query);
+ ConvertComponent(input.ref, &output->ref);
+}
+
+} // namespace
+
+// static
+PP_Var URLUtilImpl::Canonicalize(StringFromVar string_from_var,
+ VarFromUtf8 var_from_utf8,
+ PP_Module pp_module,
+ PP_Var url,
+ PP_URLComponents_Dev* components) {
+ const std::string* url_string = string_from_var(url);
+ if (!url_string)
+ return PP_MakeNull();
+ return GenerateURLReturn(var_from_utf8, pp_module,
+ GURL(*url_string), components);
+}
+
+// static
+PP_Var URLUtilImpl::ResolveRelativeToURL(StringFromVar string_from_var,
+ VarFromUtf8 var_from_utf8,
+ PP_Module pp_module,
+ PP_Var base_url,
+ PP_Var relative,
+ PP_URLComponents_Dev* components) {
+ const std::string* base_url_string = string_from_var(base_url);
+ const std::string* relative_string = string_from_var(relative);
+ if (!base_url_string || !relative_string)
+ return PP_MakeNull();
+
+ GURL base_gurl(*base_url_string);
+ if (!base_gurl.is_valid())
+ return PP_MakeNull();
+ return GenerateURLReturn(var_from_utf8, pp_module,
+ base_gurl.Resolve(*relative_string),
+ components);
+}
+
+// static
+PP_Bool URLUtilImpl::IsSameSecurityOrigin(StringFromVar string_from_var,
+ PP_Var url_a, PP_Var url_b) {
+ const std::string* url_a_string = string_from_var(url_a);
+ const std::string* url_b_string = string_from_var(url_b);
+ if (!url_a_string || !url_b_string)
+ return PP_FALSE;
+
+ GURL gurl_a(*url_a_string);
+ GURL gurl_b(*url_b_string);
+ if (!gurl_a.is_valid() || !gurl_b.is_valid())
+ return PP_FALSE;
+
+ return gurl_a.GetOrigin() == gurl_b.GetOrigin() ? PP_TRUE : PP_FALSE;
+}
+
+// Used for returning the given GURL from a PPAPI function, with an optional
+// out param indicating the components.
+PP_Var URLUtilImpl::GenerateURLReturn(VarFromUtf8 var_from_utf8,
+ PP_Module module,
+ const GURL& url,
+ PP_URLComponents_Dev* components) {
+ if (!url.is_valid())
+ return PP_MakeNull();
+ ConvertComponents(url.parsed_for_possibly_invalid_spec(), components);
+ return var_from_utf8(module, url.possibly_invalid_spec().c_str(),
+ static_cast<uint32_t>(url.possibly_invalid_spec().size()));
+}
+
+} // namespace shared_impl
+} // namespace pp
diff --git a/ppapi/shared_impl/url_util_impl.h b/ppapi/shared_impl/url_util_impl.h
new file mode 100644
index 0000000..91c7999
--- /dev/null
+++ b/ppapi/shared_impl/url_util_impl.h
@@ -0,0 +1,67 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PPAPI_SHARED_IMPL_URL_UTIL_IMPL_H_
+#define PPAPI_SHARED_IMPL_URL_UTIL_IMPL_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "googleurl/src/url_parse.h"
+#include "ppapi/c/dev/ppb_url_util_dev.h"
+#include "ppapi/c/pp_module.h"
+#include "ppapi/c/pp_var.h"
+
+class GURL;
+
+namespace pp {
+namespace shared_impl {
+
+// Contains the implementation of PPB_URLUtil that is shared between the proxy
+// and the renderer.
+class URLUtilImpl {
+ public:
+ // The functions here would normally take the var interface for constructing
+ // return strings. However, at the current time there's some mixup between
+ // using Var and VarDeprecated. To resolve this, we instead pass the pointer
+ // to the string creation function so can be used independently of this.
+ typedef PP_Var (*VarFromUtf8)(PP_Module, const char*, uint32_t);
+
+ // Function that converts the given var to a std::string or NULL if the
+ // var is not a string or is invalid.
+ //
+ // We could use PPB_Var for this, but that interface requires an additional
+ // string conversion. Both the proxy and the host side maintain the strings
+ // in a std::string, and the form we want for passing to GURL is also a
+ // std::string. Parameterizing this separately saves this, and also solves
+ // the same problem that VarFromUtf8 does.
+ typedef const std::string* (*StringFromVar)(PP_Var var);
+
+ // PPB_URLUtil shared functions.
+ static PP_Var Canonicalize(StringFromVar string_from_var,
+ VarFromUtf8 var_from_utf8,
+ PP_Module pp_module,
+ PP_Var url,
+ PP_URLComponents_Dev* components);
+ static PP_Var ResolveRelativeToURL(StringFromVar string_from_var,
+ VarFromUtf8 var_from_utf8,
+ PP_Module pp_module,
+ PP_Var base_url,
+ PP_Var relative,
+ PP_URLComponents_Dev* components);
+ static PP_Bool IsSameSecurityOrigin(StringFromVar string_from_var,
+ PP_Var url_a, PP_Var url_b);
+
+ // Used for returning the given GURL from a PPAPI function, with an optional
+ // out param indicating the components.
+ static PP_Var GenerateURLReturn(VarFromUtf8 var_from_utf8,
+ PP_Module pp_module,
+ const GURL& url,
+ PP_URLComponents_Dev* components);
+};
+
+} // namespace shared_impl
+} // namespace pp
+
+#endif