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/prerender/prerender_local_predictor.h" | 5 #include "chrome/browser/prerender/prerender_local_predictor.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <map> | 10 #include <map> |
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
633 "candidates": [ | 633 "candidates": [ |
634 { "url": "http://www.cnn.com/sports/" | 634 { "url": "http://www.cnn.com/sports/" |
635 }, | 635 }, |
636 { "url": "http://www.cnn.com/politics/" | 636 { "url": "http://www.cnn.com/politics/" |
637 } | 637 } |
638 ] | 638 ] |
639 } | 639 } |
640 } | 640 } |
641 } | 641 } |
642 */ | 642 */ |
643 DictionaryValue json_data; | 643 base::DictionaryValue json_data; |
644 DictionaryValue* req = new DictionaryValue(); | 644 base::DictionaryValue* req = new base::DictionaryValue(); |
645 req->SetInteger("version", 1); | 645 req->SetInteger("version", 1); |
646 req->SetInteger("behavior_id", GetPrerenderServiceBehaviorID()); | 646 req->SetInteger("behavior_id", GetPrerenderServiceBehaviorID()); |
647 if (ShouldQueryPrerenderServiceForCurrentURL() && | 647 if (ShouldQueryPrerenderServiceForCurrentURL() && |
648 info->source_url_.url_lookup_success) { | 648 info->source_url_.url_lookup_success) { |
649 ListValue* browse_history = new ListValue(); | 649 base::ListValue* browse_history = new base::ListValue(); |
650 DictionaryValue* browse_item = new DictionaryValue(); | 650 base::DictionaryValue* browse_item = new base::DictionaryValue(); |
651 browse_item->SetString("url", info->source_url_.url.spec()); | 651 browse_item->SetString("url", info->source_url_.url.spec()); |
652 browse_history->Append(browse_item); | 652 browse_history->Append(browse_item); |
653 DictionaryValue* hint_request = new DictionaryValue(); | 653 base::DictionaryValue* hint_request = new base::DictionaryValue(); |
654 hint_request->Set("browse_history", browse_history); | 654 hint_request->Set("browse_history", browse_history); |
655 req->Set("hint_request", hint_request); | 655 req->Set("hint_request", hint_request); |
656 } | 656 } |
657 int num_candidate_urls = 0; | 657 int num_candidate_urls = 0; |
658 for (int i = 0; i < static_cast<int>(info->candidate_urls_.size()); i++) { | 658 for (int i = 0; i < static_cast<int>(info->candidate_urls_.size()); i++) { |
659 if (info->candidate_urls_[i].url_lookup_success) | 659 if (info->candidate_urls_[i].url_lookup_success) |
660 num_candidate_urls++; | 660 num_candidate_urls++; |
661 } | 661 } |
662 if (ShouldQueryPrerenderServiceForCandidateURLs() && | 662 if (ShouldQueryPrerenderServiceForCandidateURLs() && |
663 num_candidate_urls > 0) { | 663 num_candidate_urls > 0) { |
664 ListValue* candidates = new ListValue(); | 664 base::ListValue* candidates = new base::ListValue(); |
665 DictionaryValue* candidate; | 665 base::DictionaryValue* candidate; |
666 for (int i = 0; i < static_cast<int>(info->candidate_urls_.size()); i++) { | 666 for (int i = 0; i < static_cast<int>(info->candidate_urls_.size()); i++) { |
667 if (info->candidate_urls_[i].url_lookup_success) { | 667 if (info->candidate_urls_[i].url_lookup_success) { |
668 candidate = new DictionaryValue(); | 668 candidate = new base::DictionaryValue(); |
669 candidate->SetString("url", info->candidate_urls_[i].url.spec()); | 669 candidate->SetString("url", info->candidate_urls_[i].url.spec()); |
670 candidates->Append(candidate); | 670 candidates->Append(candidate); |
671 } | 671 } |
672 } | 672 } |
673 DictionaryValue* candidate_check_request = new DictionaryValue(); | 673 base::DictionaryValue* candidate_check_request = |
| 674 new base::DictionaryValue(); |
674 candidate_check_request->Set("candidates", candidates); | 675 candidate_check_request->Set("candidates", candidates); |
675 req->Set("candidate_check_request", candidate_check_request); | 676 req->Set("candidate_check_request", candidate_check_request); |
676 } | 677 } |
677 json_data.Set("prerender_request", req); | 678 json_data.Set("prerender_request", req); |
678 string request_string; | 679 string request_string; |
679 base::JSONWriter::Write(&json_data, &request_string); | 680 base::JSONWriter::Write(&json_data, &request_string); |
680 GURL fetch_url(GetPrerenderServiceURLPrefix() + | 681 GURL fetch_url(GetPrerenderServiceURLPrefix() + |
681 net::EscapeQueryParamValue(request_string, false)); | 682 net::EscapeQueryParamValue(request_string, false)); |
682 net::URLFetcher* fetcher = net::URLFetcher::Create( | 683 net::URLFetcher* fetcher = net::URLFetcher::Create( |
683 0, | 684 0, |
(...skipping 24 matching lines...) Expand all Loading... |
708 if (it == outstanding_prerender_service_requests_.end()) | 709 if (it == outstanding_prerender_service_requests_.end()) |
709 return; | 710 return; |
710 delete it->first; | 711 delete it->first; |
711 scoped_ptr<CandidatePrerenderInfo> info(it->second); | 712 scoped_ptr<CandidatePrerenderInfo> info(it->second); |
712 outstanding_prerender_service_requests_.erase(it); | 713 outstanding_prerender_service_requests_.erase(it); |
713 RecordEvent(EVENT_PRERENDER_SERVICE_LOOKUP_TIMED_OUT); | 714 RecordEvent(EVENT_PRERENDER_SERVICE_LOOKUP_TIMED_OUT); |
714 DoLoggedInLookup(info.Pass()); | 715 DoLoggedInLookup(info.Pass()); |
715 } | 716 } |
716 | 717 |
717 bool PrerenderLocalPredictor::ApplyParsedPrerenderServiceResponse( | 718 bool PrerenderLocalPredictor::ApplyParsedPrerenderServiceResponse( |
718 DictionaryValue* dict, | 719 base::DictionaryValue* dict, |
719 CandidatePrerenderInfo* info, | 720 CandidatePrerenderInfo* info, |
720 bool* hinting_timed_out, | 721 bool* hinting_timed_out, |
721 bool* hinting_url_lookup_timed_out, | 722 bool* hinting_url_lookup_timed_out, |
722 bool* candidate_url_lookup_timed_out) { | 723 bool* candidate_url_lookup_timed_out) { |
723 /* | 724 /* |
724 Process the response to the request. | 725 Process the response to the request. |
725 Here is a sample response to illustrate the format. | 726 Here is a sample response to illustrate the format. |
726 { | 727 { |
727 "prerender_response": { | 728 "prerender_response": { |
728 "behavior_id": 6, | 729 "behavior_id": 6, |
(...skipping 20 matching lines...) Expand all Loading... |
749 }, | 750 }, |
750 { "url": "http://www.cnn.com/politics/", | 751 { "url": "http://www.cnn.com/politics/", |
751 "in_index": 0, | 752 "in_index": 0, |
752 "in_index_timed_out": "1" | 753 "in_index_timed_out": "1" |
753 } | 754 } |
754 ] | 755 ] |
755 } | 756 } |
756 } | 757 } |
757 } | 758 } |
758 */ | 759 */ |
759 ListValue* list = NULL; | 760 base::ListValue* list = NULL; |
760 int int_value; | 761 int int_value; |
761 if (!dict->GetInteger("prerender_response.behavior_id", &int_value) || | 762 if (!dict->GetInteger("prerender_response.behavior_id", &int_value) || |
762 int_value != GetPrerenderServiceBehaviorID()) { | 763 int_value != GetPrerenderServiceBehaviorID()) { |
763 return false; | 764 return false; |
764 } | 765 } |
765 if (!dict->GetList("prerender_response.candidate_check_response.candidates", | 766 if (!dict->GetList("prerender_response.candidate_check_response.candidates", |
766 &list)) { | 767 &list)) { |
767 if (ShouldQueryPrerenderServiceForCandidateURLs()) { | 768 if (ShouldQueryPrerenderServiceForCandidateURLs()) { |
768 for (int i = 0; i < static_cast<int>(info->candidate_urls_.size()); i++) { | 769 for (int i = 0; i < static_cast<int>(info->candidate_urls_.size()); i++) { |
769 if (info->candidate_urls_[i].url_lookup_success) | 770 if (info->candidate_urls_[i].url_lookup_success) |
770 return false; | 771 return false; |
771 } | 772 } |
772 } | 773 } |
773 } else { | 774 } else { |
774 for (size_t i = 0; i < list->GetSize(); i++) { | 775 for (size_t i = 0; i < list->GetSize(); i++) { |
775 DictionaryValue* d; | 776 base::DictionaryValue* d; |
776 if (!list->GetDictionary(i, &d)) | 777 if (!list->GetDictionary(i, &d)) |
777 return false; | 778 return false; |
778 string url_string; | 779 string url_string; |
779 if (!d->GetString("url", &url_string) || !GURL(url_string).is_valid()) | 780 if (!d->GetString("url", &url_string) || !GURL(url_string).is_valid()) |
780 return false; | 781 return false; |
781 GURL url(url_string); | 782 GURL url(url_string); |
782 int in_index_timed_out = 0; | 783 int in_index_timed_out = 0; |
783 int in_index = 0; | 784 int in_index = 0; |
784 if ((!d->GetInteger("in_index_timed_out", &in_index_timed_out) || | 785 if ((!d->GetInteger("in_index_timed_out", &in_index_timed_out) || |
785 in_index_timed_out != 1) && | 786 in_index_timed_out != 1) && |
(...skipping 28 matching lines...) Expand all Loading... |
814 list = NULL; | 815 list = NULL; |
815 if (dict->GetInteger("prerender_response.hint_response.hinting_timed_out", | 816 if (dict->GetInteger("prerender_response.hint_response.hinting_timed_out", |
816 &int_value) && | 817 &int_value) && |
817 int_value == 1) { | 818 int_value == 1) { |
818 *hinting_timed_out = true; | 819 *hinting_timed_out = true; |
819 } else if (!dict->GetList("prerender_response.hint_response.candidates", | 820 } else if (!dict->GetList("prerender_response.hint_response.candidates", |
820 &list)) { | 821 &list)) { |
821 return false; | 822 return false; |
822 } else { | 823 } else { |
823 for (int i = 0; i < static_cast<int>(list->GetSize()); i++) { | 824 for (int i = 0; i < static_cast<int>(list->GetSize()); i++) { |
824 DictionaryValue* d; | 825 base::DictionaryValue* d; |
825 if (!list->GetDictionary(i, &d)) | 826 if (!list->GetDictionary(i, &d)) |
826 return false; | 827 return false; |
827 string url; | 828 string url; |
828 double priority; | 829 double priority; |
829 if (!d->GetString("url", &url) || !d->GetDouble("likelihood", &priority) | 830 if (!d->GetString("url", &url) || !d->GetDouble("likelihood", &priority) |
830 || !GURL(url).is_valid()) { | 831 || !GURL(url).is_valid()) { |
831 return false; | 832 return false; |
832 } | 833 } |
833 int in_index_timed_out = 0; | 834 int in_index_timed_out = 0; |
834 int in_index = 0; | 835 int in_index = 0; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
866 if (it == outstanding_prerender_service_requests_.end()) { | 867 if (it == outstanding_prerender_service_requests_.end()) { |
867 RecordEvent(EVENT_PRERENDER_SERVICE_NO_RECORD_FOR_RESULT); | 868 RecordEvent(EVENT_PRERENDER_SERVICE_NO_RECORD_FOR_RESULT); |
868 return; | 869 return; |
869 } | 870 } |
870 scoped_ptr<CandidatePrerenderInfo> info(it->second); | 871 scoped_ptr<CandidatePrerenderInfo> info(it->second); |
871 outstanding_prerender_service_requests_.erase(it); | 872 outstanding_prerender_service_requests_.erase(it); |
872 TIMING_HISTOGRAM("Prerender.LocalPredictorServiceLookupTime", | 873 TIMING_HISTOGRAM("Prerender.LocalPredictorServiceLookupTime", |
873 base::Time::Now() - info->start_time_); | 874 base::Time::Now() - info->start_time_); |
874 string result; | 875 string result; |
875 fetcher->GetResponseAsString(&result); | 876 fetcher->GetResponseAsString(&result); |
876 scoped_ptr<Value> root; | 877 scoped_ptr<base::Value> root; |
877 root.reset(base::JSONReader::Read(result)); | 878 root.reset(base::JSONReader::Read(result)); |
878 bool hinting_timed_out = false; | 879 bool hinting_timed_out = false; |
879 bool hinting_url_lookup_timed_out = false; | 880 bool hinting_url_lookup_timed_out = false; |
880 bool candidate_url_lookup_timed_out = false; | 881 bool candidate_url_lookup_timed_out = false; |
881 if (!root.get() || !root->IsType(Value::TYPE_DICTIONARY)) { | 882 if (!root.get() || !root->IsType(base::Value::TYPE_DICTIONARY)) { |
882 RecordEvent(EVENT_PRERENDER_SERVICE_PARSE_ERROR_INCORRECT_JSON); | 883 RecordEvent(EVENT_PRERENDER_SERVICE_PARSE_ERROR_INCORRECT_JSON); |
883 } else { | 884 } else { |
884 if (ApplyParsedPrerenderServiceResponse( | 885 if (ApplyParsedPrerenderServiceResponse( |
885 static_cast<DictionaryValue*>(root.get()), | 886 static_cast<base::DictionaryValue*>(root.get()), |
886 info.get(), | 887 info.get(), |
887 &hinting_timed_out, | 888 &hinting_timed_out, |
888 &hinting_url_lookup_timed_out, | 889 &hinting_url_lookup_timed_out, |
889 &candidate_url_lookup_timed_out)) { | 890 &candidate_url_lookup_timed_out)) { |
890 // We finished parsing the result, and found no errors. | 891 // We finished parsing the result, and found no errors. |
891 RecordEvent(EVENT_PRERENDER_SERVICE_PARSED_CORRECTLY); | 892 RecordEvent(EVENT_PRERENDER_SERVICE_PARSED_CORRECTLY); |
892 if (hinting_timed_out) | 893 if (hinting_timed_out) |
893 RecordEvent(EVENT_PRERENDER_SERVICE_HINTING_TIMED_OUT); | 894 RecordEvent(EVENT_PRERENDER_SERVICE_HINTING_TIMED_OUT); |
894 if (hinting_url_lookup_timed_out) | 895 if (hinting_url_lookup_timed_out) |
895 RecordEvent(EVENT_PRERENDER_SERVICE_HINTING_URL_LOOKUP_TIMED_OUT); | 896 RecordEvent(EVENT_PRERENDER_SERVICE_HINTING_URL_LOOKUP_TIMED_OUT); |
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1363 break; | 1364 break; |
1364 case content::SessionStorageNamespace::MERGE_RESULT_MERGEABLE: | 1365 case content::SessionStorageNamespace::MERGE_RESULT_MERGEABLE: |
1365 RecordEvent(EVENT_NAMESPACE_MISMATCH_MERGE_RESULT_MERGEABLE); | 1366 RecordEvent(EVENT_NAMESPACE_MISMATCH_MERGE_RESULT_MERGEABLE); |
1366 break; | 1367 break; |
1367 default: | 1368 default: |
1368 NOTREACHED(); | 1369 NOTREACHED(); |
1369 } | 1370 } |
1370 } | 1371 } |
1371 | 1372 |
1372 } // namespace prerender | 1373 } // namespace prerender |
OLD | NEW |