OLD | NEW |
---|---|
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/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
16 #include "chrome/grit/generated_resources.h" | 17 #include "chrome/grit/generated_resources.h" |
17 #include "components/proximity_auth/switches.h" | 18 #include "components/proximity_auth/switches.h" |
18 #include "content/public/browser/web_ui.h" | 19 #include "content/public/browser/web_ui.h" |
19 #include "content/public/browser/web_ui_data_source.h" | 20 #include "content/public/browser/web_ui_data_source.h" |
20 | 21 |
21 namespace chromeos { | 22 namespace chromeos { |
22 namespace settings { | 23 namespace settings { |
23 | 24 |
24 EasyUnlockSettingsHandler::EasyUnlockSettingsHandler(Profile* profile) | 25 EasyUnlockSettingsHandler::EasyUnlockSettingsHandler(Profile* profile) |
25 : profile_(profile) { | 26 : profile_(profile) { |
26 EasyUnlockService::Get(profile)->AddObserver(this); | 27 EasyUnlockService::Get(profile)->AddObserver(this); |
28 | |
29 profile_pref_registrar_.Init(profile->GetPrefs()); | |
30 profile_pref_registrar_.Add( | |
dpapad
2016/03/15 17:49:40
Based on our WebUI initialization pitfalls discuss
tommycli
2016/03/15 18:58:27
Done.
| |
31 prefs::kEasyUnlockPairing, | |
32 base::Bind(&EasyUnlockSettingsHandler::SendEnabledStatus, | |
33 base::Unretained(this))); | |
27 } | 34 } |
28 | 35 |
29 EasyUnlockSettingsHandler::~EasyUnlockSettingsHandler() { | 36 EasyUnlockSettingsHandler::~EasyUnlockSettingsHandler() { |
30 EasyUnlockService::Get(profile_)->RemoveObserver(this); | 37 EasyUnlockService::Get(profile_)->RemoveObserver(this); |
31 } | 38 } |
32 | 39 |
33 EasyUnlockSettingsHandler* EasyUnlockSettingsHandler::Create( | 40 EasyUnlockSettingsHandler* EasyUnlockSettingsHandler::Create( |
34 content::WebUIDataSource* html_source, | 41 content::WebUIDataSource* html_source, |
35 Profile* profile) { | 42 Profile* profile) { |
36 bool allowed = EasyUnlockService::Get(profile)->IsAllowed(); | 43 EasyUnlockService* easy_unlock_service = EasyUnlockService::Get(profile); |
44 bool allowed = easy_unlock_service->IsAllowed(); | |
37 html_source->AddBoolean("easyUnlockAllowed", allowed); | 45 html_source->AddBoolean("easyUnlockAllowed", allowed); |
38 if (!allowed) | 46 if (!allowed) |
39 return nullptr; | 47 return nullptr; |
40 | 48 |
49 html_source->AddBoolean("easyUnlockEnabled", | |
50 easy_unlock_service->IsEnabled()); | |
41 html_source->AddString("easyUnlockLearnMoreURL", | 51 html_source->AddString("easyUnlockLearnMoreURL", |
dpapad
2016/03/15 17:49:40
Can we move "easyUnlockLearnMoreURL" registration
tommycli
2016/03/15 18:58:27
Done.
| |
42 chrome::kEasyUnlockLearnMoreUrl); | 52 chrome::kEasyUnlockLearnMoreUrl); |
43 html_source->AddBoolean( | 53 html_source->AddBoolean( |
44 "easyUnlockProximityDetectionAllowed", | 54 "easyUnlockProximityDetectionAllowed", |
45 base::CommandLine::ForCurrentProcess()->HasSwitch( | 55 base::CommandLine::ForCurrentProcess()->HasSwitch( |
46 proximity_auth::switches::kEnableProximityDetection)); | 56 proximity_auth::switches::kEnableProximityDetection)); |
47 | 57 |
48 return new EasyUnlockSettingsHandler(profile); | 58 return new EasyUnlockSettingsHandler(profile); |
49 } | 59 } |
50 | 60 |
51 void EasyUnlockSettingsHandler::RegisterMessages() { | 61 void EasyUnlockSettingsHandler::RegisterMessages() { |
52 web_ui()->RegisterMessageCallback( | 62 web_ui()->RegisterMessageCallback( |
63 "easyUnlockGetEnabledStatus", | |
64 base::Bind(&EasyUnlockSettingsHandler::HandleGetEnabledStatus, | |
65 base::Unretained(this))); | |
66 web_ui()->RegisterMessageCallback( | |
53 "easyUnlockGetTurnOffFlowStatus", | 67 "easyUnlockGetTurnOffFlowStatus", |
54 base::Bind(&EasyUnlockSettingsHandler::HandleGetTurnOffFlowStatus, | 68 base::Bind(&EasyUnlockSettingsHandler::HandleGetTurnOffFlowStatus, |
55 base::Unretained(this))); | 69 base::Unretained(this))); |
56 web_ui()->RegisterMessageCallback( | 70 web_ui()->RegisterMessageCallback( |
57 "easyUnlockRequestTurnOff", | 71 "easyUnlockRequestTurnOff", |
58 base::Bind(&EasyUnlockSettingsHandler::HandleRequestTurnOff, | 72 base::Bind(&EasyUnlockSettingsHandler::HandleRequestTurnOff, |
59 base::Unretained(this))); | 73 base::Unretained(this))); |
60 web_ui()->RegisterMessageCallback( | 74 web_ui()->RegisterMessageCallback( |
61 "easyUnlockTurnOffOverlayDismissed", | 75 "easyUnlockTurnOffOverlayDismissed", |
62 base::Bind(&EasyUnlockSettingsHandler::HandlePageDismissed, | 76 base::Bind(&EasyUnlockSettingsHandler::HandlePageDismissed, |
63 base::Unretained(this))); | 77 base::Unretained(this))); |
64 } | 78 } |
65 | 79 |
66 void EasyUnlockSettingsHandler::OnTurnOffOperationStatusChanged() { | 80 void EasyUnlockSettingsHandler::OnTurnOffOperationStatusChanged() { |
67 SendTurnOffOperationStatus(); | 81 SendTurnOffOperationStatus(); |
68 } | 82 } |
69 | 83 |
84 void EasyUnlockSettingsHandler::SendEnabledStatus() { | |
85 web_ui()->CallJavascriptFunction( | |
86 "cr.webUIListenerCallback", | |
87 base::StringValue("easy-unlock-enabled-status"), | |
88 base::FundamentalValue(EasyUnlockService::Get(profile_)->IsEnabled())); | |
89 } | |
90 | |
70 void EasyUnlockSettingsHandler::SendTurnOffOperationStatus() { | 91 void EasyUnlockSettingsHandler::SendTurnOffOperationStatus() { |
71 EasyUnlockService::TurnOffFlowStatus status = | 92 EasyUnlockService::TurnOffFlowStatus status = |
72 EasyUnlockService::Get(profile_)->GetTurnOffFlowStatus(); | 93 EasyUnlockService::Get(profile_)->GetTurnOffFlowStatus(); |
73 | 94 |
74 // Translate status into JS UI state string. Note the translated string | 95 // Translate status into JS UI state string. Note the translated string |
75 // should match UIState defined in easy_unlock_turn_off_overlay.js. | 96 // should match UIState defined in easy_unlock_turn_off_overlay.js. |
76 std::string status_string; | 97 std::string status_string; |
77 switch (status) { | 98 switch (status) { |
78 case EasyUnlockService::IDLE: | 99 case EasyUnlockService::IDLE: |
79 status_string = "idle"; | 100 status_string = "idle"; |
80 break; | 101 break; |
81 case EasyUnlockService::PENDING: | 102 case EasyUnlockService::PENDING: |
82 status_string = "pending"; | 103 status_string = "pending"; |
83 break; | 104 break; |
84 case EasyUnlockService::FAIL: | 105 case EasyUnlockService::FAIL: |
85 status_string = "server-error"; | 106 status_string = "server-error"; |
86 break; | 107 break; |
87 default: | 108 default: |
88 LOG(ERROR) << "Unknown Easy unlock turn-off operation status: " << status; | 109 LOG(ERROR) << "Unknown Easy unlock turn-off operation status: " << status; |
89 status_string = "idle"; | 110 status_string = "idle"; |
90 break; | 111 break; |
91 } | 112 } |
92 | 113 |
93 web_ui()->CallJavascriptFunction( | 114 web_ui()->CallJavascriptFunction( |
94 "cr.webUIListenerCallback", | 115 "cr.webUIListenerCallback", |
95 base::StringValue("easy-unlock-turn-off-flow-status"), | 116 base::StringValue("easy-unlock-turn-off-flow-status"), |
96 base::StringValue(status_string)); | 117 base::StringValue(status_string)); |
97 } | 118 } |
98 | 119 |
120 void EasyUnlockSettingsHandler::HandleGetEnabledStatus( | |
121 const base::ListValue* args) { | |
122 CHECK_EQ(1U, args->GetSize()); | |
123 const base::Value* callback_id; | |
124 CHECK(args->Get(0, &callback_id)); | |
125 ResolveJavascriptCallback( | |
126 *callback_id, | |
127 base::FundamentalValue(EasyUnlockService::Get(profile_)->IsEnabled())); | |
128 } | |
129 | |
99 void EasyUnlockSettingsHandler::HandleGetTurnOffFlowStatus( | 130 void EasyUnlockSettingsHandler::HandleGetTurnOffFlowStatus( |
100 const base::ListValue* args) { | 131 const base::ListValue* args) { |
101 SendTurnOffOperationStatus(); | 132 SendTurnOffOperationStatus(); |
102 } | 133 } |
103 | 134 |
104 void EasyUnlockSettingsHandler::HandleRequestTurnOff( | 135 void EasyUnlockSettingsHandler::HandleRequestTurnOff( |
105 const base::ListValue* args) { | 136 const base::ListValue* args) { |
106 EasyUnlockService::Get(profile_)->RunTurnOffFlow(); | 137 EasyUnlockService::Get(profile_)->RunTurnOffFlow(); |
107 } | 138 } |
108 | 139 |
109 void EasyUnlockSettingsHandler::HandlePageDismissed( | 140 void EasyUnlockSettingsHandler::HandlePageDismissed( |
110 const base::ListValue* args) { | 141 const base::ListValue* args) { |
111 EasyUnlockService::Get(profile_)->ResetTurnOffFlow(); | 142 EasyUnlockService::Get(profile_)->ResetTurnOffFlow(); |
112 } | 143 } |
113 | 144 |
114 } // namespace settings | 145 } // namespace settings |
115 } // namespace chromeos | 146 } // namespace chromeos |
OLD | NEW |