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/ui/webui/predictors/predictors_handler.h" | 5 #include "chrome/browser/ui/webui/predictors/predictors_handler.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/values.h" | 12 #include "base/values.h" |
14 #include "chrome/browser/predictors/autocomplete_action_predictor.h" | 13 #include "chrome/browser/predictors/autocomplete_action_predictor.h" |
15 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h" | 14 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h" |
16 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 15 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
17 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | 16 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" |
18 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 17 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
19 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
20 #include "content/public/browser/web_ui.h" | 19 #include "content/public/browser/web_ui.h" |
21 #include "content/public/common/resource_type.h" | 20 #include "content/public/common/resource_type.h" |
22 | 21 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 base::Bind(&PredictorsHandler::RequestResourcePrefetchPredictorDb, | 61 base::Bind(&PredictorsHandler::RequestResourcePrefetchPredictorDb, |
63 base::Unretained(this))); | 62 base::Unretained(this))); |
64 } | 63 } |
65 | 64 |
66 void PredictorsHandler::RequestAutocompleteActionPredictorDb( | 65 void PredictorsHandler::RequestAutocompleteActionPredictorDb( |
67 const base::ListValue* args) { | 66 const base::ListValue* args) { |
68 const bool enabled = (autocomplete_action_predictor_ != NULL); | 67 const bool enabled = (autocomplete_action_predictor_ != NULL); |
69 base::DictionaryValue dict; | 68 base::DictionaryValue dict; |
70 dict.SetBoolean("enabled", enabled); | 69 dict.SetBoolean("enabled", enabled); |
71 if (enabled) { | 70 if (enabled) { |
72 auto db = base::MakeUnique<base::ListValue>(); | 71 base::ListValue* db = new base::ListValue(); |
73 for (AutocompleteActionPredictor::DBCacheMap::const_iterator it = | 72 for (AutocompleteActionPredictor::DBCacheMap::const_iterator it = |
74 autocomplete_action_predictor_->db_cache_.begin(); | 73 autocomplete_action_predictor_->db_cache_.begin(); |
75 it != autocomplete_action_predictor_->db_cache_.end(); | 74 it != autocomplete_action_predictor_->db_cache_.end(); |
76 ++it) { | 75 ++it) { |
77 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue()); | 76 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue()); |
78 entry->SetString("user_text", it->first.user_text); | 77 entry->SetString("user_text", it->first.user_text); |
79 entry->SetString("url", it->first.url.spec()); | 78 entry->SetString("url", it->first.url.spec()); |
80 entry->SetInteger("hit_count", it->second.number_of_hits); | 79 entry->SetInteger("hit_count", it->second.number_of_hits); |
81 entry->SetInteger("miss_count", it->second.number_of_misses); | 80 entry->SetInteger("miss_count", it->second.number_of_misses); |
82 entry->SetDouble("confidence", | 81 entry->SetDouble("confidence", |
83 autocomplete_action_predictor_->CalculateConfidenceForDbEntry(it)); | 82 autocomplete_action_predictor_->CalculateConfidenceForDbEntry(it)); |
84 db->Append(std::move(entry)); | 83 db->Append(std::move(entry)); |
85 } | 84 } |
86 dict.Set("db", std::move(db)); | 85 dict.Set("db", db); |
87 } | 86 } |
88 | 87 |
89 web_ui()->CallJavascriptFunctionUnsafe("updateAutocompleteActionPredictorDb", | 88 web_ui()->CallJavascriptFunctionUnsafe("updateAutocompleteActionPredictorDb", |
90 dict); | 89 dict); |
91 } | 90 } |
92 | 91 |
93 void PredictorsHandler::RequestResourcePrefetchPredictorDb( | 92 void PredictorsHandler::RequestResourcePrefetchPredictorDb( |
94 const base::ListValue* args) { | 93 const base::ListValue* args) { |
95 const bool enabled = (resource_prefetch_predictor_ != NULL); | 94 const bool enabled = (resource_prefetch_predictor_ != NULL); |
96 base::DictionaryValue dict; | 95 base::DictionaryValue dict; |
97 dict.SetBoolean("enabled", enabled); | 96 dict.SetBoolean("enabled", enabled); |
98 | 97 |
99 if (enabled) { | 98 if (enabled) { |
100 // Url Database cache. | 99 // Url Database cache. |
101 auto db = base::MakeUnique<base::ListValue>(); | 100 base::ListValue* db = new base::ListValue(); |
102 AddPrefetchDataMapToListValue( | 101 AddPrefetchDataMapToListValue( |
103 *resource_prefetch_predictor_->url_table_cache_, db.get()); | 102 *resource_prefetch_predictor_->url_table_cache_, db); |
104 dict.Set("url_db", std::move(db)); | 103 dict.Set("url_db", db); |
105 | 104 |
106 db = base::MakeUnique<base::ListValue>(); | 105 db = new base::ListValue(); |
107 AddPrefetchDataMapToListValue( | 106 AddPrefetchDataMapToListValue( |
108 *resource_prefetch_predictor_->host_table_cache_, db.get()); | 107 *resource_prefetch_predictor_->host_table_cache_, db); |
109 dict.Set("host_db", std::move(db)); | 108 dict.Set("host_db", db); |
110 } | 109 } |
111 | 110 |
112 web_ui()->CallJavascriptFunctionUnsafe("updateResourcePrefetchPredictorDb", | 111 web_ui()->CallJavascriptFunctionUnsafe("updateResourcePrefetchPredictorDb", |
113 dict); | 112 dict); |
114 } | 113 } |
115 | 114 |
116 void PredictorsHandler::AddPrefetchDataMapToListValue( | 115 void PredictorsHandler::AddPrefetchDataMapToListValue( |
117 const ResourcePrefetchPredictor::PrefetchDataMap& data_map, | 116 const ResourcePrefetchPredictor::PrefetchDataMap& data_map, |
118 base::ListValue* db) const { | 117 base::ListValue* db) const { |
119 for (const auto& p : data_map) { | 118 for (const auto& p : data_map) { |
120 std::unique_ptr<base::DictionaryValue> main(new base::DictionaryValue()); | 119 std::unique_ptr<base::DictionaryValue> main(new base::DictionaryValue()); |
121 main->SetString("main_frame_url", p.first); | 120 main->SetString("main_frame_url", p.first); |
122 auto resources = base::MakeUnique<base::ListValue>(); | 121 base::ListValue* resources = new base::ListValue(); |
123 for (const predictors::ResourceData& r : p.second.resources()) { | 122 for (const predictors::ResourceData& r : p.second.resources()) { |
124 std::unique_ptr<base::DictionaryValue> resource( | 123 std::unique_ptr<base::DictionaryValue> resource( |
125 new base::DictionaryValue()); | 124 new base::DictionaryValue()); |
126 resource->SetString("resource_url", r.resource_url()); | 125 resource->SetString("resource_url", r.resource_url()); |
127 resource->SetString("resource_type", | 126 resource->SetString("resource_type", |
128 ConvertResourceType(r.resource_type())); | 127 ConvertResourceType(r.resource_type())); |
129 resource->SetInteger("number_of_hits", r.number_of_hits()); | 128 resource->SetInteger("number_of_hits", r.number_of_hits()); |
130 resource->SetInteger("number_of_misses", r.number_of_misses()); | 129 resource->SetInteger("number_of_misses", r.number_of_misses()); |
131 resource->SetInteger("consecutive_misses", r.consecutive_misses()); | 130 resource->SetInteger("consecutive_misses", r.consecutive_misses()); |
132 resource->SetDouble("position", r.average_position()); | 131 resource->SetDouble("position", r.average_position()); |
133 resource->SetDouble( | 132 resource->SetDouble( |
134 "score", ResourcePrefetchPredictorTables::ComputeResourceScore(r)); | 133 "score", ResourcePrefetchPredictorTables::ComputeResourceScore(r)); |
135 resource->SetBoolean( | 134 resource->SetBoolean( |
136 "is_prefetchable", | 135 "is_prefetchable", |
137 resource_prefetch_predictor_->IsResourcePrefetchable(r)); | 136 resource_prefetch_predictor_->IsResourcePrefetchable(r)); |
138 resources->Append(std::move(resource)); | 137 resources->Append(std::move(resource)); |
139 } | 138 } |
140 main->Set("resources", std::move(resources)); | 139 main->Set("resources", resources); |
141 db->Append(std::move(main)); | 140 db->Append(std::move(main)); |
142 } | 141 } |
143 } | 142 } |
OLD | NEW |