1 // Copyright (c) 2011 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/scoped_ptr.h"
8 #include "base/message_loop.h"
9 #include "base/values.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/webui/chrome_url_data_manager.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/jstemplate_builder.h"
15 #include "chrome/common/url_constants.h"
16 #include "content/browser/tab_contents/tab_contents.h"
17 #include "grit/browser_resources.h"
18 #include "ui/base/resource/resource_bundle.h"
19
20 namespace {
21
22 class ProxySettingsHTMLSource : public ChromeURLDataManager::DataSource {
23 public:
24 explicit ProxySettingsHTMLSource(DictionaryValue* localized_strings);
25
26 // Called when the network layer has requested a resource underneath
27 // the path we registered.
28 virtual void StartDataRequest(const std::string& path,
29 bool is_incognito,
30 int request_id);
GetMimeType(const std::string &) const31 virtual std::string GetMimeType(const std::string&) const {
32 return "text/html";
33 }
34
35 private:
36 scoped_ptr<DictionaryValue> localized_strings_;
37
38 DISALLOW_COPY_AND_ASSIGN(ProxySettingsHTMLSource);
39 };
40
ProxySettingsHTMLSource(DictionaryValue * localized_strings)41 ProxySettingsHTMLSource::ProxySettingsHTMLSource(
42 DictionaryValue* localized_strings)
43 : DataSource(chrome::kChromeUIProxySettingsHost, MessageLoop::current()),
44 localized_strings_(localized_strings) {
45 }
46
StartDataRequest(const std::string & path,bool is_incognito,int request_id)47 void ProxySettingsHTMLSource::StartDataRequest(const std::string& path,
48 bool is_incognito,
49 int request_id) {
50 SetFontAndTextDirection(localized_strings_.get());
51
52 static const base::StringPiece html(
53 ResourceBundle::GetSharedInstance().GetRawDataResource(
54 IDR_PROXY_SETTINGS_HTML));
55 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml(
56 html, localized_strings_.get());
57
58 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
59 html_bytes->data.resize(full_html.size());
60 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
61
62 SendResponse(request_id, html_bytes);
63 }
64
65 } // namespace
66
67 namespace chromeos {
68
ProxySettingsUI(TabContents * contents)69 ProxySettingsUI::ProxySettingsUI(TabContents* contents) : WebUI(contents) {
70 // |localized_strings| will be owned by ProxySettingsHTMLSource.
71 DictionaryValue* localized_strings = new DictionaryValue();
72
73 CoreChromeOSOptionsHandler* core_handler = new CoreChromeOSOptionsHandler();
74 core_handler->set_handlers_host(this);
75 core_handler->GetLocalizedValues(localized_strings);
76 AddMessageHandler(core_handler->Attach(this));
77
78 OptionsPageUIHandler* proxy_handler = new ProxyHandler();
79 proxy_handler->GetLocalizedValues(localized_strings);
80 AddMessageHandler(proxy_handler->Attach(this));
81
82 ProxySettingsHTMLSource* source =
83 new ProxySettingsHTMLSource(localized_strings);
84 contents->profile()->GetChromeURLDataManager()->AddDataSource(source);
85 }
86
~ProxySettingsUI()87 ProxySettingsUI::~ProxySettingsUI() {
88 // Uninitialize all registered handlers. The base class owns them and it will
89 // eventually delete them. Skip over the generic handler.
90 for (std::vector<WebUIMessageHandler*>::iterator iter = handlers_.begin() + 1;
91 iter != handlers_.end();
92 ++iter) {
93 static_cast<OptionsPageUIHandler*>(*iter)->Uninitialize();
94 }
95 }
96
InitializeHandlers()97 void ProxySettingsUI::InitializeHandlers() {
98 std::vector<WebUIMessageHandler*>::iterator iter;
99 // Skip over the generic handler.
100 for (iter = handlers_.begin() + 1; iter != handlers_.end(); ++iter) {
101 (static_cast<OptionsPageUIHandler*>(*iter))->Initialize();
102 }
103 }
104
105 } // namespace chromeos
106