url_utils.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include "components/dom_distiller/core/url_utils.h"
  5. #include <string>
  6. #include "base/guid.h"
  7. #include "base/strings/string_number_conversions.h"
  8. #include "base/strings/string_split.h"
  9. #include "base/strings/string_util.h"
  10. #include "components/dom_distiller/core/url_constants.h"
  11. #include "components/grit/components_resources.h"
  12. #include "crypto/sha2.h"
  13. #include "net/base/url_util.h"
  14. #include "ui/base/resource/resource_bundle.h"
  15. #include "url/gurl.h"
  16. #include "url/url_util.h"
  17. namespace dom_distiller {
  18. namespace url_utils {
  19. namespace {
  20. const char kDummyInternalUrlPrefix[] = "chrome-distiller-internal://dummy/";
  21. const char kSeparator[] = "_";
  22. std::string SHA256InHex(base::StringPiece str) {
  23. std::string sha256 = crypto::SHA256HashString(str);
  24. return base::ToLowerASCII(base::HexEncode(sha256.c_str(), sha256.size()));
  25. }
  26. } // namespace
  27. const GURL GetDistillerViewUrlFromEntryId(const std::string& scheme,
  28. const std::string& entry_id) {
  29. GURL url(scheme + "://" + base::GenerateGUID());
  30. return net::AppendOrReplaceQueryParameter(url, kEntryIdKey, entry_id);
  31. }
  32. const GURL GetDistillerViewUrlFromUrl(const std::string& scheme,
  33. const GURL& url,
  34. const std::string& title,
  35. int64_t start_time_ms) {
  36. GURL view_url(scheme + "://" + base::GenerateGUID() + kSeparator +
  37. SHA256InHex(url.spec()));
  38. view_url = net::AppendOrReplaceQueryParameter(view_url, kTitleKey, title);
  39. if (start_time_ms > 0) {
  40. view_url = net::AppendOrReplaceQueryParameter(
  41. view_url, kTimeKey, base::NumberToString(start_time_ms));
  42. }
  43. return net::AppendOrReplaceQueryParameter(view_url, kUrlKey, url.spec());
  44. }
  45. const GURL GetOriginalUrlFromDistillerUrl(const GURL& url) {
  46. if (!IsUrlDistilledFormat(url))
  47. return url;
  48. std::string original_url_str;
  49. net::GetValueForKeyInQuery(url, kUrlKey, &original_url_str);
  50. // Make sure kDomDistillerScheme is considered standard scheme for
  51. // |GURL::host_piece()| to work correctly.
  52. DCHECK(url::IsStandard(kDomDistillerScheme,
  53. url::Component(0, strlen(kDomDistillerScheme))));
  54. std::vector<base::StringPiece> pieces =
  55. base::SplitStringPiece(url.host_piece(), kSeparator,
  56. base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  57. if (pieces.size() != 2)
  58. return GURL();
  59. if (SHA256InHex(original_url_str) != pieces[1])
  60. return GURL();
  61. const GURL original_url(original_url_str);
  62. if (!IsUrlDistillable(original_url))
  63. return GURL();
  64. return original_url;
  65. }
  66. int64_t GetTimeFromDistillerUrl(const GURL& url) {
  67. if (!IsDistilledPage(url))
  68. return 0;
  69. std::string time_str;
  70. if (!net::GetValueForKeyInQuery(url, kTimeKey, &time_str))
  71. return 0;
  72. int64_t time_int = 0;
  73. if (!base::StringToInt64(time_str, &time_int))
  74. return 0;
  75. return time_int;
  76. }
  77. std::string GetTitleFromDistillerUrl(const GURL& url) {
  78. if (!IsDistilledPage(url))
  79. return "";
  80. std::string title;
  81. if (!net::GetValueForKeyInQuery(url, kTitleKey, &title))
  82. return "";
  83. return title;
  84. }
  85. std::string GetValueForKeyInUrl(const GURL& url, const std::string& key) {
  86. if (!url.is_valid())
  87. return "";
  88. std::string value;
  89. if (net::GetValueForKeyInQuery(url, key, &value)) {
  90. return value;
  91. }
  92. return "";
  93. }
  94. std::string GetValueForKeyInUrlPathQuery(const std::string& path,
  95. const std::string& key) {
  96. // Tools for retrieving a value in a query only works with full GURLs, so
  97. // using a dummy scheme and host to create a fake URL which can be parsed.
  98. GURL dummy_url(kDummyInternalUrlPrefix + path);
  99. return GetValueForKeyInUrl(dummy_url, key);
  100. }
  101. bool IsUrlDistillable(const GURL& url) {
  102. return url.is_valid() && url.SchemeIsHTTPOrHTTPS();
  103. }
  104. bool IsDistilledPage(const GURL& url) {
  105. return IsUrlDistilledFormat(url) &&
  106. GetOriginalUrlFromDistillerUrl(url).is_valid();
  107. }
  108. bool IsUrlDistilledFormat(const GURL& url) {
  109. return url.is_valid() && url.scheme() == kDomDistillerScheme;
  110. }
  111. } // namespace url_utils
  112. } // namespace dom_distiller