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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/chromeos/proxy_settings_ui.h"
7 #include "base/memory/ref_counted_memory.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "chrome/browser/chromeos/system/input_device_settings.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/webui/options/chromeos/core_chromeos_options_handler.h"
13 #include "chrome/browser/ui/webui/options/chromeos/proxy_handler.h"
14 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/url_constants.h"
16 #include "chromeos/chromeos_constants.h"
17 #include "content/public/browser/url_data_source.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_ui.h"
20 #include "content/public/browser/web_ui_message_handler.h"
21 #include "grit/browser_resources.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/base/webui/jstemplate_builder.h"
24 #include "ui/base/webui/web_ui_util.h"
26 using content::WebContents;
27 using content::WebUIMessageHandler;
31 class ProxySettingsHTMLSource : public content::URLDataSource {
33 explicit ProxySettingsHTMLSource(base::DictionaryValue* localized_strings);
35 // content::URLDataSource implementation.
36 virtual std::string GetSource() const OVERRIDE;
37 virtual void StartDataRequest(
38 const std::string& path,
39 int render_process_id,
41 const content::URLDataSource::GotDataCallback& callback) OVERRIDE;
42 virtual std::string GetMimeType(const std::string&) const OVERRIDE {
45 virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE {
50 virtual ~ProxySettingsHTMLSource() {}
53 scoped_ptr<base::DictionaryValue> localized_strings_;
55 DISALLOW_COPY_AND_ASSIGN(ProxySettingsHTMLSource);
58 ProxySettingsHTMLSource::ProxySettingsHTMLSource(
59 base::DictionaryValue* localized_strings)
60 : localized_strings_(localized_strings) {
63 std::string ProxySettingsHTMLSource::GetSource() const {
64 return chrome::kChromeUIProxySettingsHost;
67 void ProxySettingsHTMLSource::StartDataRequest(
68 const std::string& path,
69 int render_process_id,
71 const content::URLDataSource::GotDataCallback& callback) {
72 webui::SetFontAndTextDirection(localized_strings_.get());
74 static const base::StringPiece html(
75 ResourceBundle::GetSharedInstance().GetRawDataResource(
76 IDR_PROXY_SETTINGS_HTML));
77 std::string full_html = webui::GetI18nTemplateHtml(
78 html, localized_strings_.get());
80 callback.Run(base::RefCountedString::TakeString(&full_html));
87 ProxySettingsUI::ProxySettingsUI(content::WebUI* web_ui)
88 : WebUIController(web_ui),
89 initialized_handlers_(false),
90 proxy_handler_(new options::ProxyHandler()),
91 core_handler_(new options::CoreChromeOSOptionsHandler()) {
92 // |localized_strings| will be owned by ProxySettingsHTMLSource.
93 base::DictionaryValue* localized_strings = new base::DictionaryValue();
95 core_handler_->set_handlers_host(this);
96 core_handler_->GetLocalizedValues(localized_strings);
97 web_ui->AddMessageHandler(core_handler_);
99 proxy_handler_->GetLocalizedValues(localized_strings);
100 web_ui->AddMessageHandler(proxy_handler_);
102 bool keyboard_driven_oobe =
103 system::keyboard_settings::ForceKeyboardDrivenUINavigation();
104 localized_strings->SetString("highlightStrength",
105 keyboard_driven_oobe ? "strong" : "normal");
107 ProxySettingsHTMLSource* source =
108 new ProxySettingsHTMLSource(localized_strings);
109 Profile* profile = Profile::FromWebUI(web_ui);
110 content::URLDataSource::Add(profile, source);
113 ProxySettingsUI::~ProxySettingsUI() {
114 // Uninitialize all registered handlers. The base class owns them and it will
115 // eventually delete them.
116 core_handler_->Uninitialize();
117 proxy_handler_->Uninitialize();
120 void ProxySettingsUI::InitializeHandlers() {
121 // A new web page DOM has been brought up in an existing renderer, causing
122 // this method to be called twice. In that case, don't initialize the handlers
123 // again. Compare with options_ui.cc.
124 if (!initialized_handlers_) {
125 core_handler_->InitializeHandler();
126 proxy_handler_->InitializeHandler();
127 initialized_handlers_ = true;
129 core_handler_->InitializePage();
130 proxy_handler_->InitializePage();
133 } // namespace chromeos