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. 4 5 #include "chrome/browser/ui/webui/chromeos/certificate_manager_dialog_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/certificate_manager_handler.h" 13 #include "chrome/browser/ui/webui/options/chromeos/core_chromeos_options_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 26 using content::WebContents; 27 using content::WebUIMessageHandler; 28 29 namespace { 30 31 const char kLocalizedStringsFile[] = "strings.js"; 32 33 class CertificateManagerDialogHTMLSource : public content::URLDataSource { 34 public: 35 explicit CertificateManagerDialogHTMLSource( 36 base::DictionaryValue* localized_strings); 37 38 // content::URLDataSource implementation. 39 virtual std::string GetSource() const OVERRIDE; 40 virtual void StartDataRequest( 41 const std::string& path, 42 int render_process_id, 43 int render_frame_id, 44 const content::URLDataSource::GotDataCallback& callback) OVERRIDE; 45 virtual std::string GetMimeType(const std::string&) const OVERRIDE { 46 return "text/html"; 47 } 48 virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE { 49 return false; 50 } 51 52 protected: 53 virtual ~CertificateManagerDialogHTMLSource() {} 54 55 private: 56 scoped_ptr<base::DictionaryValue> localized_strings_; 57 58 DISALLOW_COPY_AND_ASSIGN(CertificateManagerDialogHTMLSource); 59 }; 60 61 CertificateManagerDialogHTMLSource::CertificateManagerDialogHTMLSource( 62 base::DictionaryValue* localized_strings) 63 : localized_strings_(localized_strings) { 64 } 65 66 std::string CertificateManagerDialogHTMLSource::GetSource() const { 67 return chrome::kChromeUICertificateManagerHost; 68 } 69 70 void CertificateManagerDialogHTMLSource::StartDataRequest( 71 const std::string& path, 72 int render_process_id, 73 int render_frame_id, 74 const content::URLDataSource::GotDataCallback& callback) { 75 scoped_refptr<base::RefCountedMemory> response_bytes; 76 webui::SetFontAndTextDirection(localized_strings_.get()); 77 78 if (path == kLocalizedStringsFile) { 79 // Return dynamically-generated strings from memory. 80 webui::UseVersion2 version; 81 std::string strings_js; 82 webui::AppendJsonJS(localized_strings_.get(), &strings_js); 83 response_bytes = base::RefCountedString::TakeString(&strings_js); 84 } else { 85 // Return (and cache) the main options html page as the default. 86 response_bytes = ui::ResourceBundle::GetSharedInstance(). 87 LoadDataResourceBytes(IDR_CERT_MANAGER_DIALOG_HTML); 88 } 89 90 callback.Run(response_bytes.get()); 91 } 92 93 } // namespace 94 95 namespace chromeos { 96 97 CertificateManagerDialogUI::CertificateManagerDialogUI(content::WebUI* web_ui) 98 : ui::WebDialogUI(web_ui), 99 initialized_handlers_(false), 100 cert_handler_(new ::options::CertificateManagerHandler(true)), 101 core_handler_(new options::CoreChromeOSOptionsHandler()) { 102 // |localized_strings| will be owned by CertificateManagerDialogHTMLSource. 103 base::DictionaryValue* localized_strings = new base::DictionaryValue(); 104 105 web_ui->AddMessageHandler(core_handler_); 106 core_handler_->set_handlers_host(this); 107 core_handler_->GetLocalizedValues(localized_strings); 108 109 web_ui->AddMessageHandler(cert_handler_); 110 cert_handler_->GetLocalizedValues(localized_strings); 111 112 bool keyboard_driven_oobe = 113 system::InputDeviceSettings::Get()->ForceKeyboardDrivenUINavigation(); 114 localized_strings->SetString("highlightStrength", 115 keyboard_driven_oobe ? "strong" : "normal"); 116 117 CertificateManagerDialogHTMLSource* source = 118 new CertificateManagerDialogHTMLSource(localized_strings); 119 Profile* profile = Profile::FromWebUI(web_ui); 120 content::URLDataSource::Add(profile, source); 121 } 122 123 CertificateManagerDialogUI::~CertificateManagerDialogUI() { 124 // Uninitialize all registered handlers. The base class owns them and it will 125 // eventually delete them. 126 core_handler_->Uninitialize(); 127 cert_handler_->Uninitialize(); 128 } 129 130 void CertificateManagerDialogUI::InitializeHandlers() { 131 // A new web page DOM has been brought up in an existing renderer, causing 132 // this method to be called twice. In that case, don't initialize the handlers 133 // again. Compare with options_ui.cc. 134 if (!initialized_handlers_) { 135 core_handler_->InitializeHandler(); 136 cert_handler_->InitializeHandler(); 137 initialized_handlers_ = true; 138 } 139 core_handler_->InitializePage(); 140 cert_handler_->InitializePage(); 141 } 142 143 } // namespace chromeos 144