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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/predictors/autocomplete_action_predictor.h" | 9 #include "chrome/browser/predictors/autocomplete_action_predictor.h" |
10 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.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" | 11 #include "chrome/browser/profiles/profile.h" |
15 #include "content/public/browser/web_ui.h" | 12 #include "content/public/browser/web_ui.h" |
16 #include "content/public/common/resource_type.h" | |
17 | 13 |
18 using predictors::AutocompleteActionPredictor; | 14 using predictors::AutocompleteActionPredictor; |
19 using predictors::ResourcePrefetchPredictor; | |
20 using predictors::ResourcePrefetchPredictorTables; | |
21 | |
22 namespace { | |
23 | |
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"; | |
34 } | |
35 } | |
36 | |
37 } // namespace | |
38 | 15 |
39 PredictorsHandler::PredictorsHandler(Profile* profile) { | 16 PredictorsHandler::PredictorsHandler(Profile* profile) { |
40 autocomplete_action_predictor_ = | 17 autocomplete_action_predictor_ = |
41 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile); | 18 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile); |
42 resource_prefetch_predictor_ = | |
43 predictors::ResourcePrefetchPredictorFactory::GetForProfile(profile); | |
44 } | 19 } |
45 | 20 |
46 PredictorsHandler::~PredictorsHandler() { } | 21 PredictorsHandler::~PredictorsHandler() { } |
47 | 22 |
48 void PredictorsHandler::RegisterMessages() { | 23 void PredictorsHandler::RegisterMessages() { |
49 web_ui()->RegisterMessageCallback("requestAutocompleteActionPredictorDb", | 24 web_ui()->RegisterMessageCallback("requestAutocompleteActionPredictorDb", |
50 base::Bind(&PredictorsHandler::RequestAutocompleteActionPredictorDb, | 25 base::Bind(&PredictorsHandler::RequestAutocompleteActionPredictorDb, |
51 base::Unretained(this))); | 26 base::Unretained(this))); |
52 web_ui()->RegisterMessageCallback("requestResourcePrefetchPredictorDb", | |
53 base::Bind(&PredictorsHandler::RequestResourcePrefetchPredictorDb, | |
54 base::Unretained(this))); | |
55 } | 27 } |
56 | 28 |
57 void PredictorsHandler::RequestAutocompleteActionPredictorDb( | 29 void PredictorsHandler::RequestAutocompleteActionPredictorDb( |
58 const base::ListValue* args) { | 30 const base::ListValue* args) { |
59 const bool enabled = (autocomplete_action_predictor_ != NULL); | 31 const bool enabled = (autocomplete_action_predictor_ != NULL); |
60 base::DictionaryValue dict; | 32 base::DictionaryValue dict; |
61 dict.SetBoolean("enabled", enabled); | 33 dict.SetBoolean("enabled", enabled); |
62 if (enabled) { | 34 if (enabled) { |
63 base::ListValue* db = new base::ListValue(); | 35 base::ListValue* db = new base::ListValue(); |
64 for (AutocompleteActionPredictor::DBCacheMap::const_iterator it = | 36 for (AutocompleteActionPredictor::DBCacheMap::const_iterator it = |
65 autocomplete_action_predictor_->db_cache_.begin(); | 37 autocomplete_action_predictor_->db_cache_.begin(); |
66 it != autocomplete_action_predictor_->db_cache_.end(); | 38 it != autocomplete_action_predictor_->db_cache_.end(); |
67 ++it) { | 39 ++it) { |
68 base::DictionaryValue* entry = new base::DictionaryValue(); | 40 base::DictionaryValue* entry = new base::DictionaryValue(); |
69 entry->SetString("user_text", it->first.user_text); | 41 entry->SetString("user_text", it->first.user_text); |
70 entry->SetString("url", it->first.url.spec()); | 42 entry->SetString("url", it->first.url.spec()); |
71 entry->SetInteger("hit_count", it->second.number_of_hits); | 43 entry->SetInteger("hit_count", it->second.number_of_hits); |
72 entry->SetInteger("miss_count", it->second.number_of_misses); | 44 entry->SetInteger("miss_count", it->second.number_of_misses); |
73 entry->SetDouble("confidence", | 45 entry->SetDouble("confidence", |
74 autocomplete_action_predictor_->CalculateConfidenceForDbEntry(it)); | 46 autocomplete_action_predictor_->CalculateConfidenceForDbEntry(it)); |
75 db->Append(entry); | 47 db->Append(entry); |
76 } | 48 } |
77 dict.Set("db", db); | 49 dict.Set("db", db); |
78 } | 50 } |
79 | 51 |
80 web_ui()->CallJavascriptFunction("updateAutocompleteActionPredictorDb", dict); | 52 web_ui()->CallJavascriptFunction("updateAutocompleteActionPredictorDb", dict); |
81 } | 53 } |
82 | |
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); | |
88 | |
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); | |
95 | |
96 db = new base::ListValue(); | |
97 AddPrefetchDataMapToListValue( | |
98 *resource_prefetch_predictor_->host_table_cache_, db); | |
99 dict.Set("host_db", db); | |
100 } | |
101 | |
102 web_ui()->CallJavascriptFunction("updateResourcePrefetchPredictorDb", dict); | |
103 } | |
104 | |
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); | |
126 } | |
127 main->Set("resources", resources); | |
128 db->Append(main); | |
129 } | |
130 } | |
OLD | NEW |