| 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 <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/predictors/autocomplete_action_predictor.h" | 12 #include "chrome/browser/predictors/autocomplete_action_predictor.h" |
| 13 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h" | 13 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h" |
| 14 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 14 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 15 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | 15 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" |
| 16 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 16 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "content/public/browser/web_ui.h" | 18 #include "content/public/browser/web_ui.h" |
| 19 #include "content/public/common/resource_type.h" | 19 #include "content/public/common/resource_type.h" |
| 20 | 20 |
| 21 using predictors::AutocompleteActionPredictor; | 21 using predictors::AutocompleteActionPredictor; |
| 22 using predictors::ResourcePrefetchPredictor; | 22 using predictors::ResourcePrefetchPredictor; |
| 23 using predictors::ResourcePrefetchPredictorTables; | 23 using predictors::ResourcePrefetchPredictorTables; |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 std::string ConvertResourceType(content::ResourceType type) { | 27 using predictors::ResourceData; |
| 28 |
| 29 std::string ConvertResourceType(ResourceData::ResourceType type) { |
| 28 switch (type) { | 30 switch (type) { |
| 29 case content::RESOURCE_TYPE_IMAGE: | 31 case ResourceData::RESOURCE_TYPE_IMAGE: |
| 30 return "Image"; | 32 return "Image"; |
| 31 case content::RESOURCE_TYPE_STYLESHEET: | 33 case ResourceData::RESOURCE_TYPE_STYLESHEET: |
| 32 return "Stylesheet"; | 34 return "Stylesheet"; |
| 33 case content::RESOURCE_TYPE_SCRIPT: | 35 case ResourceData::RESOURCE_TYPE_SCRIPT: |
| 34 return "Script"; | 36 return "Script"; |
| 37 case ResourceData::RESOURCE_TYPE_FONT_RESOURCE: |
| 38 return "Font"; |
| 35 default: | 39 default: |
| 36 return "Unknown"; | 40 return "Unknown"; |
| 37 } | 41 } |
| 38 } | 42 } |
| 39 | 43 |
| 40 } // namespace | 44 } // namespace |
| 41 | 45 |
| 42 PredictorsHandler::PredictorsHandler(Profile* profile) { | 46 PredictorsHandler::PredictorsHandler(Profile* profile) { |
| 43 autocomplete_action_predictor_ = | 47 autocomplete_action_predictor_ = |
| 44 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile); | 48 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 dict.Set("host_db", db); | 107 dict.Set("host_db", db); |
| 104 } | 108 } |
| 105 | 109 |
| 106 web_ui()->CallJavascriptFunctionUnsafe("updateResourcePrefetchPredictorDb", | 110 web_ui()->CallJavascriptFunctionUnsafe("updateResourcePrefetchPredictorDb", |
| 107 dict); | 111 dict); |
| 108 } | 112 } |
| 109 | 113 |
| 110 void PredictorsHandler::AddPrefetchDataMapToListValue( | 114 void PredictorsHandler::AddPrefetchDataMapToListValue( |
| 111 const ResourcePrefetchPredictor::PrefetchDataMap& data_map, | 115 const ResourcePrefetchPredictor::PrefetchDataMap& data_map, |
| 112 base::ListValue* db) const { | 116 base::ListValue* db) const { |
| 113 for (ResourcePrefetchPredictor::PrefetchDataMap::const_iterator it = | 117 for (const auto& p : data_map) { |
| 114 data_map.begin(); it != data_map.end(); ++it) { | |
| 115 std::unique_ptr<base::DictionaryValue> main(new base::DictionaryValue()); | 118 std::unique_ptr<base::DictionaryValue> main(new base::DictionaryValue()); |
| 116 main->SetString("main_frame_url", it->first); | 119 main->SetString("main_frame_url", p.first); |
| 117 base::ListValue* resources = new base::ListValue(); | 120 base::ListValue* resources = new base::ListValue(); |
| 118 for (ResourcePrefetchPredictor::ResourceRows::const_iterator | 121 for (const predictors::ResourceData& r : p.second.resources) { |
| 119 row = it->second.resources.begin(); | |
| 120 row != it->second.resources.end(); ++row) { | |
| 121 std::unique_ptr<base::DictionaryValue> resource( | 122 std::unique_ptr<base::DictionaryValue> resource( |
| 122 new base::DictionaryValue()); | 123 new base::DictionaryValue()); |
| 123 resource->SetString("resource_url", row->resource_url.spec()); | 124 resource->SetString("resource_url", r.resource_url()); |
| 124 resource->SetString("resource_type", | 125 resource->SetString("resource_type", |
| 125 ConvertResourceType(row->resource_type)); | 126 ConvertResourceType(r.resource_type())); |
| 126 resource->SetInteger("number_of_hits", row->number_of_hits); | 127 resource->SetInteger("number_of_hits", r.number_of_hits()); |
| 127 resource->SetInteger("number_of_misses", row->number_of_misses); | 128 resource->SetInteger("number_of_misses", r.number_of_misses()); |
| 128 resource->SetInteger("consecutive_misses", row->consecutive_misses); | 129 resource->SetInteger("consecutive_misses", r.consecutive_misses()); |
| 129 resource->SetDouble("position", row->average_position); | 130 resource->SetDouble("position", r.average_position()); |
| 130 resource->SetDouble("score", row->score); | |
| 131 resources->Append(std::move(resource)); | 131 resources->Append(std::move(resource)); |
| 132 } | 132 } |
| 133 main->Set("resources", resources); | 133 main->Set("resources", resources); |
| 134 db->Append(std::move(main)); | 134 db->Append(std::move(main)); |
| 135 } | 135 } |
| 136 } | 136 } |
| OLD | NEW |