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/metrics/histogram.h" | |
13 #include "base/prefs/pref_service.h" | |
14 #include "base/values.h" | |
15 #include "chrome/browser/managed_mode/managed_user_passphrase.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" | |
18 #include "chrome/common/pref_names.h" | |
19 #include "chrome/common/url_constants.h" | |
20 #include "content/public/browser/user_metrics.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/base/resource/resource_bundle.h" | |
27 #include "ui/gfx/size.h" | |
28 | |
29 using content::UserMetricsAction; | |
30 | |
31 namespace { | |
32 | |
33 // Handles the message when the user entered a passphrase and clicks the Unlock | |
34 // button. | |
35 class ManagedUserPassphraseDialogMessageHandler | |
36 : public content::WebUIMessageHandler { | |
37 | |
38 public: | |
39 ManagedUserPassphraseDialogMessageHandler(); | |
40 virtual ~ManagedUserPassphraseDialogMessageHandler() {} | |
41 | |
42 // content::WebUIMessageHandler implementation. | |
43 virtual void RegisterMessages() OVERRIDE; | |
44 | |
45 private: | |
46 // Gets called from the UI with the entered passphrase as a parameter. The | |
47 // correctness of the passphrase is checked and the result is returned to the | |
48 // UI. | |
49 void CheckPassphrase(const base::ListValue* args) const; | |
50 | |
51 base::WeakPtrFactory<ManagedUserPassphraseDialogMessageHandler> weak_factory_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(ManagedUserPassphraseDialogMessageHandler); | |
54 }; | |
55 | |
56 ManagedUserPassphraseDialogMessageHandler | |
57 ::ManagedUserPassphraseDialogMessageHandler() | |
58 : weak_factory_(this) { | |
59 } | |
60 | |
61 void ManagedUserPassphraseDialogMessageHandler::RegisterMessages() { | |
62 web_ui()->RegisterMessageCallback( | |
63 "checkPassphrase", | |
64 base::Bind(&ManagedUserPassphraseDialogMessageHandler::CheckPassphrase, | |
65 weak_factory_.GetWeakPtr())); | |
66 } | |
67 | |
68 void ManagedUserPassphraseDialogMessageHandler::CheckPassphrase( | |
69 const base::ListValue* args) const { | |
70 // Extract the passphrase from the provided ListValue parameter. | |
71 const base::Value* passphrase_arg = NULL; | |
72 args->Get(0, &passphrase_arg); | |
73 std::string passphrase; | |
74 passphrase_arg->GetAsString(&passphrase); | |
75 | |
76 // Get the hashed passphrase and the salt that was used to calculate it. | |
77 Profile* profile = Profile::FromWebUI(web_ui()); | |
78 PrefService* pref_service = profile->GetPrefs(); | |
79 std::string stored_passphrase_hash = | |
80 pref_service->GetString(prefs::kManagedModeLocalPassphrase); | |
81 std::string salt = pref_service->GetString(prefs::kManagedModeLocalSalt); | |
82 | |
83 // Calculate the hash of the entered passphrase. | |
84 ManagedUserPassphrase passphrase_key_generator(salt); | |
85 std::string encoded_passphrase_hash; | |
86 passphrase_key_generator.GenerateHashFromPassphrase(passphrase, | |
87 &encoded_passphrase_hash); | |
88 | |
89 // Check if the entered passphrase is correct and give the result back to the | |
90 // UI. | |
91 bool is_correct = stored_passphrase_hash == encoded_passphrase_hash; | |
92 base::FundamentalValue passphrase_correct(is_correct); | |
93 web_ui()->CallJavascriptFunction("passphraseResult", passphrase_correct); | |
94 UMA_HISTOGRAM_BOOLEAN("ManagedMode_PassphraseCorrect", is_correct); | |
95 } | |
96 | |
97 } // namespace | |
98 | |
99 ManagedUserPassphraseDialog::ManagedUserPassphraseDialog( | |
100 content::WebContents* web_contents, | |
101 const PassphraseCheckedCallback& callback) : callback_(callback) { | |
102 content::RecordAction(UserMetricsAction("ManagedMode_OpenPassphraseDialog")); | |
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 = 383; | |
132 const int kDialogHeight = 225; | |
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 content::RecordAction( | |
146 UserMetricsAction("ManagedMode_ClosePassphraseDialog")); | |
147 } | |
148 } | |
149 | |
150 void ManagedUserPassphraseDialog::OnCloseContents( | |
151 content::WebContents* source, bool* out_close_dialog) { | |
152 } | |
153 | |
154 bool ManagedUserPassphraseDialog::ShouldShowDialogTitle() const { | |
155 return false; | |
156 } | |
157 | |
158 ManagedUserPassphraseDialog::~ManagedUserPassphraseDialog() { | |
159 } | |
160 | |
161 void ManagedUserPassphraseDialog::CreateDataSource(Profile* profile) const { | |
162 content::WebUIDataSource* data_source = content::WebUIDataSource::Create( | |
163 chrome::kChromeUIManagedUserPassphrasePageHost); | |
164 data_source->SetDefaultResource(IDR_MANAGED_USER_PASSPHRASE_DIALOG_HTML); | |
165 // DCHECK to uncover if the passphrase dialog can actually load and display | |
166 // the html resource when running a browser test. | |
167 DCHECK(ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | |
168 IDR_MANAGED_USER_PASSPHRASE_DIALOG_HTML)); | |
169 data_source->AddResourcePath("managed_user_passphrase_dialog.js", | |
170 IDR_MANAGED_USER_PASSPHRASE_DIALOG_JS); | |
171 data_source->AddLocalizedString("managedModePassphrasePage", | |
172 IDS_PASSPHRASE_TITLE); | |
173 data_source->AddLocalizedString("unlockPassphraseButton", | |
174 IDS_UNLOCK_PASSPHRASE_BUTTON); | |
175 data_source->AddLocalizedString("passphraseInstruction", | |
176 IDS_PASSPHRASE_INSTRUCTION); | |
177 data_source->AddLocalizedString("incorrectPassphraseWarning", | |
178 IDS_INCORRECT_PASSPHRASE_WARNING); | |
179 data_source->AddLocalizedString("cancelPassphraseButton", IDS_CANCEL); | |
180 data_source->SetJsonPath("strings.js"); | |
181 data_source->SetUseJsonJSFormatV2(); | |
182 content::WebUIDataSource::Add(profile, data_source); | |
183 } | |
OLD | NEW |