• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 "components/dom_distiller/core/url_utils.h"
6 
7 #include <string>
8 
9 #include "base/guid.h"
10 #include "components/dom_distiller/core/url_constants.h"
11 #include "net/base/url_util.h"
12 #include "url/gurl.h"
13 
14 namespace dom_distiller {
15 
16 namespace url_utils {
17 
18 namespace {
19 
20 const char kDummyInternalUrlPrefix[] = "chrome-distiller-internal://dummy/";
21 
22 }  // namespace
23 
GetDistillerViewUrlFromEntryId(const std::string & scheme,const std::string & entry_id)24 const GURL GetDistillerViewUrlFromEntryId(const std::string& scheme,
25                                           const std::string& entry_id) {
26   GURL url(scheme + "://" + base::GenerateGUID());
27   return net::AppendOrReplaceQueryParameter(url, kEntryIdKey, entry_id);
28 }
29 
GetDistillerViewUrlFromUrl(const std::string & scheme,const GURL & view_url)30 const GURL GetDistillerViewUrlFromUrl(const std::string& scheme,
31                                       const GURL& view_url) {
32   GURL url(scheme + "://" + base::GenerateGUID());
33   return net::AppendOrReplaceQueryParameter(url, kUrlKey, view_url.spec());
34 }
35 
GetValueForKeyInUrlPathQuery(const std::string & path,const std::string & key)36 std::string GetValueForKeyInUrlPathQuery(const std::string& path,
37                                          const std::string& key) {
38   // Tools for retrieving a value in a query only works with full GURLs, so
39   // using a dummy scheme and host to create a fake URL which can be parsed.
40   GURL dummy_url(kDummyInternalUrlPrefix + path);
41   std::string value;
42   net::GetValueForKeyInQuery(dummy_url, key, &value);
43   return value;
44 }
45 
IsUrlDistillable(const GURL & url)46 bool IsUrlDistillable(const GURL& url) {
47   return url.is_valid() && url.SchemeIsHTTPOrHTTPS();
48 }
49 
IsUrlReportable(const std::string & scheme,const GURL & url)50 bool IsUrlReportable(const std::string& scheme, const GURL& url) {
51   return url.is_valid() && url.scheme() == scheme;
52 }
53 
54 }  // namespace url_utils
55 
56 }  // namespace dom_distiller
57