Chromium Code Reviews| Index: chrome/browser/ui/webui/managed_user_passphrase_dialog.cc |
| diff --git a/chrome/browser/ui/webui/managed_user_passphrase_dialog.cc b/chrome/browser/ui/webui/managed_user_passphrase_dialog.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f4d29029a8821f04563aa37938a434c6a89795d4 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/managed_user_passphrase_dialog.cc |
| @@ -0,0 +1,170 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/webui/managed_user_passphrase_dialog.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/values.h" |
|
James Hawkins
2013/02/12 18:57:16
Where is this used?
Adrian Kuegel
2013/02/13 10:53:36
For the CheckPassphrase method, which is called fr
|
| +#include "chrome/browser/managed_mode/managed_user_passphrase.h" |
| +#include "chrome/browser/managed_mode/managed_user_service.h" |
| +#include "chrome/browser/managed_mode/managed_user_service_factory.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_ui_data_source.h" |
| +#include "content/public/browser/web_ui_message_handler.h" |
| +#include "grit/browser_resources.h" |
| +#include "grit/generated_resources.h" |
| +#include "ui/gfx/size.h" |
| + |
| +namespace { |
| + |
| +class ManagedUserPassphraseDialogMessageHandler |
|
James Hawkins
2013/02/12 18:57:16
nit: Document class.
Adrian Kuegel
2013/02/13 10:53:36
Done.
|
| + : public content::WebUIMessageHandler { |
| + |
| + public: |
| + ManagedUserPassphraseDialogMessageHandler(); |
| + virtual ~ManagedUserPassphraseDialogMessageHandler() {} |
| + virtual void RegisterMessages() OVERRIDE; |
|
James Hawkins
2013/02/12 18:57:16
// content::WebUIMessageHandler implementation.
Adrian Kuegel
2013/02/13 10:53:36
Done.
|
| + |
| + private: |
| + void CheckPassphrase(const base::ListValue* args) const; |
|
James Hawkins
2013/02/12 18:57:16
nit: Document method.
Adrian Kuegel
2013/02/13 10:53:36
Done.
|
| + |
| + base::WeakPtrFactory<ManagedUserPassphraseDialogMessageHandler> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ManagedUserPassphraseDialogMessageHandler); |
| +}; |
| + |
| +ManagedUserPassphraseDialogMessageHandler |
| + ::ManagedUserPassphraseDialogMessageHandler() : weak_factory_(this) { |
| +} |
| + |
| +void ManagedUserPassphraseDialogMessageHandler::RegisterMessages() { |
| + web_ui()->RegisterMessageCallback("checkPassphrase", |
|
James Hawkins
2013/02/12 18:57:16
nit: The start of parameter rows must align on the
Adrian Kuegel
2013/02/13 10:53:36
Done.
|
| + base::Bind(&ManagedUserPassphraseDialogMessageHandler::CheckPassphrase, |
| + weak_factory_.GetWeakPtr())); |
| +} |
| + |
| +void ManagedUserPassphraseDialogMessageHandler::CheckPassphrase( |
| + const base::ListValue* args) const { |
| + const base::Value* passphrase_arg = NULL; |
|
James Hawkins
2013/02/12 18:57:16
This method could use some whitespace love and som
Adrian Kuegel
2013/02/13 10:53:36
Done.
|
| + args->Get(0, &passphrase_arg); |
| + std::string passphrase; |
| + passphrase_arg->GetAsString(&passphrase); |
| + Profile* profile = Profile::FromWebUI(web_ui()); |
| + PrefService* pref_service = profile->GetPrefs(); |
| + std::string stored_passphrase_hash = |
| + pref_service->GetString(prefs::kManagedModeLocalPassphrase); |
| + std::string salt = pref_service->GetString(prefs::kManagedModeLocalSalt); |
| + ManagedUserPassphrase passphrase_key_generator(salt); |
| + std::string encoded_passphrase_hash; |
| + passphrase_key_generator.GenerateHashFromPassphrase(passphrase, |
| + &encoded_passphrase_hash); |
| + ManagedUserService* managed_user_service = |
| + ManagedUserServiceFactory::GetForProfile(profile); |
| + if (stored_passphrase_hash == encoded_passphrase_hash) { |
| + managed_user_service->SetElevated(true); |
| + web_ui()->CallJavascriptFunction("passphraseCorrect"); |
| + } else { |
| + managed_user_service->SetElevated(false); |
| + web_ui()->CallJavascriptFunction("passphraseIncorrect"); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +const int kDialogWidth = 400; |
|
James Hawkins
2013/02/12 18:57:16
These are in the global namespace, which is no bue
Adrian Kuegel
2013/02/13 10:53:36
Done.
|
| +const int kDialogHeight = 310; |
| + |
| +ManagedUserPassphraseDialog::ManagedUserPassphraseDialog( |
| + content::WebContents* web_contents, |
| + const base::Callback<void(bool)>& callback) : callback_(callback), |
| + closing_(false) { |
| + Profile* profile = |
| + Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| + CreateDataSource(profile); |
| + CreateConstrainedWebDialog(profile, this, NULL, web_contents); |
| +} |
| + |
| + |
| +ui::ModalType ManagedUserPassphraseDialog::GetDialogModalType() const { |
| + return ui::MODAL_TYPE_WINDOW; |
| +} |
| + |
| +string16 ManagedUserPassphraseDialog::GetDialogTitle() const { |
| + return string16(); |
| +} |
| + |
| +GURL ManagedUserPassphraseDialog::GetDialogContentURL() const { |
| + return GURL(chrome::kChromeUIManagedUserPassphrasePageURL); |
| +} |
| + |
| +// This function is called by the constrained window delegate. |
|
James Hawkins
2013/02/12 18:57:16
This comment is superfluous.
Adrian Kuegel
2013/02/13 10:53:36
Done.
|
| +void ManagedUserPassphraseDialog::GetWebUIMessageHandlers( |
| + std::vector<content::WebUIMessageHandler*>* handlers) const { |
| + DCHECK(handlers); |
| + // The constrained window delegate takes care of registering the handler. |
| + // The handler is also deleted automatically. |
| + handlers->push_back(new ManagedUserPassphraseDialogMessageHandler()); |
| +} |
| + |
| +void ManagedUserPassphraseDialog::GetDialogSize(gfx::Size* size) const { |
| + size->SetSize(kDialogWidth, kDialogHeight); |
| +} |
| + |
| +std::string ManagedUserPassphraseDialog::GetDialogArgs() const { |
| + return std::string(); |
| +} |
| + |
| +void ManagedUserPassphraseDialog::OnDialogClosed( |
| + const std::string& json_retval) { |
| + if (closing_) |
|
James Hawkins
2013/02/12 18:57:16
Why do you need |closing_|? Can't you reset the c
Adrian Kuegel
2013/02/13 10:53:36
True, I don't need |closing_|. I changed it like y
|
| + return; |
| + |
| + closing_ = true; |
| + callback_.Run(!json_retval.empty()); |
| +} |
| + |
| +void ManagedUserPassphraseDialog::OnCloseContents( |
| + content::WebContents* source, bool* out_close_dialog) { |
| +} |
| + |
| +bool ManagedUserPassphraseDialog::ShouldShowDialogTitle() const { |
| + return false; |
| +} |
| + |
| +ManagedUserPassphraseDialog::~ManagedUserPassphraseDialog() { |
| +} |
| + |
| +void ManagedUserPassphraseDialog::CreateDataSource(Profile* profile) const { |
| + content::WebUIDataSource* data_source = content::WebUIDataSource::Create( |
| + chrome::kChromeUIManagedUserPassphrasePageHost); |
| + data_source->SetDefaultResource(IDR_MANAGED_USER_PASSPHRASE_DIALOG_HTML); |
| + data_source->AddResourcePath("managed_user_passphrase_dialog.js", |
| + IDR_MANAGED_USER_PASSPHRASE_DIALOG_JS); |
| + data_source->AddResourcePath("managed_user_passphrase_dialog.css", |
| + IDR_MANAGED_USER_PASSPHRASE_DIALOG_CSS); |
| + data_source->AddResourcePath("managed_user.png", |
| + IDR_MANAGED_USER_PASSPHRASE_DIALOG_IMG); |
| + data_source->AddLocalizedString("managedModePassphrasePage", |
| + IDS_PASSPHRASE_TITLE); |
| + data_source->AddLocalizedString("forgot_passphrase", IDS_FORGOT_PASSPHRASE); |
| + data_source->AddLocalizedString("unlock_passphrase_button", |
| + IDS_UNLOCK_PASSPHRASE_BUTTON); |
| + data_source->AddLocalizedString("passphrase_instruction", |
| + IDS_PASSPHRASE_INSTRUCTION); |
| + data_source->AddLocalizedString("incorrect_passphrase_warning", |
| + IDS_INCORRECT_PASSPHRASE_WARNING); |
| + data_source->AddLocalizedString("cancel_passphrase_button", IDS_CANCEL); |
| + data_source->SetJsonPath("strings.js"); |
| + data_source->SetUseJsonJSFormatV2(); |
| + content::WebUIDataSource::Add(profile, data_source); |
| +} |