Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / certificate_manager_dialog_ui.cc
blob9b7cbf31c60601063bbdb4fbf23667002af2a354
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/chromeos/certificate_manager_dialog_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/certificate_manager_handler.h"
14 #include "chrome/browser/ui/webui/options/chromeos/core_chromeos_options_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;
30 namespace {
32 const char kLocalizedStringsFile[] = "strings.js";
34 class CertificateManagerDialogHTMLSource : public content::URLDataSource {
35 public:
36 explicit CertificateManagerDialogHTMLSource(
37 base::DictionaryValue* localized_strings);
39 // content::URLDataSource implementation.
40 std::string GetSource() const override;
41 void StartDataRequest(
42 const std::string& path,
43 int render_process_id,
44 int render_frame_id,
45 const content::URLDataSource::GotDataCallback& callback) override;
46 std::string GetMimeType(const std::string&) const override {
47 return "text/html";
49 bool ShouldAddContentSecurityPolicy() const override { return false; }
51 protected:
52 ~CertificateManagerDialogHTMLSource() override {}
54 private:
55 scoped_ptr<base::DictionaryValue> localized_strings_;
57 DISALLOW_COPY_AND_ASSIGN(CertificateManagerDialogHTMLSource);
60 CertificateManagerDialogHTMLSource::CertificateManagerDialogHTMLSource(
61 base::DictionaryValue* localized_strings)
62 : localized_strings_(localized_strings) {
65 std::string CertificateManagerDialogHTMLSource::GetSource() const {
66 return chrome::kChromeUICertificateManagerHost;
69 void CertificateManagerDialogHTMLSource::StartDataRequest(
70 const std::string& path,
71 int render_process_id,
72 int render_frame_id,
73 const content::URLDataSource::GotDataCallback& callback) {
74 scoped_refptr<base::RefCountedMemory> response_bytes;
75 const std::string& app_locale = g_browser_process->GetApplicationLocale();
76 webui::SetLoadTimeDataDefaults(app_locale, localized_strings_.get());
78 if (path == kLocalizedStringsFile) {
79 // Return dynamically-generated strings from memory.
80 std::string strings_js;
81 webui::AppendJsonJS(localized_strings_.get(), &strings_js);
82 response_bytes = base::RefCountedString::TakeString(&strings_js);
83 } else {
84 // Return (and cache) the main options html page as the default.
85 response_bytes = ui::ResourceBundle::GetSharedInstance().
86 LoadDataResourceBytes(IDR_CERT_MANAGER_DIALOG_HTML);
89 callback.Run(response_bytes.get());
92 } // namespace
94 namespace chromeos {
96 CertificateManagerDialogUI::CertificateManagerDialogUI(content::WebUI* web_ui)
97 : ui::WebDialogUI(web_ui),
98 initialized_handlers_(false),
99 cert_handler_(new ::options::CertificateManagerHandler(true)),
100 core_handler_(new options::CoreChromeOSOptionsHandler()) {
101 // |localized_strings| will be owned by CertificateManagerDialogHTMLSource.
102 base::DictionaryValue* localized_strings = new base::DictionaryValue();
104 web_ui->AddMessageHandler(core_handler_);
105 core_handler_->set_handlers_host(this);
106 core_handler_->GetLocalizedValues(localized_strings);
108 web_ui->AddMessageHandler(cert_handler_);
109 cert_handler_->GetLocalizedValues(localized_strings);
111 bool keyboard_driven_oobe =
112 system::InputDeviceSettings::Get()->ForceKeyboardDrivenUINavigation();
113 localized_strings->SetString("highlightStrength",
114 keyboard_driven_oobe ? "strong" : "normal");
116 CertificateManagerDialogHTMLSource* source =
117 new CertificateManagerDialogHTMLSource(localized_strings);
118 Profile* profile = Profile::FromWebUI(web_ui);
119 content::URLDataSource::Add(profile, source);
122 CertificateManagerDialogUI::~CertificateManagerDialogUI() {
123 // Uninitialize all registered handlers. The base class owns them and it will
124 // eventually delete them.
125 core_handler_->Uninitialize();
126 cert_handler_->Uninitialize();
129 void CertificateManagerDialogUI::InitializeHandlers() {
130 // A new web page DOM has been brought up in an existing renderer, causing
131 // this method to be called twice. In that case, don't initialize the handlers
132 // again. Compare with options_ui.cc.
133 if (!initialized_handlers_) {
134 core_handler_->InitializeHandler();
135 cert_handler_->InitializeHandler();
136 initialized_handlers_ = true;
138 core_handler_->InitializePage();
139 cert_handler_->InitializePage();
142 } // namespace chromeos