Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/local_state/local_state_ui.h" | 5 #include "chrome/browser/ui/webui/local_state/local_state_ui.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 | 43 |
| 44 LocalStateUIHandler::~LocalStateUIHandler() { | 44 LocalStateUIHandler::~LocalStateUIHandler() { |
| 45 } | 45 } |
| 46 | 46 |
| 47 void LocalStateUIHandler::RegisterMessages() { | 47 void LocalStateUIHandler::RegisterMessages() { |
| 48 web_ui()->RegisterMessageCallback( | 48 web_ui()->RegisterMessageCallback( |
| 49 "requestJson", base::Bind(&LocalStateUIHandler::HandleRequestJson, | 49 "requestJson", base::Bind(&LocalStateUIHandler::HandleRequestJson, |
| 50 base::Unretained(this))); | 50 base::Unretained(this))); |
| 51 } | 51 } |
| 52 | 52 |
| 53 #if defined(OS_CHROMEOS) | |
| 54 bool HasValidPrefix(const std::string& pref_name, | |
| 55 const std::vector<std::string> valid_prefixes) { | |
| 56 for (const std::string& prefix : valid_prefixes) { | |
| 57 if (pref_name.compare(0, prefix.size(), prefix) == 0) | |
| 58 return true; | |
| 59 } | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 void FilterPrefs(const std::vector<std::string>& valid_prefixes, | |
| 64 base::DictionaryValue* prefs) { | |
| 65 std::vector<std::string> prefs_to_remove; | |
| 66 for (base::DictionaryValue::Iterator it(*prefs); !it.IsAtEnd(); | |
| 67 it.Advance()) { | |
| 68 if (!HasValidPrefix(it.key(), valid_prefixes)) | |
| 69 prefs_to_remove.push_back(it.key()); | |
| 70 } | |
| 71 for (const std::string& pref_to_remove : prefs_to_remove) { | |
| 72 std::unique_ptr<base::Value> removed_value; | |
| 73 bool successfully_removed = prefs->Remove(pref_to_remove, &removed_value); | |
| 74 DCHECK(successfully_removed); | |
| 75 } | |
| 76 } | |
| 77 #endif // defined(OS_CHROMEOS) | |
| 78 | |
| 53 void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) { | 79 void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) { |
| 54 #if !defined(OS_CHROMEOS) | |
| 55 std::unique_ptr<base::DictionaryValue> local_state_values( | 80 std::unique_ptr<base::DictionaryValue> local_state_values( |
| 56 g_browser_process->local_state()->GetPreferenceValuesOmitDefaults()); | 81 g_browser_process->local_state()->GetPreferenceValuesOmitDefaults()); |
| 57 | 82 #if defined(OS_CHROMEOS) |
| 83 std::vector<std::string> whitelisted_prefixes = { | |
| 84 "variations", "user_experience_metrics", "uninstall_metrics"}; | |
| 85 FilterPrefs(whitelisted_prefixes, local_state_values.get()); | |
| 86 #endif // !defined(OS_CHROMEOS) | |
|
Bernhard Bauer
2016/04/25 16:17:58
Remove the exclamation mark here as well.
hamelphi
2016/04/25 21:05:14
Done.
| |
| 58 std::string json; | 87 std::string json; |
| 59 JSONStringValueSerializer serializer(&json); | 88 JSONStringValueSerializer serializer(&json); |
| 60 serializer.set_pretty_print(true); | 89 serializer.set_pretty_print(true); |
| 61 bool result = serializer.Serialize(*local_state_values); | 90 bool result = serializer.Serialize(*local_state_values); |
| 62 if (!result) | 91 if (!result) |
| 63 json = "Error loading Local State file."; | 92 json = "Error loading Local State file."; |
| 64 | 93 |
| 65 web_ui()->CallJavascriptFunction("localState.setLocalState", | 94 web_ui()->CallJavascriptFunction("localState.setLocalState", |
| 66 base::StringValue(json)); | 95 base::StringValue(json)); |
| 67 #endif // !defined(OS_CHROMEOS) | |
| 68 } | 96 } |
| 69 | 97 |
| 70 } // namespace | 98 } // namespace |
| 71 | 99 |
| 72 LocalStateUI::LocalStateUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 100 LocalStateUI::LocalStateUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| 73 // Set up the chrome://local-state source. | 101 // Set up the chrome://local-state source. |
| 74 content::WebUIDataSource* html_source = | 102 content::WebUIDataSource* html_source = |
| 75 content::WebUIDataSource::Create(chrome::kChromeUILocalStateHost); | 103 content::WebUIDataSource::Create(chrome::kChromeUILocalStateHost); |
| 76 html_source->SetDefaultResource(IDR_LOCAL_STATE_HTML); | 104 html_source->SetDefaultResource(IDR_LOCAL_STATE_HTML); |
| 77 html_source->AddResourcePath("local_state.js", IDR_LOCAL_STATE_JS); | 105 html_source->AddResourcePath("local_state.js", IDR_LOCAL_STATE_JS); |
| 78 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); | 106 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); |
| 79 | 107 |
| 80 // AddMessageHandler takes ownership of LocalStateUIHandler. | 108 // AddMessageHandler takes ownership of LocalStateUIHandler. |
| 81 web_ui->AddMessageHandler(new LocalStateUIHandler); | 109 web_ui->AddMessageHandler(new LocalStateUIHandler); |
| 82 } | 110 } |
| 83 | 111 |
| 84 LocalStateUI::~LocalStateUI() { | 112 LocalStateUI::~LocalStateUI() { |
| 85 } | 113 } |
| OLD | NEW |