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, | |
Alexei Svitkine (slow)
2016/04/22 20:35:51
These should be in the anon namespace or they can
hamelphi
2016/04/25 15:58:01
They are already under anon namespace. The whole L
Alexei Svitkine (slow)
2016/04/25 16:24:55
Ah right. I forgot that LocalStateUIHandler is in
hamelphi
2016/04/25 21:05:14
Done.
| |
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, | |
Alexei Svitkine (slow)
2016/04/22 20:35:51
Nit: const ref
hamelphi
2016/04/25 15:58:01
Done.
| |
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) { | |
Alexei Svitkine (slow)
2016/04/22 20:35:51
const ref please.
hamelphi
2016/04/25 15:58:01
Done.
| |
72 std::unique_ptr<base::Value> removed_value; | |
73 DCHECK(prefs->Remove(pref_to_remove, &removed_value)); | |
Bernhard Bauer
2016/04/25 09:16:28
In a Release build, this will be compiled out, so
hamelphi
2016/04/25 15:58:01
Oh yeah right. Thanks for catching that. Done.
| |
74 } | |
75 } | |
76 #endif // !defined(OS_CHROMEOS) | |
Bernhard Bauer
2016/04/25 09:16:28
Remove the exclamation mark.
hamelphi
2016/04/25 15:58:01
Done.
| |
77 | |
53 void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) { | 78 void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) { |
54 #if !defined(OS_CHROMEOS) | |
55 std::unique_ptr<base::DictionaryValue> local_state_values( | 79 std::unique_ptr<base::DictionaryValue> local_state_values( |
56 g_browser_process->local_state()->GetPreferenceValuesOmitDefaults()); | 80 g_browser_process->local_state()->GetPreferenceValuesOmitDefaults()); |
57 | 81 #if defined(OS_CHROMEOS) |
82 std::vector<std::string> whitelisted_prefixes = { | |
83 "variations", "user_experience_metrics", "uninstall_metrics"}; | |
84 FilterPrefs(whitelisted_prefixes, local_state_values.get()); | |
85 #endif // !defined(OS_CHROMEOS) | |
58 std::string json; | 86 std::string json; |
59 JSONStringValueSerializer serializer(&json); | 87 JSONStringValueSerializer serializer(&json); |
60 serializer.set_pretty_print(true); | 88 serializer.set_pretty_print(true); |
61 bool result = serializer.Serialize(*local_state_values); | 89 bool result = serializer.Serialize(*local_state_values); |
62 if (!result) | 90 if (!result) |
63 json = "Error loading Local State file."; | 91 json = "Error loading Local State file."; |
64 | 92 |
65 web_ui()->CallJavascriptFunction("localState.setLocalState", | 93 web_ui()->CallJavascriptFunction("localState.setLocalState", |
66 base::StringValue(json)); | 94 base::StringValue(json)); |
67 #endif // !defined(OS_CHROMEOS) | |
68 } | 95 } |
69 | 96 |
70 } // namespace | 97 } // namespace |
71 | 98 |
72 LocalStateUI::LocalStateUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 99 LocalStateUI::LocalStateUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
73 // Set up the chrome://local-state source. | 100 // Set up the chrome://local-state source. |
74 content::WebUIDataSource* html_source = | 101 content::WebUIDataSource* html_source = |
75 content::WebUIDataSource::Create(chrome::kChromeUILocalStateHost); | 102 content::WebUIDataSource::Create(chrome::kChromeUILocalStateHost); |
76 html_source->SetDefaultResource(IDR_LOCAL_STATE_HTML); | 103 html_source->SetDefaultResource(IDR_LOCAL_STATE_HTML); |
77 html_source->AddResourcePath("local_state.js", IDR_LOCAL_STATE_JS); | 104 html_source->AddResourcePath("local_state.js", IDR_LOCAL_STATE_JS); |
78 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); | 105 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); |
79 | 106 |
80 // AddMessageHandler takes ownership of LocalStateUIHandler. | 107 // AddMessageHandler takes ownership of LocalStateUIHandler. |
81 web_ui->AddMessageHandler(new LocalStateUIHandler); | 108 web_ui->AddMessageHandler(new LocalStateUIHandler); |
82 } | 109 } |
83 | 110 |
84 LocalStateUI::~LocalStateUI() { | 111 LocalStateUI::~LocalStateUI() { |
85 } | 112 } |
OLD | NEW |