| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/predictor.h" | 5 #include "chrome/browser/net/predictor.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <sstream> | 10 #include <sstream> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/string_split.h" |
| 18 #include "base/string_util.h" |
| 17 #include "base/stringprintf.h" | 19 #include "base/stringprintf.h" |
| 18 #include "base/synchronization/waitable_event.h" | 20 #include "base/synchronization/waitable_event.h" |
| 19 #include "base/time.h" | 21 #include "base/time.h" |
| 20 #include "base/values.h" | 22 #include "base/values.h" |
| 21 #include "chrome/browser/io_thread.h" | 23 #include "chrome/browser/io_thread.h" |
| 22 #include "chrome/browser/net/preconnect.h" | 24 #include "chrome/browser/net/preconnect.h" |
| 23 #include "chrome/browser/prefs/browser_prefs.h" | 25 #include "chrome/browser/prefs/browser_prefs.h" |
| 24 #include "chrome/browser/prefs/pref_service.h" | 26 #include "chrome/browser/prefs/pref_service.h" |
| 25 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 27 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 26 #include "chrome/browser/prefs/session_startup_pref.h" | 28 #include "chrome/browser/prefs/session_startup_pref.h" |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 if (predictor && predictor->predictor_enabled()) { | 471 if (predictor && predictor->predictor_enabled()) { |
| 470 predictor->GetHtmlInfo(output); | 472 predictor->GetHtmlInfo(output); |
| 471 } else { | 473 } else { |
| 472 output->append("DNS pre-resolution and TCP pre-connection is disabled."); | 474 output->append("DNS pre-resolution and TCP pre-connection is disabled."); |
| 473 } | 475 } |
| 474 output->append("</body></html>"); | 476 output->append("</body></html>"); |
| 475 } | 477 } |
| 476 | 478 |
| 477 // Provide sort order so all .com's are together, etc. | 479 // Provide sort order so all .com's are together, etc. |
| 478 struct RightToLeftStringSorter { | 480 struct RightToLeftStringSorter { |
| 479 bool operator()(const GURL& left, | 481 bool operator()(const GURL& left, const GURL& right) const { |
| 480 const GURL& right) const { | 482 return ReverseComponents(left) < ReverseComponents(right); |
| 481 return string_compare(left.host(), right.host()); | |
| 482 } | 483 } |
| 483 | 484 |
| 484 static bool string_compare(const std::string& left_host, | 485 private: |
| 485 const std::string& right_host) { | 486 // Transforms something like "http://www.google.com/xyz" to |
| 486 if (left_host == right_host) return true; | 487 // "http://com.google.www/xyz". |
| 487 size_t left_already_matched = left_host.size(); | 488 static std::string ReverseComponents(const GURL& url) { |
| 488 size_t right_already_matched = right_host.size(); | 489 // Reverse the components in the hostname. |
| 490 std::vector<std::string> parts; |
| 491 base::SplitString(url.host(), '.', &parts); |
| 492 std::reverse(parts.begin(), parts.end()); |
| 493 std::string reversed_host = JoinString(parts, '.'); |
| 489 | 494 |
| 490 // Ensure both strings have characters. | 495 // Return the new URL. |
| 491 if (!left_already_matched) return true; | 496 GURL::Replacements url_components; |
| 492 if (!right_already_matched) return false; | 497 url_components.SetHostStr(reversed_host); |
| 493 | 498 return url.ReplaceComponents(url_components).spec(); |
| 494 // Watch for trailing dot, so we'll always be safe to go one beyond dot. | |
| 495 if ('.' == left_host[left_already_matched - 1]) { | |
| 496 if ('.' != right_host[right_already_matched - 1]) | |
| 497 return true; | |
| 498 // Both have dots at end of string. | |
| 499 --left_already_matched; | |
| 500 --right_already_matched; | |
| 501 } else { | |
| 502 if ('.' == right_host[right_already_matched - 1]) | |
| 503 return false; | |
| 504 } | |
| 505 | |
| 506 while (1) { | |
| 507 if (!left_already_matched) return true; | |
| 508 if (!right_already_matched) return false; | |
| 509 | |
| 510 size_t left_start = left_host.find_last_of('.', left_already_matched - 1); | |
| 511 if (std::string::npos == left_start) { | |
| 512 left_already_matched = left_start = 0; | |
| 513 } else { | |
| 514 left_already_matched = left_start; | |
| 515 ++left_start; // Don't compare the dot. | |
| 516 } | |
| 517 size_t right_start = right_host.find_last_of('.', | |
| 518 right_already_matched - 1); | |
| 519 if (std::string::npos == right_start) { | |
| 520 right_already_matched = right_start = 0; | |
| 521 } else { | |
| 522 right_already_matched = right_start; | |
| 523 ++right_start; // Don't compare the dot. | |
| 524 } | |
| 525 | |
| 526 int diff = left_host.compare(left_start, left_host.size(), | |
| 527 right_host, right_start, right_host.size()); | |
| 528 if (diff > 0) return false; | |
| 529 if (diff < 0) return true; | |
| 530 } | |
| 531 } | 499 } |
| 532 }; | 500 }; |
| 533 | 501 |
| 534 void Predictor::GetHtmlReferrerLists(std::string* output) { | 502 void Predictor::GetHtmlReferrerLists(std::string* output) { |
| 535 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 503 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 536 if (referrers_.empty()) | 504 if (referrers_.empty()) |
| 537 return; | 505 return; |
| 538 | 506 |
| 539 // TODO(jar): Remove any plausible JavaScript from names before displaying. | 507 // TODO(jar): Remove any plausible JavaScript from names before displaying. |
| 540 | 508 |
| (...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1219 IOThread* io_thread, | 1187 IOThread* io_thread, |
| 1220 net::URLRequestContextGetter* getter) { | 1188 net::URLRequestContextGetter* getter) { |
| 1221 // Empty function for unittests. | 1189 // Empty function for unittests. |
| 1222 } | 1190 } |
| 1223 | 1191 |
| 1224 void SimplePredictor::ShutdownOnUIThread(PrefService* user_prefs) { | 1192 void SimplePredictor::ShutdownOnUIThread(PrefService* user_prefs) { |
| 1225 SetShutdown(true); | 1193 SetShutdown(true); |
| 1226 } | 1194 } |
| 1227 | 1195 |
| 1228 } // namespace chrome_browser_net | 1196 } // namespace chrome_browser_net |
| OLD | NEW |