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" | |
11 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
12 #include "content/public/browser/web_ui.h" | 15 #include "content/public/browser/web_ui.h" |
16 #include "webkit/glue/resource_type.h" | |
13 | 17 |
14 using predictors::AutocompleteActionPredictor; | 18 using predictors::AutocompleteActionPredictor; |
19 using predictors::ResourcePrefetchPredictor; | |
20 using predictors::ResourcePrefetchPredictorTables; | |
21 | |
22 namespace { | |
23 | |
24 std::string ConvertResourceType(ResourceType::Type type) { | |
25 switch (type) { | |
26 case ResourceType::IMAGE: | |
27 return "Image"; | |
28 case ResourceType::STYLESHEET: | |
29 return "Stylesheet"; | |
30 case ResourceType::SCRIPT: | |
31 return "Script"; | |
32 default: | |
33 return "Unknown"; | |
34 } | |
35 } | |
36 | |
37 } // namespace | |
15 | 38 |
16 PredictorsHandler::PredictorsHandler(Profile* profile) { | 39 PredictorsHandler::PredictorsHandler(Profile* profile) { |
17 autocomplete_action_predictor_ = | 40 autocomplete_action_predictor_ = |
18 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile); | 41 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile); |
42 resource_prefetch_predictor_ = | |
43 predictors::ResourcePrefetchPredictorFactory::GetForProfile(profile); | |
19 } | 44 } |
20 | 45 |
21 PredictorsHandler::~PredictorsHandler() { } | 46 PredictorsHandler::~PredictorsHandler() { } |
22 | 47 |
23 void PredictorsHandler::RegisterMessages() { | 48 void PredictorsHandler::RegisterMessages() { |
24 web_ui()->RegisterMessageCallback("requestAutocompleteActionPredictorDb", | 49 web_ui()->RegisterMessageCallback("requestAutocompleteActionPredictorDb", |
25 base::Bind(&PredictorsHandler::RequestAutocompleteActionPredictorDb, | 50 base::Bind(&PredictorsHandler::RequestAutocompleteActionPredictorDb, |
26 base::Unretained(this))); | 51 base::Unretained(this))); |
52 web_ui()->RegisterMessageCallback("requestResourcePrefetchPredictorDb", | |
53 base::Bind(&PredictorsHandler::RequestResourcePrefetchPredictorDb, | |
54 base::Unretained(this))); | |
27 } | 55 } |
28 | 56 |
29 void PredictorsHandler::RequestAutocompleteActionPredictorDb( | 57 void PredictorsHandler::RequestAutocompleteActionPredictorDb( |
30 const base::ListValue* args) { | 58 const base::ListValue* args) { |
31 const bool enabled = (autocomplete_action_predictor_ != NULL); | 59 const bool enabled = (autocomplete_action_predictor_ != NULL); |
32 base::DictionaryValue dict; | 60 base::DictionaryValue dict; |
33 dict.SetBoolean("enabled", enabled); | 61 dict.SetBoolean("enabled", enabled); |
34 | 62 |
35 if (enabled) { | 63 if (enabled) { |
36 base::ListValue* db = new base::ListValue(); | 64 base::ListValue* db = new base::ListValue(); |
37 for (AutocompleteActionPredictor::DBCacheMap::const_iterator it = | 65 for (AutocompleteActionPredictor::DBCacheMap::const_iterator it = |
38 autocomplete_action_predictor_->db_cache_.begin(); | 66 autocomplete_action_predictor_->db_cache_.begin(); |
39 it != autocomplete_action_predictor_->db_cache_.end(); | 67 it != autocomplete_action_predictor_->db_cache_.end(); |
40 ++it) { | 68 ++it) { |
41 base::DictionaryValue* entry = new base::DictionaryValue(); | 69 base::DictionaryValue* entry = new base::DictionaryValue(); |
42 entry->SetString("user_text", it->first.user_text); | 70 entry->SetString("user_text", it->first.user_text); |
43 entry->SetString("url", it->first.url.spec()); | 71 entry->SetString("url", it->first.url.spec()); |
44 entry->SetInteger("hit_count", it->second.number_of_hits); | 72 entry->SetInteger("hit_count", it->second.number_of_hits); |
45 entry->SetInteger("miss_count", it->second.number_of_misses); | 73 entry->SetInteger("miss_count", it->second.number_of_misses); |
46 entry->SetDouble("confidence", | 74 entry->SetDouble("confidence", |
47 autocomplete_action_predictor_->CalculateConfidenceForDbEntry(it)); | 75 autocomplete_action_predictor_->CalculateConfidenceForDbEntry(it)); |
48 db->Append(entry); | 76 db->Append(entry); |
49 } | 77 } |
50 dict.Set("db", db); | 78 dict.Set("db", db); |
51 dict.SetDouble("hit_weight", AutocompleteActionPredictor::get_hit_weight()); | 79 dict.SetDouble("hit_weight", AutocompleteActionPredictor::get_hit_weight()); |
52 } | 80 } |
53 | 81 |
54 web_ui()->CallJavascriptFunction("updateDatabaseTable", dict); | 82 web_ui()->CallJavascriptFunction("updateAutocompleteActionPredictorDb", dict); |
55 } | 83 } |
84 | |
85 void PredictorsHandler::RequestResourcePrefetchPredictorDb( | |
86 const base::ListValue* args) { | |
87 const bool enabled = (resource_prefetch_predictor_ != NULL); | |
88 base::DictionaryValue dict; | |
89 dict.SetBoolean("enabled", enabled); | |
90 | |
91 if (enabled) { | |
92 // Url Database cache. | |
93 base::ListValue* db = new base::ListValue(); | |
94 for (ResourcePrefetchPredictor::UrlTableCacheMap::const_iterator it = | |
95 resource_prefetch_predictor_->url_table_cache_.begin(); | |
96 it != resource_prefetch_predictor_->url_table_cache_.end(); | |
97 ++it) { | |
98 base::DictionaryValue* main = new base::DictionaryValue(); | |
99 main->SetString("main_frame_url", it->first.spec()); | |
100 base::ListValue* resources = new base::ListValue(); | |
101 for (int i = 0; i < static_cast<int>(it->second.rows_.size()); ++i) { | |
dominich
2012/05/21 16:16:53
use iterator loop
Shishir
2012/05/23 01:46:46
Done.
| |
102 const ResourcePrefetchPredictorTables::UrlTableRow& row = | |
103 it->second.rows_[i]; | |
104 | |
105 base::DictionaryValue* resource = new base::DictionaryValue(); | |
106 resource->SetString("resource_url", row.resource_url_.spec()); | |
107 resource->SetString("resource_type", | |
108 ConvertResourceType(row.resource_type_)); | |
109 resource->SetInteger("number_of_hits", row.number_of_hits_); | |
110 resource->SetInteger("number_of_misses", row.number_of_misses_); | |
111 resource->SetInteger("consecutive_misses", row.consecutive_misses_); | |
112 resource->SetDouble("position", row.average_position_); | |
113 resource->SetDouble("score", row.score_); | |
114 resources->Append(resource); | |
115 } | |
116 main->Set("resources", resources); | |
117 db->Append(main); | |
118 } | |
119 dict.Set("db", db); | |
120 } | |
121 | |
122 web_ui()->CallJavascriptFunction("updateResourcePrefetchPredictorDb", dict); | |
123 } | |
OLD | NEW |