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.
4
5#include "chrome/browser/ui/webui/chromeos/proxy_settings_ui.h"
6
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"
25
26using content::WebContents;
27using content::WebUIMessageHandler;
28
29namespace {
30
31class ProxySettingsHTMLSource : public content::URLDataSource {
32 public:
33  explicit ProxySettingsHTMLSource(base::DictionaryValue* localized_strings);
34
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,
40      int render_frame_id,
41      const content::URLDataSource::GotDataCallback& callback) OVERRIDE;
42  virtual std::string GetMimeType(const std::string&) const OVERRIDE {
43    return "text/html";
44  }
45  virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE {
46    return false;
47  }
48
49 protected:
50  virtual ~ProxySettingsHTMLSource() {}
51
52 private:
53  scoped_ptr<base::DictionaryValue> localized_strings_;
54
55  DISALLOW_COPY_AND_ASSIGN(ProxySettingsHTMLSource);
56};
57
58ProxySettingsHTMLSource::ProxySettingsHTMLSource(
59    base::DictionaryValue* localized_strings)
60    : localized_strings_(localized_strings) {
61}
62
63std::string ProxySettingsHTMLSource::GetSource() const {
64  return chrome::kChromeUIProxySettingsHost;
65}
66
67void ProxySettingsHTMLSource::StartDataRequest(
68    const std::string& path,
69    int render_process_id,
70    int render_frame_id,
71    const content::URLDataSource::GotDataCallback& callback) {
72  webui::SetFontAndTextDirection(localized_strings_.get());
73
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());
79
80  callback.Run(base::RefCountedString::TakeString(&full_html));
81}
82
83}  // namespace
84
85namespace chromeos {
86
87ProxySettingsUI::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();
94
95  web_ui->AddMessageHandler(core_handler_);
96  core_handler_->set_handlers_host(this);
97  core_handler_->GetLocalizedValues(localized_strings);
98
99  web_ui->AddMessageHandler(proxy_handler_);
100  proxy_handler_->GetLocalizedValues(localized_strings);
101
102  bool keyboard_driven_oobe =
103      system::InputDeviceSettings::Get()->ForceKeyboardDrivenUINavigation();
104  localized_strings->SetString("highlightStrength",
105                               keyboard_driven_oobe ? "strong" : "normal");
106
107  ProxySettingsHTMLSource* source =
108      new ProxySettingsHTMLSource(localized_strings);
109  Profile* profile = Profile::FromWebUI(web_ui);
110  content::URLDataSource::Add(profile, source);
111}
112
113ProxySettingsUI::~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();
118}
119
120void 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;
128  }
129  core_handler_->InitializePage();
130  proxy_handler_->InitializePage();
131}
132
133}  // namespace chromeos
134