Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/options_ui.h" | 5 #include "chrome/browser/ui/webui/options/options_ui.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 #include "content/public/browser/render_frame_host.h" | 61 #include "content/public/browser/render_frame_host.h" |
| 62 #include "content/public/browser/url_data_source.h" | 62 #include "content/public/browser/url_data_source.h" |
| 63 #include "content/public/browser/web_contents.h" | 63 #include "content/public/browser/web_contents.h" |
| 64 #include "content/public/browser/web_contents_delegate.h" | 64 #include "content/public/browser/web_contents_delegate.h" |
| 65 #include "content/public/browser/web_ui.h" | 65 #include "content/public/browser/web_ui.h" |
| 66 #include "grit/options_resources.h" | 66 #include "grit/options_resources.h" |
| 67 #include "grit/theme_resources.h" | 67 #include "grit/theme_resources.h" |
| 68 #include "net/base/escape.h" | 68 #include "net/base/escape.h" |
| 69 #include "ui/base/l10n/l10n_util.h" | 69 #include "ui/base/l10n/l10n_util.h" |
| 70 #include "ui/base/resource/resource_bundle.h" | 70 #include "ui/base/resource/resource_bundle.h" |
| 71 #include "ui/base/template_expressions.h" | |
| 71 #include "ui/base/webui/jstemplate_builder.h" | 72 #include "ui/base/webui/jstemplate_builder.h" |
| 72 #include "ui/base/webui/web_ui_util.h" | 73 #include "ui/base/webui/web_ui_util.h" |
| 73 #include "url/gurl.h" | 74 #include "url/gurl.h" |
| 74 | 75 |
| 75 #if defined(ENABLE_SUPERVISED_USERS) | 76 #if defined(ENABLE_SUPERVISED_USERS) |
| 76 #include "chrome/browser/ui/webui/options/supervised_user_create_confirm_handler .h" | 77 #include "chrome/browser/ui/webui/options/supervised_user_create_confirm_handler .h" |
| 77 #include "chrome/browser/ui/webui/options/supervised_user_import_handler.h" | 78 #include "chrome/browser/ui/webui/options/supervised_user_import_handler.h" |
| 78 #include "chrome/browser/ui/webui/options/supervised_user_learn_more_handler.h" | 79 #include "chrome/browser/ui/webui/options/supervised_user_learn_more_handler.h" |
| 79 #endif | 80 #endif |
| 80 | 81 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 109 #include "chrome/browser/ui/webui/options/geolocation_options_handler.h" | 110 #include "chrome/browser/ui/webui/options/geolocation_options_handler.h" |
| 110 #endif | 111 #endif |
| 111 | 112 |
| 112 using content::RenderViewHost; | 113 using content::RenderViewHost; |
| 113 | 114 |
| 114 namespace { | 115 namespace { |
| 115 | 116 |
| 116 const char kLocalizedStringsFile[] = "strings.js"; | 117 const char kLocalizedStringsFile[] = "strings.js"; |
| 117 const char kOptionsBundleJsFile[] = "options_bundle.js"; | 118 const char kOptionsBundleJsFile[] = "options_bundle.js"; |
| 118 | 119 |
| 120 #if defined(OS_CHROMEOS) | |
| 121 constexpr char kPinKeyboardHTMLPath[] = "people_page/pin_keyboard.html"; | |
| 122 constexpr char kPinKeyboardJSPath[] = "people_page/pin_keyboard.js"; | |
| 123 constexpr char kPasswordPromptDialogHTMLPath[] = | |
| 124 "people_page/password_prompt_dialog.html"; | |
| 125 constexpr char kPasswordPromptDialogJSPath[] = | |
| 126 "people_page/password_prompt_dialog.js"; | |
| 127 constexpr char kLockStateBehaviorHTMLPath[] = | |
| 128 "people_page/lock_state_behavior.html"; | |
| 129 constexpr char kLockStateBehaviorJSPath[] = | |
| 130 "people_page/lock_state_behavior.js"; | |
| 131 constexpr char kLockScreenHTMLPath[] = "people_page/lock_screen.html"; | |
| 132 constexpr char kLockScreenJSPath[] = "people_page/lock_screen.js"; | |
| 133 constexpr char kSetupPinHTMLPath[] = "people_page/setup_pin_dialog.html"; | |
| 134 constexpr char kSetupPinJSPath[] = "people_page/setup_pin_dialog.js"; | |
| 135 constexpr char kSettingsRouteHTMLPath[] = "route.html"; | |
| 136 constexpr char kSettingsRouteJSPath[] = "route_stub.js"; | |
| 137 constexpr char kSettingsSharedCSSHTMLPath[] = "settings_shared_css.html"; | |
| 138 constexpr char kSettingsVarsCSSHTMLPath[] = "settings_vars_css.html"; | |
| 139 constexpr char kSettingsPrefsBehaviorHTMLPath[] = "prefs/prefs_behavior.html"; | |
| 140 constexpr char kSettingsPrefsBehaviorJSPath[] = "prefs/prefs_behavior.js"; | |
| 141 constexpr char kSettingsPrefsTypesHTMLPath[] = "prefs/prefs_types.html"; | |
| 142 constexpr char kSettingsPrefsTypesJSPath[] = "prefs/prefs_types.js"; | |
| 143 constexpr char kOptionsPolymerHTMLPath[] = "options_polymer.html"; | |
| 144 #endif | |
| 145 | |
| 119 } // namespace | 146 } // namespace |
| 120 | 147 |
| 121 namespace options { | 148 namespace options { |
| 122 | 149 |
| 123 //////////////////////////////////////////////////////////////////////////////// | 150 //////////////////////////////////////////////////////////////////////////////// |
| 124 // | 151 // |
| 125 // OptionsUIHTMLSource | 152 // OptionsUIHTMLSource |
| 126 // | 153 // |
| 127 //////////////////////////////////////////////////////////////////////////////// | 154 //////////////////////////////////////////////////////////////////////////////// |
| 128 | 155 |
| 129 class OptionsUIHTMLSource : public content::URLDataSource { | 156 class OptionsUIHTMLSource : public content::URLDataSource { |
| 130 public: | 157 public: |
| 131 // The constructor takes over ownership of |localized_strings|. | 158 // The constructor takes over ownership of |localized_strings|. |
| 132 explicit OptionsUIHTMLSource(base::DictionaryValue* localized_strings); | 159 explicit OptionsUIHTMLSource(base::DictionaryValue* localized_strings); |
| 133 | 160 |
| 134 // content::URLDataSource implementation. | 161 // content::URLDataSource implementation. |
| 135 std::string GetSource() const override; | 162 std::string GetSource() const override; |
| 136 void StartDataRequest( | 163 void StartDataRequest( |
| 137 const std::string& path, | 164 const std::string& path, |
| 138 int render_process_id, | 165 int render_process_id, |
| 139 int render_frame_id, | 166 int render_frame_id, |
| 140 const content::URLDataSource::GotDataCallback& callback) override; | 167 const content::URLDataSource::GotDataCallback& callback) override; |
| 141 std::string GetMimeType(const std::string&) const override; | 168 std::string GetMimeType(const std::string&) const override; |
| 142 bool ShouldDenyXFrameOptions() const override; | 169 bool ShouldDenyXFrameOptions() const override; |
| 143 | 170 |
| 144 private: | 171 private: |
| 145 ~OptionsUIHTMLSource() override; | 172 ~OptionsUIHTMLSource() override; |
| 173 void CreateDataSourceMap(); | |
| 174 void AddReplacements(base::DictionaryValue* localized_strings); | |
| 146 | 175 |
| 147 // Localized strings collection. | 176 // Localized strings collection. |
| 148 std::unique_ptr<base::DictionaryValue> localized_strings_; | 177 std::unique_ptr<base::DictionaryValue> localized_strings_; |
| 178 std::map<std::string, int> path_to_idr_map_; | |
| 179 ui::TemplateReplacements replacements_; | |
| 149 | 180 |
| 150 DISALLOW_COPY_AND_ASSIGN(OptionsUIHTMLSource); | 181 DISALLOW_COPY_AND_ASSIGN(OptionsUIHTMLSource); |
| 151 }; | 182 }; |
| 152 | 183 |
| 153 OptionsUIHTMLSource::OptionsUIHTMLSource( | 184 OptionsUIHTMLSource::OptionsUIHTMLSource( |
| 154 base::DictionaryValue* localized_strings) { | 185 base::DictionaryValue* localized_strings) { |
| 155 DCHECK(localized_strings); | 186 DCHECK(localized_strings); |
| 187 AddReplacements(localized_strings); | |
| 156 localized_strings_.reset(localized_strings); | 188 localized_strings_.reset(localized_strings); |
| 189 CreateDataSourceMap(); | |
| 157 } | 190 } |
| 158 | 191 |
| 159 std::string OptionsUIHTMLSource::GetSource() const { | 192 std::string OptionsUIHTMLSource::GetSource() const { |
| 160 return chrome::kChromeUISettingsFrameHost; | 193 return chrome::kChromeUISettingsFrameHost; |
| 161 } | 194 } |
| 162 | 195 |
| 163 void OptionsUIHTMLSource::StartDataRequest( | 196 void OptionsUIHTMLSource::StartDataRequest( |
| 164 const std::string& path, | 197 const std::string& path, |
| 165 int render_process_id, | 198 int render_process_id, |
| 166 int render_frame_id, | 199 int render_frame_id, |
| 167 const content::URLDataSource::GotDataCallback& callback) { | 200 const content::URLDataSource::GotDataCallback& callback) { |
| 168 scoped_refptr<base::RefCountedMemory> response_bytes; | 201 scoped_refptr<base::RefCountedMemory> response_bytes; |
| 169 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | 202 const std::string& app_locale = g_browser_process->GetApplicationLocale(); |
| 170 webui::SetLoadTimeDataDefaults(app_locale, localized_strings_.get()); | 203 webui::SetLoadTimeDataDefaults(app_locale, localized_strings_.get()); |
| 171 | 204 |
| 205 std::map<std::string, int>::iterator result; | |
| 206 result = path_to_idr_map_.find(path); | |
| 207 | |
| 172 if (path == kLocalizedStringsFile) { | 208 if (path == kLocalizedStringsFile) { |
| 173 // Return dynamically-generated strings from memory. | 209 // Return dynamically-generated strings from memory. |
| 174 std::string strings_js; | 210 std::string strings_js; |
| 175 webui::AppendJsonJS(localized_strings_.get(), &strings_js); | 211 webui::AppendJsonJS(localized_strings_.get(), &strings_js); |
| 176 response_bytes = base::RefCountedString::TakeString(&strings_js); | 212 response_bytes = base::RefCountedString::TakeString(&strings_js); |
| 177 } else if (path == kOptionsBundleJsFile) { | 213 } else if (path == kOptionsBundleJsFile) { |
| 178 // Return (and cache) the options javascript code. | 214 // Return (and cache) the options javascript code. |
| 179 response_bytes = ui::ResourceBundle::GetSharedInstance(). | 215 response_bytes = ui::ResourceBundle::GetSharedInstance(). |
| 180 LoadDataResourceBytes(IDR_OPTIONS_BUNDLE_JS); | 216 LoadDataResourceBytes(IDR_OPTIONS_BUNDLE_JS); |
| 217 } else if (result != path_to_idr_map_.end()) { | |
| 218 response_bytes = | |
| 219 ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | |
| 220 result->second); | |
| 181 } else { | 221 } else { |
| 182 // Return (and cache) the main options html page as the default. | 222 // Return (and cache) the main options html page as the default. |
| 183 response_bytes = ui::ResourceBundle::GetSharedInstance(). | 223 response_bytes = ui::ResourceBundle::GetSharedInstance(). |
| 184 LoadDataResourceBytes(IDR_OPTIONS_HTML); | 224 LoadDataResourceBytes(IDR_OPTIONS_HTML); |
| 185 } | 225 } |
| 186 | 226 |
| 227 // pre-process i18n strings | |
| 228 if (GetMimeType(path) == "text/html") { | |
| 229 std::string replaced = ui::ReplaceTemplateExpressions( | |
| 230 base::StringPiece(response_bytes->front_as<char>(), | |
| 231 response_bytes->size()), | |
| 232 replacements_); | |
| 233 response_bytes = base::RefCountedString::TakeString(&replaced); | |
| 234 } | |
| 235 | |
| 187 callback.Run(response_bytes.get()); | 236 callback.Run(response_bytes.get()); |
| 188 } | 237 } |
| 189 | 238 |
| 190 std::string OptionsUIHTMLSource::GetMimeType(const std::string& path) const { | 239 std::string OptionsUIHTMLSource::GetMimeType(const std::string& path) const { |
| 191 if (path == kLocalizedStringsFile || path == kOptionsBundleJsFile) | 240 if (path == kLocalizedStringsFile || path == kOptionsBundleJsFile) |
|
xiyuan
2016/08/17 21:30:35
nit: We can get rid of this since the added "if" b
xiaoyinh(OOO Sep 11-29)
2016/08/18 22:41:12
Done.
| |
| 192 return "application/javascript"; | 241 return "application/javascript"; |
| 193 | 242 |
| 243 if (base::EndsWith(path, ".js", base::CompareCase::INSENSITIVE_ASCII)) | |
| 244 return "application/javascript"; | |
| 245 | |
| 194 return "text/html"; | 246 return "text/html"; |
| 195 } | 247 } |
| 196 | 248 |
| 197 bool OptionsUIHTMLSource::ShouldDenyXFrameOptions() const { | 249 bool OptionsUIHTMLSource::ShouldDenyXFrameOptions() const { |
| 198 return false; | 250 return false; |
| 199 } | 251 } |
| 200 | 252 |
| 201 OptionsUIHTMLSource::~OptionsUIHTMLSource() {} | 253 OptionsUIHTMLSource::~OptionsUIHTMLSource() {} |
| 202 | 254 |
| 255 void OptionsUIHTMLSource::CreateDataSourceMap() { | |
| 256 #if defined(OS_CHROMEOS) | |
| 257 path_to_idr_map_[kPinKeyboardHTMLPath] = IDR_OPTIONS_PIN_KEYBOARD_HTML; | |
| 258 path_to_idr_map_[kPinKeyboardJSPath] = IDR_OPTIONS_PIN_KEYBOARD_JS; | |
| 259 path_to_idr_map_[kPasswordPromptDialogHTMLPath] = | |
| 260 IDR_OPTIONS_PASSWORD_PROMPT_DIALOG_HTML; | |
| 261 path_to_idr_map_[kPasswordPromptDialogJSPath] = | |
| 262 IDR_OPTIONS_PASSWORD_PROMPT_DIALOG_JS; | |
| 263 path_to_idr_map_[kLockStateBehaviorHTMLPath] = | |
| 264 IDR_OPTIONS_LOCK_STATE_BEHAVIOR_HTML; | |
| 265 path_to_idr_map_[kLockStateBehaviorJSPath] = | |
| 266 IDR_OPTIONS_LOCK_STATE_BEHAVIOR_JS; | |
| 267 path_to_idr_map_[kLockScreenHTMLPath] = IDR_OPTIONS_LOCK_SCREEN_HTML; | |
| 268 path_to_idr_map_[kLockScreenJSPath] = IDR_OPTIONS_LOCK_SCREEN_JS; | |
| 269 path_to_idr_map_[kSetupPinHTMLPath] = IDR_OPTIONS_SETUP_PIN_DIALOG_HTML; | |
| 270 path_to_idr_map_[kSetupPinJSPath] = IDR_OPTIONS_SETUP_PIN_DIALOG_JS; | |
| 271 path_to_idr_map_[kSettingsRouteHTMLPath] = IDR_OPTIONS_ROUTE_HTML; | |
| 272 path_to_idr_map_[kSettingsRouteJSPath] = IDR_OPTIONS_ROUTE_JS; | |
| 273 path_to_idr_map_[kSettingsSharedCSSHTMLPath] = IDR_SETTINGS_SHARED_CSS_HTML; | |
| 274 path_to_idr_map_[kSettingsVarsCSSHTMLPath] = IDR_SETTINGS_VARS_CSS_HTML; | |
| 275 path_to_idr_map_[kSettingsPrefsBehaviorHTMLPath] = | |
| 276 IDR_SETTINGS_PREFS_BEHAVIOR_HTML; | |
| 277 path_to_idr_map_[kSettingsPrefsBehaviorJSPath] = | |
| 278 IDR_SETTINGS_PREFS_BEHAVIOR_JS; | |
| 279 path_to_idr_map_[kSettingsPrefsTypesHTMLPath] = IDR_SETTINGS_PREFS_TYPES_HTML; | |
| 280 path_to_idr_map_[kSettingsPrefsTypesJSPath] = IDR_SETTINGS_PREFS_TYPES_JS; | |
| 281 path_to_idr_map_[kOptionsPolymerHTMLPath] = IDR_OPTIONS_POLYMER_ELEMENTS_HTML; | |
| 282 #endif | |
| 283 } | |
| 284 | |
| 285 void OptionsUIHTMLSource::AddReplacements( | |
| 286 base::DictionaryValue* localized_strings) { | |
| 287 for (auto it = base::DictionaryValue::Iterator(*localized_strings); | |
| 288 !it.IsAtEnd(); it.Advance()) { | |
| 289 std::string str_value; | |
| 290 it.value().DeepCopy()->GetAsString(&str_value); | |
|
xiyuan
2016/08/17 21:30:35
A couple of problems:
1. DeepCopy() is wrong and i
xiaoyinh(OOO Sep 11-29)
2016/08/18 22:41:13
Oops :(, thanks for catching that!
| |
| 291 replacements_[it.key()] = str_value; | |
| 292 } | |
| 293 } | |
| 294 | |
| 203 //////////////////////////////////////////////////////////////////////////////// | 295 //////////////////////////////////////////////////////////////////////////////// |
| 204 // | 296 // |
| 205 // OptionsPageUIHandler | 297 // OptionsPageUIHandler |
| 206 // | 298 // |
| 207 //////////////////////////////////////////////////////////////////////////////// | 299 //////////////////////////////////////////////////////////////////////////////// |
| 208 | 300 |
| 209 OptionsPageUIHandler::OptionsPageUIHandler() { | 301 OptionsPageUIHandler::OptionsPageUIHandler() { |
| 210 } | 302 } |
| 211 | 303 |
| 212 OptionsPageUIHandler::~OptionsPageUIHandler() { | 304 OptionsPageUIHandler::~OptionsPageUIHandler() { |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 // Add only if handler's service is enabled. | 570 // Add only if handler's service is enabled. |
| 479 if (handler->IsEnabled()) { | 571 if (handler->IsEnabled()) { |
| 480 // Add handler to the list and also pass the ownership. | 572 // Add handler to the list and also pass the ownership. |
| 481 web_ui()->AddMessageHandler(handler.release()); | 573 web_ui()->AddMessageHandler(handler.release()); |
| 482 handler_raw->GetLocalizedValues(localized_strings); | 574 handler_raw->GetLocalizedValues(localized_strings); |
| 483 handlers_.push_back(handler_raw); | 575 handlers_.push_back(handler_raw); |
| 484 } | 576 } |
| 485 } | 577 } |
| 486 | 578 |
| 487 } // namespace options | 579 } // namespace options |
| OLD | NEW |