Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2394)

Unified Diff: chrome/browser/net/predictor.cc

Issue 9555017: Fix a crash when viewing about:dns. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/net/predictor_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/net/predictor.cc
diff --git a/chrome/browser/net/predictor.cc b/chrome/browser/net/predictor.cc
index f30e4ae38642117164894126a44a0b3a11475485..103621bd96c64363381c3b5d14c4c98794883ba8 100644
--- a/chrome/browser/net/predictor.cc
+++ b/chrome/browser/net/predictor.cc
@@ -14,6 +14,8 @@
#include "base/compiler_specific.h"
#include "base/metrics/histogram.h"
#include "base/stl_util.h"
+#include "base/string_split.h"
+#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/time.h"
@@ -476,58 +478,24 @@ void Predictor::PredictorGetHtmlInfo(Predictor* predictor,
// Provide sort order so all .com's are together, etc.
struct RightToLeftStringSorter {
- bool operator()(const GURL& left,
- const GURL& right) const {
- return string_compare(left.host(), right.host());
+ bool operator()(const GURL& left, const GURL& right) const {
+ return ReverseComponents(left) < ReverseComponents(right);
}
- static bool string_compare(const std::string& left_host,
- const std::string& right_host) {
- if (left_host == right_host) return true;
- size_t left_already_matched = left_host.size();
- size_t right_already_matched = right_host.size();
-
- // Ensure both strings have characters.
- if (!left_already_matched) return true;
- if (!right_already_matched) return false;
-
- // Watch for trailing dot, so we'll always be safe to go one beyond dot.
- if ('.' == left_host[left_already_matched - 1]) {
- if ('.' != right_host[right_already_matched - 1])
- return true;
- // Both have dots at end of string.
- --left_already_matched;
- --right_already_matched;
- } else {
- if ('.' == right_host[right_already_matched - 1])
- return false;
- }
-
- while (1) {
- if (!left_already_matched) return true;
- if (!right_already_matched) return false;
-
- size_t left_start = left_host.find_last_of('.', left_already_matched - 1);
- if (std::string::npos == left_start) {
- left_already_matched = left_start = 0;
- } else {
- left_already_matched = left_start;
- ++left_start; // Don't compare the dot.
- }
- size_t right_start = right_host.find_last_of('.',
- right_already_matched - 1);
- if (std::string::npos == right_start) {
- right_already_matched = right_start = 0;
- } else {
- right_already_matched = right_start;
- ++right_start; // Don't compare the dot.
- }
-
- int diff = left_host.compare(left_start, left_host.size(),
- right_host, right_start, right_host.size());
- if (diff > 0) return false;
- if (diff < 0) return true;
- }
+ private:
+ // Transforms something like "http://www.google.com/xyz" to
+ // "http://com.google.www/xyz".
+ static std::string ReverseComponents(const GURL& url) {
+ // Reverse the components in the hostname.
+ std::vector<std::string> parts;
+ base::SplitString(url.host(), '.', &parts);
+ std::reverse(parts.begin(), parts.end());
+ std::string reversed_host = JoinString(parts, '.');
+
+ // Return the new URL.
+ GURL::Replacements url_components;
+ url_components.SetHostStr(reversed_host);
+ return url.ReplaceComponents(url_components).spec();
}
};
« no previous file with comments | « no previous file | chrome/browser/net/predictor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698