Upstream version 9.38.198.0
[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/component_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 std::string GetValueForKeyInUrl(const GURL& url, const std::string& key) {
39   if (!url.is_valid())
40     return "";
41   std::string value;
42   if (net::GetValueForKeyInQuery(url, key, &value)) {
43     return value;
44   }
45   return "";
46 }
47
48 std::string GetValueForKeyInUrlPathQuery(const std::string& path,
49                                          const std::string& key) {
50   // Tools for retrieving a value in a query only works with full GURLs, so
51   // using a dummy scheme and host to create a fake URL which can be parsed.
52   GURL dummy_url(kDummyInternalUrlPrefix + path);
53   return GetValueForKeyInUrl(dummy_url, key);
54 }
55
56 bool IsUrlDistillable(const GURL& url) {
57   return url.is_valid() && url.SchemeIsHTTPOrHTTPS();
58 }
59
60 bool IsDistilledPage(const GURL& url) {
61   return url.is_valid() && url.scheme() == kDomDistillerScheme;
62 }
63
64 std::string GetIsDistillableJs() {
65   return ResourceBundle::GetSharedInstance()
66       .GetRawDataResource(IDR_IS_DISTILLABLE_JS).as_string();
67 }
68
69 }  // namespace url_utils
70
71 }  // namespace dom_distiller