cfe4fcb9a21398cec21c8f68c5cf8d9b13147cb7
[platform/framework/web/crosswalk.git] / src / components / dom_distiller / core / url_utils.cc
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 "grit/components_resources.h"
12 #include "net/base/url_util.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "url/gurl.h"
15
16 namespace dom_distiller {
17
18 namespace url_utils {
19
20 namespace {
21
22 const char kDummyInternalUrlPrefix[] = "chrome-distiller-internal://dummy/";
23
24 }  // namespace
25
26 const GURL GetDistillerViewUrlFromEntryId(const std::string& scheme,
27                                           const std::string& entry_id) {
28   GURL url(scheme + "://" + base::GenerateGUID());
29   return net::AppendOrReplaceQueryParameter(url, kEntryIdKey, entry_id);
30 }
31
32 const GURL GetDistillerViewUrlFromUrl(const std::string& scheme,
33                                       const GURL& view_url) {
34   GURL url(scheme + "://" + base::GenerateGUID());
35   return net::AppendOrReplaceQueryParameter(url, kUrlKey, view_url.spec());
36 }
37
38 const GURL GetOriginalUrlFromDistillerUrl(const GURL& url) {
39   if (!dom_distiller::url_utils::IsDistilledPage(url))
40     return url;
41
42   std::string original_url_str;
43   net::GetValueForKeyInQuery(url, kUrlKey, &original_url_str);
44
45   return GURL(original_url_str);
46 }
47
48 std::string GetValueForKeyInUrl(const GURL& url, const std::string& key) {
49   if (!url.is_valid())
50     return "";
51   std::string value;
52   if (net::GetValueForKeyInQuery(url, key, &value)) {
53     return value;
54   }
55   return "";
56 }
57
58 std::string GetValueForKeyInUrlPathQuery(const std::string& path,
59                                          const std::string& key) {
60   // Tools for retrieving a value in a query only works with full GURLs, so
61   // using a dummy scheme and host to create a fake URL which can be parsed.
62   GURL dummy_url(kDummyInternalUrlPrefix + path);
63   return GetValueForKeyInUrl(dummy_url, key);
64 }
65
66 bool IsUrlDistillable(const GURL& url) {
67   return url.is_valid() && url.SchemeIsHTTPOrHTTPS();
68 }
69
70 bool IsDistilledPage(const GURL& url) {
71   return url.is_valid() && url.scheme() == kDomDistillerScheme;
72 }
73
74 std::string GetIsDistillableJs() {
75   return ResourceBundle::GetSharedInstance()
76       .GetRawDataResource(IDR_IS_DISTILLABLE_JS).as_string();
77 }
78
79 }  // namespace url_utils
80
81 }  // namespace dom_distiller