| 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/url_constants.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) { | 25 : profile_(profile), observers_registered_(false) { |
| 26 EasyUnlockService::Get(profile)->AddObserver(this); | 26 profile_pref_registrar_.Init(profile->GetPrefs()); |
| 27 } | 27 } |
| 28 | 28 |
| 29 EasyUnlockSettingsHandler::~EasyUnlockSettingsHandler() { | 29 EasyUnlockSettingsHandler::~EasyUnlockSettingsHandler() { |
| 30 EasyUnlockService::Get(profile_)->RemoveObserver(this); | 30 if (observers_registered_) |
| 31 EasyUnlockService::Get(profile_)->RemoveObserver(this); |
| 31 } | 32 } |
| 32 | 33 |
| 33 EasyUnlockSettingsHandler* EasyUnlockSettingsHandler::Create( | 34 EasyUnlockSettingsHandler* EasyUnlockSettingsHandler::Create( |
| 34 content::WebUIDataSource* html_source, | 35 content::WebUIDataSource* html_source, |
| 35 Profile* profile) { | 36 Profile* profile) { |
| 36 bool allowed = EasyUnlockService::Get(profile)->IsAllowed(); | 37 EasyUnlockService* easy_unlock_service = EasyUnlockService::Get(profile); |
| 38 bool allowed = easy_unlock_service->IsAllowed(); |
| 37 html_source->AddBoolean("easyUnlockAllowed", allowed); | 39 html_source->AddBoolean("easyUnlockAllowed", allowed); |
| 40 html_source->AddBoolean("easyUnlockEnabled", |
| 41 allowed ? easy_unlock_service->IsEnabled() : false); |
| 38 if (!allowed) | 42 if (!allowed) |
| 39 return nullptr; | 43 return nullptr; |
| 40 | 44 |
| 41 html_source->AddString("easyUnlockLearnMoreURL", | |
| 42 chrome::kEasyUnlockLearnMoreUrl); | |
| 43 html_source->AddBoolean( | 45 html_source->AddBoolean( |
| 44 "easyUnlockProximityDetectionAllowed", | 46 "easyUnlockProximityDetectionAllowed", |
| 45 base::CommandLine::ForCurrentProcess()->HasSwitch( | 47 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 46 proximity_auth::switches::kEnableProximityDetection)); | 48 proximity_auth::switches::kEnableProximityDetection)); |
| 47 | 49 |
| 48 return new EasyUnlockSettingsHandler(profile); | 50 return new EasyUnlockSettingsHandler(profile); |
| 49 } | 51 } |
| 50 | 52 |
| 51 void EasyUnlockSettingsHandler::RegisterMessages() { | 53 void EasyUnlockSettingsHandler::RegisterMessages() { |
| 52 web_ui()->RegisterMessageCallback( | 54 web_ui()->RegisterMessageCallback( |
| 55 "easyUnlockGetEnabledStatus", |
| 56 base::Bind(&EasyUnlockSettingsHandler::HandleGetEnabledStatus, |
| 57 base::Unretained(this))); |
| 58 web_ui()->RegisterMessageCallback( |
| 53 "easyUnlockGetTurnOffFlowStatus", | 59 "easyUnlockGetTurnOffFlowStatus", |
| 54 base::Bind(&EasyUnlockSettingsHandler::HandleGetTurnOffFlowStatus, | 60 base::Bind(&EasyUnlockSettingsHandler::HandleGetTurnOffFlowStatus, |
| 55 base::Unretained(this))); | 61 base::Unretained(this))); |
| 56 web_ui()->RegisterMessageCallback( | 62 web_ui()->RegisterMessageCallback( |
| 57 "easyUnlockRequestTurnOff", | 63 "easyUnlockRequestTurnOff", |
| 58 base::Bind(&EasyUnlockSettingsHandler::HandleRequestTurnOff, | 64 base::Bind(&EasyUnlockSettingsHandler::HandleRequestTurnOff, |
| 59 base::Unretained(this))); | 65 base::Unretained(this))); |
| 60 web_ui()->RegisterMessageCallback( | 66 web_ui()->RegisterMessageCallback( |
| 61 "easyUnlockTurnOffOverlayDismissed", | 67 "easyUnlockTurnOffOverlayDismissed", |
| 62 base::Bind(&EasyUnlockSettingsHandler::HandlePageDismissed, | 68 base::Bind(&EasyUnlockSettingsHandler::HandlePageDismissed, |
| 63 base::Unretained(this))); | 69 base::Unretained(this))); |
| 64 } | 70 } |
| 65 | 71 |
| 72 void EasyUnlockSettingsHandler::RenderViewReused() { |
| 73 // When the page is reloaded, we clear our observers and re-register when |
| 74 // the new page's DOM is ready. |
| 75 if (!observers_registered_) |
| 76 return; |
| 77 |
| 78 EasyUnlockService::Get(profile_)->RemoveObserver(this); |
| 79 profile_pref_registrar_.RemoveAll(); |
| 80 |
| 81 observers_registered_ = false; |
| 82 } |
| 83 |
| 66 void EasyUnlockSettingsHandler::OnTurnOffOperationStatusChanged() { | 84 void EasyUnlockSettingsHandler::OnTurnOffOperationStatusChanged() { |
| 67 SendTurnOffOperationStatus(); | 85 SendTurnOffOperationStatus(); |
| 68 } | 86 } |
| 69 | 87 |
| 88 void EasyUnlockSettingsHandler::SendEnabledStatus() { |
| 89 web_ui()->CallJavascriptFunction( |
| 90 "cr.webUIListenerCallback", |
| 91 base::StringValue("easy-unlock-enabled-status"), |
| 92 base::FundamentalValue(EasyUnlockService::Get(profile_)->IsEnabled())); |
| 93 } |
| 94 |
| 70 void EasyUnlockSettingsHandler::SendTurnOffOperationStatus() { | 95 void EasyUnlockSettingsHandler::SendTurnOffOperationStatus() { |
| 71 EasyUnlockService::TurnOffFlowStatus status = | 96 EasyUnlockService::TurnOffFlowStatus status = |
| 72 EasyUnlockService::Get(profile_)->GetTurnOffFlowStatus(); | 97 EasyUnlockService::Get(profile_)->GetTurnOffFlowStatus(); |
| 73 | 98 |
| 74 // Translate status into JS UI state string. Note the translated string | 99 // Translate status into JS UI state string. Note the translated string |
| 75 // should match UIState defined in easy_unlock_turn_off_overlay.js. | 100 // should match UIState defined in easy_unlock_turn_off_overlay.js. |
| 76 std::string status_string; | 101 std::string status_string; |
| 77 switch (status) { | 102 switch (status) { |
| 78 case EasyUnlockService::IDLE: | 103 case EasyUnlockService::IDLE: |
| 79 status_string = "idle"; | 104 status_string = "idle"; |
| 80 break; | 105 break; |
| 81 case EasyUnlockService::PENDING: | 106 case EasyUnlockService::PENDING: |
| 82 status_string = "pending"; | 107 status_string = "pending"; |
| 83 break; | 108 break; |
| 84 case EasyUnlockService::FAIL: | 109 case EasyUnlockService::FAIL: |
| 85 status_string = "server-error"; | 110 status_string = "server-error"; |
| 86 break; | 111 break; |
| 87 default: | 112 default: |
| 88 LOG(ERROR) << "Unknown Easy unlock turn-off operation status: " << status; | 113 LOG(ERROR) << "Unknown Easy unlock turn-off operation status: " << status; |
| 89 status_string = "idle"; | 114 status_string = "idle"; |
| 90 break; | 115 break; |
| 91 } | 116 } |
| 92 | 117 |
| 93 web_ui()->CallJavascriptFunction( | 118 web_ui()->CallJavascriptFunction( |
| 94 "cr.webUIListenerCallback", | 119 "cr.webUIListenerCallback", |
| 95 base::StringValue("easy-unlock-turn-off-flow-status"), | 120 base::StringValue("easy-unlock-turn-off-flow-status"), |
| 96 base::StringValue(status_string)); | 121 base::StringValue(status_string)); |
| 97 } | 122 } |
| 98 | 123 |
| 124 void EasyUnlockSettingsHandler::HandleGetEnabledStatus( |
| 125 const base::ListValue* args) { |
| 126 // This method is called when the DOM is first ready. Therefore we initialize |
| 127 // our observers here. |
| 128 if (!observers_registered_) { |
| 129 EasyUnlockService::Get(profile_)->AddObserver(this); |
| 130 |
| 131 profile_pref_registrar_.Add( |
| 132 prefs::kEasyUnlockPairing, |
| 133 base::Bind(&EasyUnlockSettingsHandler::SendEnabledStatus, |
| 134 base::Unretained(this))); |
| 135 |
| 136 observers_registered_ = true; |
| 137 } |
| 138 |
| 139 CHECK_EQ(1U, args->GetSize()); |
| 140 const base::Value* callback_id; |
| 141 CHECK(args->Get(0, &callback_id)); |
| 142 ResolveJavascriptCallback( |
| 143 *callback_id, |
| 144 base::FundamentalValue(EasyUnlockService::Get(profile_)->IsEnabled())); |
| 145 } |
| 146 |
| 99 void EasyUnlockSettingsHandler::HandleGetTurnOffFlowStatus( | 147 void EasyUnlockSettingsHandler::HandleGetTurnOffFlowStatus( |
| 100 const base::ListValue* args) { | 148 const base::ListValue* args) { |
| 101 SendTurnOffOperationStatus(); | 149 SendTurnOffOperationStatus(); |
| 102 } | 150 } |
| 103 | 151 |
| 104 void EasyUnlockSettingsHandler::HandleRequestTurnOff( | 152 void EasyUnlockSettingsHandler::HandleRequestTurnOff( |
| 105 const base::ListValue* args) { | 153 const base::ListValue* args) { |
| 106 EasyUnlockService::Get(profile_)->RunTurnOffFlow(); | 154 EasyUnlockService::Get(profile_)->RunTurnOffFlow(); |
| 107 } | 155 } |
| 108 | 156 |
| 109 void EasyUnlockSettingsHandler::HandlePageDismissed( | 157 void EasyUnlockSettingsHandler::HandlePageDismissed( |
| 110 const base::ListValue* args) { | 158 const base::ListValue* args) { |
| 111 EasyUnlockService::Get(profile_)->ResetTurnOffFlow(); | 159 EasyUnlockService::Get(profile_)->ResetTurnOffFlow(); |
| 112 } | 160 } |
| 113 | 161 |
| 114 } // namespace settings | 162 } // namespace settings |
| 115 } // namespace chromeos | 163 } // namespace chromeos |
| OLD | NEW |