OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/browser/managed_mode/managed_user_passphrase.h" |
| 15 #include "chrome/browser/managed_mode/managed_user_service.h" |
| 16 #include "chrome/browser/managed_mode/managed_user_service_factory.h" |
| 17 #include "chrome/browser/profiles/profile.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_data_source.h" |
| 23 #include "content/public/browser/web_ui_message_handler.h" |
| 24 #include "grit/browser_resources.h" |
| 25 #include "grit/generated_resources.h" |
| 26 #include "ui/gfx/size.h" |
| 27 |
| 28 namespace { |
| 29 |
| 30 // Handles the message when the user entered a passphrase and clicks the Unlock |
| 31 // button. |
| 32 class ManagedUserPassphraseDialogMessageHandler |
| 33 : public content::WebUIMessageHandler { |
| 34 |
| 35 public: |
| 36 ManagedUserPassphraseDialogMessageHandler(); |
| 37 virtual ~ManagedUserPassphraseDialogMessageHandler() {} |
| 38 |
| 39 // content::WebUIMessageHandler implementation. |
| 40 virtual void RegisterMessages() OVERRIDE; |
| 41 |
| 42 private: |
| 43 // Gets called from the UI with the entered passphrase as a parameter. The |
| 44 // correctness of the passphrase is checked and the result is returned to the |
| 45 // UI. |
| 46 void CheckPassphrase(const base::ListValue* args) const; |
| 47 |
| 48 base::WeakPtrFactory<ManagedUserPassphraseDialogMessageHandler> weak_factory_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(ManagedUserPassphraseDialogMessageHandler); |
| 51 }; |
| 52 |
| 53 ManagedUserPassphraseDialogMessageHandler |
| 54 ::ManagedUserPassphraseDialogMessageHandler() : weak_factory_(this) { |
| 55 } |
| 56 |
| 57 void ManagedUserPassphraseDialogMessageHandler::RegisterMessages() { |
| 58 web_ui()->RegisterMessageCallback( |
| 59 "checkPassphrase", |
| 60 base::Bind(&ManagedUserPassphraseDialogMessageHandler::CheckPassphrase, |
| 61 weak_factory_.GetWeakPtr())); |
| 62 } |
| 63 |
| 64 void ManagedUserPassphraseDialogMessageHandler::CheckPassphrase( |
| 65 const base::ListValue* args) const { |
| 66 // Extract the passphrase from the provided ListValue parameter. |
| 67 const base::Value* passphrase_arg = NULL; |
| 68 args->Get(0, &passphrase_arg); |
| 69 std::string passphrase; |
| 70 passphrase_arg->GetAsString(&passphrase); |
| 71 |
| 72 // Get the hashed passphrase and the salt that was used to calculate it. |
| 73 Profile* profile = Profile::FromWebUI(web_ui()); |
| 74 PrefService* pref_service = profile->GetPrefs(); |
| 75 std::string stored_passphrase_hash = |
| 76 pref_service->GetString(prefs::kManagedModeLocalPassphrase); |
| 77 std::string salt = pref_service->GetString(prefs::kManagedModeLocalSalt); |
| 78 |
| 79 // Calculate the hash of the entered passphrase. |
| 80 ManagedUserPassphrase passphrase_key_generator(salt); |
| 81 std::string encoded_passphrase_hash; |
| 82 passphrase_key_generator.GenerateHashFromPassphrase(passphrase, |
| 83 &encoded_passphrase_hash); |
| 84 |
| 85 // Check if the entered passphrase is correct and possibly set the managed |
| 86 // user into the elevated state which for example allows to modify settings. |
| 87 ManagedUserService* managed_user_service = |
| 88 ManagedUserServiceFactory::GetForProfile(profile); |
| 89 if (stored_passphrase_hash == encoded_passphrase_hash) { |
| 90 managed_user_service->SetElevated(true); |
| 91 web_ui()->CallJavascriptFunction("passphraseCorrect"); |
| 92 } else { |
| 93 managed_user_service->SetElevated(false); |
| 94 web_ui()->CallJavascriptFunction("passphraseIncorrect"); |
| 95 } |
| 96 } |
| 97 |
| 98 } // namespace |
| 99 |
| 100 ManagedUserPassphraseDialog::ManagedUserPassphraseDialog( |
| 101 content::WebContents* web_contents, |
| 102 const PassphraseCheckedCallback& callback) : callback_(callback) { |
| 103 Profile* profile = |
| 104 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 105 CreateDataSource(profile); |
| 106 CreateConstrainedWebDialog(profile, this, NULL, web_contents); |
| 107 } |
| 108 |
| 109 |
| 110 ui::ModalType ManagedUserPassphraseDialog::GetDialogModalType() const { |
| 111 return ui::MODAL_TYPE_WINDOW; |
| 112 } |
| 113 |
| 114 string16 ManagedUserPassphraseDialog::GetDialogTitle() const { |
| 115 return string16(); |
| 116 } |
| 117 |
| 118 GURL ManagedUserPassphraseDialog::GetDialogContentURL() const { |
| 119 return GURL(chrome::kChromeUIManagedUserPassphrasePageURL); |
| 120 } |
| 121 |
| 122 void ManagedUserPassphraseDialog::GetWebUIMessageHandlers( |
| 123 std::vector<content::WebUIMessageHandler*>* handlers) const { |
| 124 DCHECK(handlers); |
| 125 // The constrained window delegate takes care of registering the handler. |
| 126 // The handler is also deleted automatically. |
| 127 handlers->push_back(new ManagedUserPassphraseDialogMessageHandler()); |
| 128 } |
| 129 |
| 130 void ManagedUserPassphraseDialog::GetDialogSize(gfx::Size* size) const { |
| 131 const int kDialogWidth = 400; |
| 132 const int kDialogHeight = 310; |
| 133 size->SetSize(kDialogWidth, kDialogHeight); |
| 134 } |
| 135 |
| 136 std::string ManagedUserPassphraseDialog::GetDialogArgs() const { |
| 137 return std::string(); |
| 138 } |
| 139 |
| 140 void ManagedUserPassphraseDialog::OnDialogClosed( |
| 141 const std::string& json_retval) { |
| 142 if (!callback_.is_null()) { |
| 143 callback_.Run(!json_retval.empty()); |
| 144 callback_.Reset(); |
| 145 } |
| 146 } |
| 147 |
| 148 void ManagedUserPassphraseDialog::OnCloseContents( |
| 149 content::WebContents* source, bool* out_close_dialog) { |
| 150 } |
| 151 |
| 152 bool ManagedUserPassphraseDialog::ShouldShowDialogTitle() const { |
| 153 return false; |
| 154 } |
| 155 |
| 156 ManagedUserPassphraseDialog::~ManagedUserPassphraseDialog() { |
| 157 } |
| 158 |
| 159 void ManagedUserPassphraseDialog::CreateDataSource(Profile* profile) const { |
| 160 content::WebUIDataSource* data_source = content::WebUIDataSource::Create( |
| 161 chrome::kChromeUIManagedUserPassphrasePageHost); |
| 162 data_source->SetDefaultResource(IDR_MANAGED_USER_PASSPHRASE_DIALOG_HTML); |
| 163 data_source->AddResourcePath("managed_user_passphrase_dialog.js", |
| 164 IDR_MANAGED_USER_PASSPHRASE_DIALOG_JS); |
| 165 data_source->AddResourcePath("managed_user_passphrase_dialog.css", |
| 166 IDR_MANAGED_USER_PASSPHRASE_DIALOG_CSS); |
| 167 data_source->AddResourcePath("managed_user.png", |
| 168 IDR_MANAGED_USER_PASSPHRASE_DIALOG_IMG); |
| 169 data_source->AddLocalizedString("managedModePassphrasePage", |
| 170 IDS_PASSPHRASE_TITLE); |
| 171 data_source->AddLocalizedString("unlockPassphraseButton", |
| 172 IDS_UNLOCK_PASSPHRASE_BUTTON); |
| 173 data_source->AddLocalizedString("passphraseInstruction", |
| 174 IDS_PASSPHRASE_INSTRUCTION); |
| 175 data_source->AddLocalizedString("incorrectPassphraseWarning", |
| 176 IDS_INCORRECT_PASSPHRASE_WARNING); |
| 177 data_source->AddLocalizedString("cancelPassphraseButton", IDS_CANCEL); |
| 178 data_source->SetJsonPath("strings.js"); |
| 179 data_source->SetUseJsonJSFormatV2(); |
| 180 content::WebUIDataSource::Add(profile, data_source); |
| 181 } |
OLD | NEW |