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.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/managed_mode/managed_user_service.h" |
| 15 #include "chrome/browser/managed_mode/managed_user_service_factory.h" |
| 16 #include "chrome/browser/prefs/pref_service.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 class ManagedUserPassphraseDialogMessageHandler |
| 31 : public content::WebUIMessageHandler { |
| 32 |
| 33 public: |
| 34 ManagedUserPassphraseDialogMessageHandler(); |
| 35 virtual ~ManagedUserPassphraseDialogMessageHandler() {} |
| 36 virtual void RegisterMessages() OVERRIDE; |
| 37 |
| 38 private: |
| 39 void CheckPassphrase(const base::ListValue* args) const; |
| 40 |
| 41 base::WeakPtrFactory<ManagedUserPassphraseDialogMessageHandler> weak_factory_; |
| 42 |
| 43 DISALLOW_COPY_AND_ASSIGN(ManagedUserPassphraseDialogMessageHandler); |
| 44 }; |
| 45 |
| 46 ManagedUserPassphraseDialogMessageHandler |
| 47 ::ManagedUserPassphraseDialogMessageHandler() : weak_factory_(this) { |
| 48 } |
| 49 |
| 50 void ManagedUserPassphraseDialogMessageHandler::RegisterMessages() { |
| 51 web_ui()->RegisterMessageCallback("checkPassphrase", |
| 52 base::Bind(&ManagedUserPassphraseDialogMessageHandler::CheckPassphrase, |
| 53 weak_factory_.GetWeakPtr())); |
| 54 } |
| 55 |
| 56 void ManagedUserPassphraseDialogMessageHandler::CheckPassphrase( |
| 57 const base::ListValue* args) const { |
| 58 const base::Value* passphrase_arg = NULL; |
| 59 args->Get(0, &passphrase_arg); |
| 60 std::string passphrase; |
| 61 passphrase_arg->GetAsString(&passphrase); |
| 62 Profile* profile = Profile::FromWebUI(web_ui()); |
| 63 PrefService* pref_service = profile->GetPrefs(); |
| 64 std::string stored_passphrase_hash = |
| 65 pref_service->GetString(prefs::kManagedModeLocalPassphrase); |
| 66 std::string salt = pref_service->GetString(prefs::kManagedModeLocalSalt); |
| 67 ManagedUserPassphrase passphrase_key_generator(salt); |
| 68 std::string encoded_passphrase_hash; |
| 69 passphrase_key_generator.GenerateHashFromPassphrase(passphrase, |
| 70 &encoded_passphrase_hash); |
| 71 ManagedUserService* managed_user_service = |
| 72 ManagedUserServiceFactory::GetForProfile(profile); |
| 73 if (stored_passphrase_hash == encoded_passphrase_hash) { |
| 74 managed_user_service->SetElevated(true); |
| 75 web_ui()->CallJavascriptFunction("passphraseCorrect"); |
| 76 } else { |
| 77 managed_user_service->SetElevated(false); |
| 78 web_ui()->CallJavascriptFunction("passphraseIncorrect"); |
| 79 } |
| 80 } |
| 81 |
| 82 } // namespace |
| 83 |
| 84 const int kDialogWidth = 400; |
| 85 const int kDialogHeight = 310; |
| 86 |
| 87 ManagedUserPassphraseDialog::ManagedUserPassphraseDialog( |
| 88 content::WebContents* web_contents, |
| 89 const base::Callback<void(bool)>& callback) : callback_(callback), |
| 90 closing_(false) { |
| 91 Profile* profile = |
| 92 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 93 CreateDataSource(profile); |
| 94 CreateConstrainedWebDialog(profile, this, NULL, web_contents); |
| 95 } |
| 96 |
| 97 |
| 98 ui::ModalType ManagedUserPassphraseDialog::GetDialogModalType() const { |
| 99 return ui::MODAL_TYPE_WINDOW; |
| 100 } |
| 101 |
| 102 string16 ManagedUserPassphraseDialog::GetDialogTitle() const { |
| 103 return string16(); |
| 104 } |
| 105 |
| 106 GURL ManagedUserPassphraseDialog::GetDialogContentURL() const { |
| 107 return GURL(chrome::kChromeUIManagedUserPassphrasePageURL); |
| 108 } |
| 109 |
| 110 // This function is called by the constrained window delegate. |
| 111 void ManagedUserPassphraseDialog::GetWebUIMessageHandlers( |
| 112 std::vector<content::WebUIMessageHandler*>* handlers) const { |
| 113 DCHECK(handlers); |
| 114 // The constrained window delegate takes care of registering the handler. |
| 115 // The handler is also deleted automatically. |
| 116 handlers->push_back(new ManagedUserPassphraseDialogMessageHandler()); |
| 117 } |
| 118 |
| 119 void ManagedUserPassphraseDialog::GetDialogSize(gfx::Size* size) const { |
| 120 size->SetSize(kDialogWidth, kDialogHeight); |
| 121 } |
| 122 |
| 123 std::string ManagedUserPassphraseDialog::GetDialogArgs() const { |
| 124 return std::string(); |
| 125 } |
| 126 |
| 127 void ManagedUserPassphraseDialog::OnDialogClosed( |
| 128 const std::string& json_retval) { |
| 129 if (closing_) |
| 130 return; |
| 131 |
| 132 closing_ = true; |
| 133 callback_.Run(!json_retval.empty()); |
| 134 } |
| 135 |
| 136 void ManagedUserPassphraseDialog::OnCloseContents( |
| 137 content::WebContents* source, bool* out_close_dialog) { |
| 138 } |
| 139 |
| 140 bool ManagedUserPassphraseDialog::ShouldShowDialogTitle() const { |
| 141 return false; |
| 142 } |
| 143 |
| 144 ManagedUserPassphraseDialog::~ManagedUserPassphraseDialog() { |
| 145 } |
| 146 |
| 147 void ManagedUserPassphraseDialog::CreateDataSource(Profile* profile) const { |
| 148 content::WebUIDataSource* data_source = content::WebUIDataSource::Create( |
| 149 chrome::kChromeUIManagedUserPassphrasePageHost); |
| 150 data_source->SetDefaultResource(IDR_MANAGED_USER_PASSPHRASE_DIALOG_HTML); |
| 151 data_source->AddResourcePath("managed_user_passphrase_dialog.js", |
| 152 IDR_MANAGED_USER_PASSPHRASE_DIALOG_JS); |
| 153 data_source->AddResourcePath("managed_user_passphrase_dialog.css", |
| 154 IDR_MANAGED_USER_PASSPHRASE_DIALOG_CSS); |
| 155 data_source->AddResourcePath("managed_user.png", |
| 156 IDR_MANAGED_USER_PASSPHRASE_DIALOG_IMG); |
| 157 data_source->AddLocalizedString("managedModePassphrasePage", |
| 158 IDS_PASSPHRASE_TITLE); |
| 159 data_source->AddLocalizedString("forgot_passphrase", IDS_FORGOT_PASSPHRASE); |
| 160 data_source->AddLocalizedString("unlock_passphrase_button", |
| 161 IDS_UNLOCK_PASSPHRASE_BUTTON); |
| 162 data_source->AddLocalizedString("passphrase_instruction", |
| 163 IDS_PASSPHRASE_INSTRUCTION); |
| 164 data_source->AddLocalizedString("incorrect_passphrase_warning", |
| 165 IDS_INCORRECT_PASSPHRASE_WARNING); |
| 166 data_source->AddLocalizedString("cancel_passphrase_button", IDS_CANCEL); |
| 167 data_source->SetJsonPath("strings.js"); |
| 168 data_source->SetUseJsonJSFormatV2(); |
| 169 content::WebUIDataSource::Add(profile, data_source); |
| 170 } |
OLD | NEW |