Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/managed_user_passphrase_dialog_webui.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/managed_mode/managed_user_passphrase.h" | |
| 14 #include "chrome/browser/prefs/pref_service.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
| 17 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | |
| 18 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" | |
| 19 #include "chrome/common/pref_names.h" | |
| 20 #include "chrome/common/url_constants.h" | |
| 21 #include "content/public/browser/web_contents.h" | |
| 22 #include "content/public/browser/web_ui_message_handler.h" | |
| 23 #include "grit/browser_resources.h" | |
| 24 #include "grit/generated_resources.h" | |
| 25 #include "ui/gfx/size.h" | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 class ManagedUserPassphraseDialogMessageHandler | |
| 30 : public content::WebUIMessageHandler { | |
| 31 | |
| 32 public: | |
| 33 ManagedUserPassphraseDialogMessageHandler(); | |
| 34 virtual ~ManagedUserPassphraseDialogMessageHandler() {} | |
| 35 virtual void RegisterMessages() OVERRIDE; | |
| 36 | |
| 37 private: | |
| 38 void CheckPassphrase(const base::ListValue* args); | |
| 39 | |
| 40 base::WeakPtrFactory<ManagedUserPassphraseDialogMessageHandler> weak_factory_; | |
| 41 | |
| 42 DISALLOW_COPY_AND_ASSIGN(ManagedUserPassphraseDialogMessageHandler); | |
| 43 }; | |
| 44 | |
| 45 ManagedUserPassphraseDialogMessageHandler | |
| 46 ::ManagedUserPassphraseDialogMessageHandler() : weak_factory_(this) { | |
| 47 } | |
| 48 | |
| 49 void ManagedUserPassphraseDialogMessageHandler::RegisterMessages() { | |
| 50 web_ui()->RegisterMessageCallback("checkPassphrase", | |
| 51 base::Bind(&ManagedUserPassphraseDialogMessageHandler::CheckPassphrase, | |
| 52 weak_factory_.GetWeakPtr())); | |
| 53 } | |
| 54 | |
| 55 void ManagedUserPassphraseDialogMessageHandler::CheckPassphrase( | |
| 56 const base::ListValue* args) { | |
| 57 const base::Value* passphrase_arg = NULL; | |
| 58 args->Get(0, &passphrase_arg); | |
| 59 std::string passphrase; | |
| 60 passphrase_arg->GetAsString(&passphrase); | |
| 61 const PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
| 62 const PrefService::Preference* pref = | |
| 63 pref_service->FindPreference(prefs::kManagedModeLocalPassphrase); | |
| 64 DCHECK(pref); | |
|
Bernhard Bauer
2013/01/08 17:43:14
The pref checks are unnecessary again.
Oh, and yo
Adrian Kuegel
2013/01/09 12:10:05
Done.
| |
| 65 std::string stored_passphrase_hash; | |
| 66 bool success = pref->GetValue()->GetAsString(&stored_passphrase_hash); | |
| 67 DCHECK(success); | |
| 68 pref = pref_service->FindPreference(prefs::kManagedModeLocalSalt); | |
| 69 DCHECK(pref); | |
| 70 std::string salt; | |
| 71 success = pref->GetValue()->GetAsString(&salt); | |
| 72 DCHECK(success); | |
| 73 ManagedUserPassphrase passphrase_key_generator(salt); | |
| 74 std::string encoded_passphrase_hash; | |
| 75 passphrase_key_generator.GenerateHashFromPassphrase(passphrase, | |
| 76 &encoded_passphrase_hash); | |
|
Bernhard Bauer
2013/01/08 17:43:14
Align with previous parameter, please.
Adrian Kuegel
2013/01/09 12:10:05
Done.
| |
| 77 if (stored_passphrase_hash == encoded_passphrase_hash) { | |
| 78 web_ui()->CallJavascriptFunction("passphraseCorrect"); | |
| 79 } else { | |
| 80 web_ui()->CallJavascriptFunction("passphraseIncorrect"); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 | |
| 86 const int kDialogWidth = 400; | |
| 87 const int kDialogHeight = 310; | |
| 88 | |
| 89 void ManagedUserPassphraseDialogWebUI::CreateManagedUserPassphraseDialog( | |
| 90 content::WebContents* web_contents, | |
| 91 const base::Callback<void(bool)>& callback) { | |
| 92 // This is deleted automatically when the user closes the dialog. | |
| 93 new ManagedUserPassphraseDialogWebUI(web_contents, callback); | |
| 94 } | |
| 95 | |
| 96 ui::ModalType ManagedUserPassphraseDialogWebUI::GetDialogModalType() const { | |
| 97 return ui::MODAL_TYPE_WINDOW; | |
| 98 } | |
| 99 | |
| 100 string16 ManagedUserPassphraseDialogWebUI::GetDialogTitle() const { | |
| 101 return string16(); | |
| 102 } | |
| 103 | |
| 104 GURL ManagedUserPassphraseDialogWebUI::GetDialogContentURL() const { | |
| 105 return GURL(chrome::kChromeUIManagedUserPassphrasePageURL); | |
| 106 } | |
| 107 | |
| 108 // This function is called by the constrained window delegate. | |
| 109 void ManagedUserPassphraseDialogWebUI::GetWebUIMessageHandlers( | |
| 110 std::vector<content::WebUIMessageHandler*>* handlers) const { | |
| 111 DCHECK(handlers); | |
| 112 // The constrained window delegate takes care of registering the handler. | |
| 113 // The handler is also deleted automatically. | |
| 114 handlers->push_back(new ManagedUserPassphraseDialogMessageHandler()); | |
| 115 } | |
| 116 | |
| 117 void ManagedUserPassphraseDialogWebUI::GetDialogSize(gfx::Size* size) const { | |
| 118 size->SetSize(kDialogWidth, kDialogHeight); | |
| 119 } | |
| 120 | |
| 121 std::string ManagedUserPassphraseDialogWebUI::GetDialogArgs() const { | |
| 122 return std::string(); | |
| 123 } | |
| 124 | |
| 125 void ManagedUserPassphraseDialogWebUI::OnDialogClosed( | |
| 126 const std::string& json_retval) { | |
| 127 if (closing_) | |
| 128 return; | |
| 129 | |
| 130 closing_ = true; | |
| 131 callback_.Run(!json_retval.empty()); | |
| 132 } | |
| 133 | |
| 134 void ManagedUserPassphraseDialogWebUI::OnCloseContents( | |
| 135 content::WebContents* source, bool* out_close_dialog) {} | |
| 136 | |
| 137 bool ManagedUserPassphraseDialogWebUI::ShouldShowDialogTitle() const { | |
| 138 return false; | |
| 139 } | |
| 140 | |
| 141 ManagedUserPassphraseDialogWebUI::ManagedUserPassphraseDialogWebUI( | |
| 142 content::WebContents* web_contents, | |
| 143 const base::Callback<void(bool)>& callback) : callback_(callback), | |
| 144 closing_(false) { | |
| 145 Profile* profile = | |
| 146 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 147 CreateDataSource(profile); | |
| 148 CreateConstrainedWebDialog(profile, this, NULL, web_contents); | |
| 149 } | |
| 150 | |
| 151 ManagedUserPassphraseDialogWebUI::~ManagedUserPassphraseDialogWebUI() { | |
| 152 } | |
| 153 | |
| 154 void ManagedUserPassphraseDialogWebUI::CreateDataSource(Profile* profile) { | |
| 155 ChromeWebUIDataSource* data_source = | |
| 156 new ChromeWebUIDataSource(chrome::kChromeUIManagedUserPassphrasePageHost); | |
| 157 data_source->set_default_resource(IDR_MANAGED_USER_PASSPHRASE_DIALOG_HTML); | |
| 158 data_source->add_resource_path("managed_user_passphrase_dialog.js", | |
| 159 IDR_MANAGED_USER_PASSPHRASE_DIALOG_JS); | |
| 160 data_source->add_resource_path("managed_user_passphrase_dialog.css", | |
| 161 IDR_MANAGED_USER_PASSPHRASE_DIALOG_CSS); | |
| 162 data_source->add_resource_path("chrome_kid.png", | |
| 163 IDR_MANAGED_USER_PASSPHRASE_DIALOG_IMG); | |
| 164 data_source->AddLocalizedString("managedModePassphrasePage", | |
| 165 IDS_PASSPHRASE_TITLE); | |
| 166 data_source->AddLocalizedString("forgot_passphrase", IDS_FORGOT_PASSPHRASE); | |
| 167 data_source->AddLocalizedString("unlock_passphrase_button", | |
| 168 IDS_UNLOCK_PASSPHRASE_BUTTON); | |
| 169 data_source->AddLocalizedString("passphrase_instruction", | |
| 170 IDS_PASSPHRASE_INSTRUCTION); | |
| 171 data_source->AddLocalizedString("incorrect_passphrase_warning", | |
| 172 IDS_INCORRECT_PASSPHRASE_WARNING); | |
| 173 data_source->AddLocalizedString("cancel_passphrase_button", IDS_CANCEL); | |
| 174 data_source->set_json_path("strings.js"); | |
| 175 data_source->set_use_json_js_format_v2(); | |
| 176 ChromeURLDataManager::AddDataSource(profile, data_source); | |
| 177 } | |
| OLD | NEW |