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 "content/public/common/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(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 |
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 if (enabled) { | 62 if (enabled) { |
35 base::ListValue* db = new base::ListValue(); | 63 base::ListValue* db = new base::ListValue(); |
36 for (AutocompleteActionPredictor::DBCacheMap::const_iterator it = | 64 for (AutocompleteActionPredictor::DBCacheMap::const_iterator it = |
37 autocomplete_action_predictor_->db_cache_.begin(); | 65 autocomplete_action_predictor_->db_cache_.begin(); |
38 it != autocomplete_action_predictor_->db_cache_.end(); | 66 it != autocomplete_action_predictor_->db_cache_.end(); |
39 ++it) { | 67 ++it) { |
40 base::DictionaryValue* entry = new base::DictionaryValue(); | 68 base::DictionaryValue* entry = new base::DictionaryValue(); |
41 entry->SetString("user_text", it->first.user_text); | 69 entry->SetString("user_text", it->first.user_text); |
42 entry->SetString("url", it->first.url.spec()); | 70 entry->SetString("url", it->first.url.spec()); |
43 entry->SetInteger("hit_count", it->second.number_of_hits); | 71 entry->SetInteger("hit_count", it->second.number_of_hits); |
44 entry->SetInteger("miss_count", it->second.number_of_misses); | 72 entry->SetInteger("miss_count", it->second.number_of_misses); |
45 entry->SetDouble("confidence", | 73 entry->SetDouble("confidence", |
46 autocomplete_action_predictor_->CalculateConfidenceForDbEntry(it)); | 74 autocomplete_action_predictor_->CalculateConfidenceForDbEntry(it)); |
47 db->Append(entry); | 75 db->Append(entry); |
48 } | 76 } |
49 dict.Set("db", db); | 77 dict.Set("db", db); |
50 } | 78 } |
51 | 79 |
52 web_ui()->CallJavascriptFunction("updateAutocompleteActionPredictorDb", dict); | 80 web_ui()->CallJavascriptFunction("updateAutocompleteActionPredictorDb", dict); |
53 } | 81 } |
| 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 |