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/browser_process.h"
11 #include "chrome/browser/chromeos/system/input_device_settings.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/webui/options/chromeos/core_chromeos_options_handler.h"
14 #include "chrome/browser/ui/webui/options/chromeos/proxy_handler.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/url_constants.h"
17 #include "chromeos/chromeos_constants.h"
18 #include "content/public/browser/url_data_source.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/browser/web_ui.h"
21 #include "content/public/browser/web_ui_message_handler.h"
22 #include "grit/browser_resources.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/base/webui/jstemplate_builder.h"
25 #include "ui/base/webui/web_ui_util.h"
27 using content::WebContents
;
28 using content::WebUIMessageHandler
;
32 class ProxySettingsHTMLSource
: public content::URLDataSource
{
34 explicit ProxySettingsHTMLSource(base::DictionaryValue
* localized_strings
);
36 // content::URLDataSource implementation.
37 std::string
GetSource() const override
;
38 void StartDataRequest(
39 const std::string
& path
,
40 int render_process_id
,
42 const content::URLDataSource::GotDataCallback
& callback
) override
;
43 std::string
GetMimeType(const std::string
&) const override
{
46 bool ShouldAddContentSecurityPolicy() const override
{ return false; }
49 ~ProxySettingsHTMLSource() override
{}
52 scoped_ptr
<base::DictionaryValue
> localized_strings_
;
54 DISALLOW_COPY_AND_ASSIGN(ProxySettingsHTMLSource
);
57 ProxySettingsHTMLSource::ProxySettingsHTMLSource(
58 base::DictionaryValue
* localized_strings
)
59 : localized_strings_(localized_strings
) {
62 std::string
ProxySettingsHTMLSource::GetSource() const {
63 return chrome::kChromeUIProxySettingsHost
;
66 void ProxySettingsHTMLSource::StartDataRequest(
67 const std::string
& path
,
68 int render_process_id
,
70 const content::URLDataSource::GotDataCallback
& callback
) {
71 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
72 webui::SetLoadTimeDataDefaults(app_locale
, 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 : ui::WebDialogUI(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 web_ui
->AddMessageHandler(core_handler_
);
96 core_handler_
->set_handlers_host(this);
97 core_handler_
->GetLocalizedValues(localized_strings
);
99 web_ui
->AddMessageHandler(proxy_handler_
);
100 proxy_handler_
->GetLocalizedValues(localized_strings
);
102 bool keyboard_driven_oobe
=
103 system::InputDeviceSettings::Get()->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