Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Side by Side Diff: chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler.cc

Issue 1896463003: WebUI: Add JavaScript lifecycle-control to WebUIMessageHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler .h" 5 #include "chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler .h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/signin/easy_unlock_service.h" 14 #include "chrome/browser/signin/easy_unlock_service.h"
15 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
16 #include "chrome/grit/generated_resources.h" 16 #include "chrome/grit/generated_resources.h"
17 #include "components/proximity_auth/switches.h" 17 #include "components/proximity_auth/switches.h"
18 #include "content/public/browser/web_ui.h" 18 #include "content/public/browser/web_ui.h"
19 #include "content/public/browser/web_ui_data_source.h" 19 #include "content/public/browser/web_ui_data_source.h"
20 20
21 namespace chromeos { 21 namespace chromeos {
22 namespace settings { 22 namespace settings {
23 23
24 EasyUnlockSettingsHandler::EasyUnlockSettingsHandler(Profile* profile) 24 EasyUnlockSettingsHandler::EasyUnlockSettingsHandler(Profile* profile)
25 : profile_(profile), observers_registered_(false) { 25 : profile_(profile) {
26 profile_pref_registrar_.Init(profile->GetPrefs()); 26 profile_pref_registrar_.Init(profile->GetPrefs());
27 } 27 }
28 28
29 EasyUnlockSettingsHandler::~EasyUnlockSettingsHandler() { 29 EasyUnlockSettingsHandler::~EasyUnlockSettingsHandler() {
30 if (observers_registered_) 30 EasyUnlockService::Get(profile_)->RemoveObserver(this);
31 EasyUnlockService::Get(profile_)->RemoveObserver(this);
32 } 31 }
33 32
34 EasyUnlockSettingsHandler* EasyUnlockSettingsHandler::Create( 33 EasyUnlockSettingsHandler* EasyUnlockSettingsHandler::Create(
35 content::WebUIDataSource* html_source, 34 content::WebUIDataSource* html_source,
36 Profile* profile) { 35 Profile* profile) {
37 EasyUnlockService* easy_unlock_service = EasyUnlockService::Get(profile); 36 EasyUnlockService* easy_unlock_service = EasyUnlockService::Get(profile);
38 bool allowed = easy_unlock_service->IsAllowed(); 37 bool allowed = easy_unlock_service->IsAllowed();
39 html_source->AddBoolean("easyUnlockAllowed", allowed); 38 html_source->AddBoolean("easyUnlockAllowed", allowed);
40 html_source->AddBoolean("easyUnlockEnabled", 39 html_source->AddBoolean("easyUnlockEnabled",
41 allowed ? easy_unlock_service->IsEnabled() : false); 40 allowed ? easy_unlock_service->IsEnabled() : false);
(...skipping 24 matching lines...) Expand all
66 web_ui()->RegisterMessageCallback( 65 web_ui()->RegisterMessageCallback(
67 "easyUnlockStartTurnOffFlow", 66 "easyUnlockStartTurnOffFlow",
68 base::Bind(&EasyUnlockSettingsHandler::HandleStartTurnOffFlow, 67 base::Bind(&EasyUnlockSettingsHandler::HandleStartTurnOffFlow,
69 base::Unretained(this))); 68 base::Unretained(this)));
70 web_ui()->RegisterMessageCallback( 69 web_ui()->RegisterMessageCallback(
71 "easyUnlockCancelTurnOffFlow", 70 "easyUnlockCancelTurnOffFlow",
72 base::Bind(&EasyUnlockSettingsHandler::HandleCancelTurnOffFlow, 71 base::Bind(&EasyUnlockSettingsHandler::HandleCancelTurnOffFlow,
73 base::Unretained(this))); 72 base::Unretained(this)));
74 } 73 }
75 74
76 void EasyUnlockSettingsHandler::RenderViewReused() { 75 void EasyUnlockSettingsHandler::OnJavascriptAllowed() {
77 // When the page is reloaded, we clear our observers and re-register when 76 EasyUnlockService::Get(profile_)->AddObserver(this);
78 // the new page's DOM is ready.
79 if (!observers_registered_)
80 return;
81 77
78 profile_pref_registrar_.Add(
79 prefs::kEasyUnlockPairing,
80 base::Bind(&EasyUnlockSettingsHandler::SendEnabledStatus,
81 base::Unretained(this)));
82 }
83
84 void EasyUnlockSettingsHandler::OnJavascriptDisallowed() {
82 EasyUnlockService::Get(profile_)->RemoveObserver(this); 85 EasyUnlockService::Get(profile_)->RemoveObserver(this);
83 profile_pref_registrar_.RemoveAll(); 86 profile_pref_registrar_.RemoveAll();
84
85 observers_registered_ = false;
86 } 87 }
87 88
88 void EasyUnlockSettingsHandler::OnTurnOffOperationStatusChanged() { 89 void EasyUnlockSettingsHandler::OnTurnOffOperationStatusChanged() {
89 web_ui()->CallJavascriptFunction( 90 CallJavascriptFunction("cr.webUIListenerCallback",
90 "cr.webUIListenerCallback", 91 base::StringValue("easy-unlock-turn-off-flow-status"),
91 base::StringValue("easy-unlock-turn-off-flow-status"), 92 base::StringValue(GetTurnOffFlowStatus()));
92 base::StringValue(GetTurnOffFlowStatus()));
93 } 93 }
94 94
95 void EasyUnlockSettingsHandler::SendEnabledStatus() { 95 void EasyUnlockSettingsHandler::SendEnabledStatus() {
96 web_ui()->CallJavascriptFunction( 96 CallJavascriptFunction(
97 "cr.webUIListenerCallback", 97 "cr.webUIListenerCallback",
98 base::StringValue("easy-unlock-enabled-status"), 98 base::StringValue("easy-unlock-enabled-status"),
99 base::FundamentalValue(EasyUnlockService::Get(profile_)->IsEnabled())); 99 base::FundamentalValue(EasyUnlockService::Get(profile_)->IsEnabled()));
100 } 100 }
101 101
102 std::string EasyUnlockSettingsHandler::GetTurnOffFlowStatus() { 102 std::string EasyUnlockSettingsHandler::GetTurnOffFlowStatus() {
103 EasyUnlockService::TurnOffFlowStatus status = 103 EasyUnlockService::TurnOffFlowStatus status =
104 EasyUnlockService::Get(profile_)->GetTurnOffFlowStatus(); 104 EasyUnlockService::Get(profile_)->GetTurnOffFlowStatus();
105 105
106 // Translate status into JS UI state string. Note the translated string 106 // Translate status into JS UI state string. Note the translated string
(...skipping 13 matching lines...) Expand all
120 LOG(ERROR) << "Unknown Easy unlock turn-off operation status: " << status; 120 LOG(ERROR) << "Unknown Easy unlock turn-off operation status: " << status;
121 status_string = "idle"; 121 status_string = "idle";
122 break; 122 break;
123 } 123 }
124 124
125 return status_string; 125 return status_string;
126 } 126 }
127 127
128 void EasyUnlockSettingsHandler::HandleGetEnabledStatus( 128 void EasyUnlockSettingsHandler::HandleGetEnabledStatus(
129 const base::ListValue* args) { 129 const base::ListValue* args) {
130 // This method is called when the DOM is first ready. Therefore we initialize 130 AllowJavascript();
131 // our observers here.
132 if (!observers_registered_) {
133 EasyUnlockService::Get(profile_)->AddObserver(this);
134
135 profile_pref_registrar_.Add(
136 prefs::kEasyUnlockPairing,
137 base::Bind(&EasyUnlockSettingsHandler::SendEnabledStatus,
138 base::Unretained(this)));
139
140 observers_registered_ = true;
141 }
142 131
143 CHECK_EQ(1U, args->GetSize()); 132 CHECK_EQ(1U, args->GetSize());
144 const base::Value* callback_id; 133 const base::Value* callback_id;
145 CHECK(args->Get(0, &callback_id)); 134 CHECK(args->Get(0, &callback_id));
146 ResolveJavascriptCallback( 135 ResolveJavascriptCallback(
147 *callback_id, 136 *callback_id,
148 base::FundamentalValue(EasyUnlockService::Get(profile_)->IsEnabled())); 137 base::FundamentalValue(EasyUnlockService::Get(profile_)->IsEnabled()));
149 } 138 }
150 139
151 void EasyUnlockSettingsHandler::HandleStartTurnOnFlow( 140 void EasyUnlockSettingsHandler::HandleStartTurnOnFlow(
(...skipping 15 matching lines...) Expand all
167 EasyUnlockService::Get(profile_)->RunTurnOffFlow(); 156 EasyUnlockService::Get(profile_)->RunTurnOffFlow();
168 } 157 }
169 158
170 void EasyUnlockSettingsHandler::HandleCancelTurnOffFlow( 159 void EasyUnlockSettingsHandler::HandleCancelTurnOffFlow(
171 const base::ListValue* args) { 160 const base::ListValue* args) {
172 EasyUnlockService::Get(profile_)->ResetTurnOffFlow(); 161 EasyUnlockService::Get(profile_)->ResetTurnOffFlow();
173 } 162 }
174 163
175 } // namespace settings 164 } // namespace settings
176 } // namespace chromeos 165 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698