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