Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / local_state / local_state_ui.cc
blob13f4776fadbf1c99f2d5b630ed47e538327134fa
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/local_state/local_state_ui.h"
7 #include <string>
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/prefs/pref_service.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/url_constants.h"
14 #include "content/public/browser/web_ui.h"
15 #include "content/public/browser/web_ui_controller.h"
16 #include "content/public/browser/web_ui_data_source.h"
17 #include "content/public/browser/web_ui_message_handler.h"
18 #include "grit/browser_resources.h"
20 namespace {
22 // UI Handler for chrome://local-state. Displays the Local State file as JSON.
23 class LocalStateUIHandler : public content::WebUIMessageHandler {
24 public:
25 LocalStateUIHandler();
26 ~LocalStateUIHandler() override;
28 // content::WebUIMessageHandler:
29 void RegisterMessages() override;
31 private:
32 // Called from JS when the page has loaded. Serializes local state prefs and
33 // sends them to the page.
34 void HandleRequestJson(const base::ListValue* args);
36 DISALLOW_COPY_AND_ASSIGN(LocalStateUIHandler);
39 LocalStateUIHandler::LocalStateUIHandler() {
42 LocalStateUIHandler::~LocalStateUIHandler() {
45 void LocalStateUIHandler::RegisterMessages() {
46 web_ui()->RegisterMessageCallback(
47 "requestJson", base::Bind(&LocalStateUIHandler::HandleRequestJson,
48 base::Unretained(this)));
51 void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) {
52 #if !defined(OS_CHROMEOS)
53 scoped_ptr<base::DictionaryValue> local_state_values(
54 g_browser_process->local_state()->GetPreferenceValuesOmitDefaults());
56 std::string json;
57 JSONStringValueSerializer serializer(&json);
58 serializer.set_pretty_print(true);
59 bool result = serializer.Serialize(*local_state_values);
60 if (!result)
61 json = "Error loading Local State file.";
63 web_ui()->CallJavascriptFunction("localState.setLocalState",
64 base::StringValue(json));
65 #endif // !defined(OS_CHROMEOS)
68 } // namespace
70 LocalStateUI::LocalStateUI(content::WebUI* web_ui) : WebUIController(web_ui) {
71 // Set up the chrome://local-state source.
72 content::WebUIDataSource* html_source =
73 content::WebUIDataSource::Create(chrome::kChromeUILocalStateHost);
74 html_source->SetDefaultResource(IDR_LOCAL_STATE_HTML);
75 html_source->AddResourcePath("local_state.js", IDR_LOCAL_STATE_JS);
76 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source);
78 // AddMessageHandler takes ownership of LocalStateUIHandler.
79 web_ui->AddMessageHandler(new LocalStateUIHandler);
82 LocalStateUI::~LocalStateUI() {