• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that can
3 // be found in the LICENSE file.
4 
5 #include <sstream>
6 
7 #include "include/cef_parser.h"
8 
9 #include "base/base64.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "components/url_formatter/elide_url.h"
12 #include "net/base/escape.h"
13 #include "net/base/mime_util.h"
14 #include "url/gurl.h"
15 
CefParseURL(const CefString & url,CefURLParts & parts)16 bool CefParseURL(const CefString& url, CefURLParts& parts) {
17   GURL gurl(url.ToString());
18   if (!gurl.is_valid())
19     return false;
20 
21   CefString(&parts.spec).FromString(gurl.spec());
22   CefString(&parts.scheme).FromString(gurl.scheme());
23   CefString(&parts.username).FromString(gurl.username());
24   CefString(&parts.password).FromString(gurl.password());
25   CefString(&parts.host).FromString(gurl.host());
26   CefString(&parts.origin).FromString(gurl.DeprecatedGetOriginAsURL().spec());
27   CefString(&parts.port).FromString(gurl.port());
28   CefString(&parts.path).FromString(gurl.path());
29   CefString(&parts.query).FromString(gurl.query());
30   CefString(&parts.fragment).FromString(gurl.ref());
31 
32   return true;
33 }
34 
CefCreateURL(const CefURLParts & parts,CefString & url)35 bool CefCreateURL(const CefURLParts& parts, CefString& url) {
36   std::string spec = CefString(parts.spec.str, parts.spec.length, false);
37   std::string scheme = CefString(parts.scheme.str, parts.scheme.length, false);
38   std::string username =
39       CefString(parts.username.str, parts.username.length, false);
40   std::string password =
41       CefString(parts.password.str, parts.password.length, false);
42   std::string host = CefString(parts.host.str, parts.host.length, false);
43   std::string port = CefString(parts.port.str, parts.port.length, false);
44   std::string path = CefString(parts.path.str, parts.path.length, false);
45   std::string query = CefString(parts.query.str, parts.query.length, false);
46   std::string fragment =
47       CefString(parts.fragment.str, parts.fragment.length, false);
48 
49   GURL gurl;
50   if (!spec.empty()) {
51     gurl = GURL(spec);
52   } else if (!scheme.empty() && !host.empty()) {
53     std::stringstream ss;
54     ss << scheme << "://";
55     if (!username.empty()) {
56       ss << username;
57       if (!password.empty())
58         ss << ":" << password;
59       ss << "@";
60     }
61     ss << host;
62     if (!port.empty())
63       ss << ":" << port;
64     if (!path.empty())
65       ss << path;
66     if (!query.empty())
67       ss << "?" << query;
68     if (!fragment.empty())
69       ss << "#" << fragment;
70     gurl = GURL(ss.str());
71   }
72 
73   if (gurl.is_valid()) {
74     url = gurl.spec();
75     return true;
76   }
77 
78   return false;
79 }
80 
CefFormatUrlForSecurityDisplay(const CefString & origin_url)81 CefString CefFormatUrlForSecurityDisplay(const CefString& origin_url) {
82   return url_formatter::FormatUrlForSecurityDisplay(
83       GURL(origin_url.ToString()));
84 }
85 
CefGetMimeType(const CefString & extension)86 CefString CefGetMimeType(const CefString& extension) {
87   // Requests should not block on the disk!  On POSIX this goes to disk.
88   // http://code.google.com/p/chromium/issues/detail?id=59849
89   base::ThreadRestrictions::ScopedAllowIO allow_io;
90 
91   std::string mime_type;
92   net::GetMimeTypeFromExtension(extension, &mime_type);
93   return mime_type;
94 }
95 
CefGetExtensionsForMimeType(const CefString & mime_type,std::vector<CefString> & extensions)96 void CefGetExtensionsForMimeType(const CefString& mime_type,
97                                  std::vector<CefString>& extensions) {
98   using VectorType = std::vector<base::FilePath::StringType>;
99   VectorType ext;
100   net::GetExtensionsForMimeType(mime_type, &ext);
101   VectorType::const_iterator it = ext.begin();
102   for (; it != ext.end(); ++it)
103     extensions.push_back(*it);
104 }
105 
CefBase64Encode(const void * data,size_t data_size)106 CefString CefBase64Encode(const void* data, size_t data_size) {
107   if (data_size == 0)
108     return CefString();
109 
110   base::StringPiece input(static_cast<const char*>(data), data_size);
111   std::string output;
112   base::Base64Encode(input, &output);
113   return output;
114 }
115 
CefBase64Decode(const CefString & data)116 CefRefPtr<CefBinaryValue> CefBase64Decode(const CefString& data) {
117   if (data.size() == 0)
118     return nullptr;
119 
120   const std::string& input = data;
121   std::string output;
122   if (base::Base64Decode(input, &output))
123     return CefBinaryValue::Create(output.data(), output.size());
124   return nullptr;
125 }
126 
CefURIEncode(const CefString & text,bool use_plus)127 CefString CefURIEncode(const CefString& text, bool use_plus) {
128   return net::EscapeQueryParamValue(text.ToString(), use_plus);
129 }
130 
CefURIDecode(const CefString & text,bool convert_to_utf8,cef_uri_unescape_rule_t unescape_rule)131 CefString CefURIDecode(const CefString& text,
132                        bool convert_to_utf8,
133                        cef_uri_unescape_rule_t unescape_rule) {
134   const net::UnescapeRule::Type type =
135       static_cast<net::UnescapeRule::Type>(unescape_rule);
136   if (convert_to_utf8)
137     return net::UnescapeAndDecodeUTF8URLComponentWithAdjustments(
138         text.ToString(), type, nullptr);
139   else
140     return net::UnescapeURLComponent(text.ToString(), type);
141 }
142