Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / predictors / predictors_handler.cc
blob5845797c43d209f02d012a0e900f19b600bef1b0
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/predictors/predictors_handler.h"
7 #include "base/bind.h"
8 #include "base/values.h"
9 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
10 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
11 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
12 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h"
13 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "content/public/browser/web_ui.h"
16 #include "content/public/common/resource_type.h"
18 using predictors::AutocompleteActionPredictor;
19 using predictors::ResourcePrefetchPredictor;
20 using predictors::ResourcePrefetchPredictorTables;
22 namespace {
24 std::string ConvertResourceType(content::ResourceType type) {
25 switch (type) {
26 case content::RESOURCE_TYPE_IMAGE:
27 return "Image";
28 case content::RESOURCE_TYPE_STYLESHEET:
29 return "Stylesheet";
30 case content::RESOURCE_TYPE_SCRIPT:
31 return "Script";
32 default:
33 return "Unknown";
37 } // namespace
39 PredictorsHandler::PredictorsHandler(Profile* profile) {
40 autocomplete_action_predictor_ =
41 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile);
42 resource_prefetch_predictor_ =
43 predictors::ResourcePrefetchPredictorFactory::GetForProfile(profile);
46 PredictorsHandler::~PredictorsHandler() { }
48 void PredictorsHandler::RegisterMessages() {
49 web_ui()->RegisterMessageCallback("requestAutocompleteActionPredictorDb",
50 base::Bind(&PredictorsHandler::RequestAutocompleteActionPredictorDb,
51 base::Unretained(this)));
52 web_ui()->RegisterMessageCallback("requestResourcePrefetchPredictorDb",
53 base::Bind(&PredictorsHandler::RequestResourcePrefetchPredictorDb,
54 base::Unretained(this)));
57 void PredictorsHandler::RequestAutocompleteActionPredictorDb(
58 const base::ListValue* args) {
59 const bool enabled = (autocomplete_action_predictor_ != NULL);
60 base::DictionaryValue dict;
61 dict.SetBoolean("enabled", enabled);
62 if (enabled) {
63 base::ListValue* db = new base::ListValue();
64 for (AutocompleteActionPredictor::DBCacheMap::const_iterator it =
65 autocomplete_action_predictor_->db_cache_.begin();
66 it != autocomplete_action_predictor_->db_cache_.end();
67 ++it) {
68 base::DictionaryValue* entry = new base::DictionaryValue();
69 entry->SetString("user_text", it->first.user_text);
70 entry->SetString("url", it->first.url.spec());
71 entry->SetInteger("hit_count", it->second.number_of_hits);
72 entry->SetInteger("miss_count", it->second.number_of_misses);
73 entry->SetDouble("confidence",
74 autocomplete_action_predictor_->CalculateConfidenceForDbEntry(it));
75 db->Append(entry);
77 dict.Set("db", db);
80 web_ui()->CallJavascriptFunction("updateAutocompleteActionPredictorDb", dict);
83 void PredictorsHandler::RequestResourcePrefetchPredictorDb(
84 const base::ListValue* args) {
85 const bool enabled = (resource_prefetch_predictor_ != NULL);
86 base::DictionaryValue dict;
87 dict.SetBoolean("enabled", enabled);
89 if (enabled) {
90 // Url Database cache.
91 base::ListValue* db = new base::ListValue();
92 AddPrefetchDataMapToListValue(
93 *resource_prefetch_predictor_->url_table_cache_, db);
94 dict.Set("url_db", db);
96 db = new base::ListValue();
97 AddPrefetchDataMapToListValue(
98 *resource_prefetch_predictor_->host_table_cache_, db);
99 dict.Set("host_db", db);
102 web_ui()->CallJavascriptFunction("updateResourcePrefetchPredictorDb", dict);
105 void PredictorsHandler::AddPrefetchDataMapToListValue(
106 const ResourcePrefetchPredictor::PrefetchDataMap& data_map,
107 base::ListValue* db) const {
108 for (ResourcePrefetchPredictor::PrefetchDataMap::const_iterator it =
109 data_map.begin(); it != data_map.end(); ++it) {
110 base::DictionaryValue* main = new base::DictionaryValue();
111 main->SetString("main_frame_url", it->first);
112 base::ListValue* resources = new base::ListValue();
113 for (ResourcePrefetchPredictor::ResourceRows::const_iterator
114 row = it->second.resources.begin();
115 row != it->second.resources.end(); ++row) {
116 base::DictionaryValue* resource = new base::DictionaryValue();
117 resource->SetString("resource_url", row->resource_url.spec());
118 resource->SetString("resource_type",
119 ConvertResourceType(row->resource_type));
120 resource->SetInteger("number_of_hits", row->number_of_hits);
121 resource->SetInteger("number_of_misses", row->number_of_misses);
122 resource->SetInteger("consecutive_misses", row->consecutive_misses);
123 resource->SetDouble("position", row->average_position);
124 resource->SetDouble("score", row->score);
125 resources->Append(resource);
127 main->Set("resources", resources);
128 db->Append(main);