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 } | 79 } |
52 | 80 |
53 web_ui()->CallJavascriptFunction("updateDatabaseTable", dict); | 81 web_ui()->CallJavascriptFunction("updateAutocompleteActionPredictorDb", dict); |
54 } | 82 } |
| 83 |
| 84 void PredictorsHandler::RequestResourcePrefetchPredictorDb( |
| 85 const base::ListValue* args) { |
| 86 const bool enabled = (resource_prefetch_predictor_ != NULL); |
| 87 base::DictionaryValue dict; |
| 88 dict.SetBoolean("enabled", enabled); |
| 89 |
| 90 if (enabled) { |
| 91 // Url Database cache. |
| 92 base::ListValue* db = new base::ListValue(); |
| 93 for (ResourcePrefetchPredictor::UrlTableCacheMap::const_iterator it = |
| 94 resource_prefetch_predictor_->url_table_cache_.begin(); |
| 95 it != resource_prefetch_predictor_->url_table_cache_.end(); |
| 96 ++it) { |
| 97 base::DictionaryValue* main = new base::DictionaryValue(); |
| 98 main->SetString("main_frame_url", it->first.spec()); |
| 99 base::ListValue* resources = new base::ListValue(); |
| 100 for (ResourcePrefetchPredictor::UrlTableRowVector::const_iterator row = |
| 101 it->second.rows.begin(); row != it->second.rows.end(); ++row) { |
| 102 base::DictionaryValue* resource = new base::DictionaryValue(); |
| 103 resource->SetString("resource_url", row->resource_url.spec()); |
| 104 resource->SetString("resource_type", |
| 105 ConvertResourceType(row->resource_type)); |
| 106 resource->SetInteger("number_of_hits", row->number_of_hits); |
| 107 resource->SetInteger("number_of_misses", row->number_of_misses); |
| 108 resource->SetInteger("consecutive_misses", row->consecutive_misses); |
| 109 resource->SetDouble("position", row->average_position); |
| 110 resource->SetDouble("score", row->score); |
| 111 resources->Append(resource); |
| 112 } |
| 113 main->Set("resources", resources); |
| 114 db->Append(main); |
| 115 } |
| 116 dict.Set("db", db); |
| 117 } |
| 118 |
| 119 web_ui()->CallJavascriptFunction("updateResourcePrefetchPredictorDb", dict); |
| 120 } |
OLD | NEW |