Commit e4d20a31 authored by hidehiko's avatar hidehiko Committed by Commit bot

Remove controller concept from the ARC support extension.

Currently, the responsibility of the ARC auth code
control flow was distributed across ArcAuthService
ArcSupportHost and background.js etc. This CL removes
it from background.js, as a preparation to move all
of them into a manageable form.

Now, background.js has two types of messages. 1) Action,
which is sent from the native code, to do something in
extension, and 2) Event to notify to the native code
that something (e.g., user clieck etc.) is happend in the
extension

BUG=b/31079732
TEST=Ran on test device. Ran tests.
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2388763002
Cr-Commit-Position: refs/heads/master@{#422734}
parent 749309fd
...@@ -29,34 +29,52 @@ ...@@ -29,34 +29,52 @@
#include "ui/display/screen.h" #include "ui/display/screen.h"
namespace { namespace {
const char kAction[] = "action"; constexpr char kAction[] = "action";
const char kArcManaged[] = "arcManaged"; constexpr char kArcManaged[] = "arcManaged";
const char kCanEnable[] = "canEnable"; constexpr char kCanEnable[] = "canEnable";
const char kCode[] = "code"; constexpr char kData[] = "data";
const char kData[] = "data"; constexpr char kDeviceId[] = "deviceId";
const char kDeviceId[] = "deviceId"; constexpr char kEnabled[] = "enabled";
const char kEnabled[] = "enabled"; constexpr char kManaged[] = "managed";
const char kManaged[] = "managed"; constexpr char kOn[] = "on";
const char kOn[] = "on"; constexpr char kPage[] = "page";
const char kPage[] = "page"; constexpr char kStatus[] = "status";
const char kStatus[] = "status"; constexpr char kText[] = "text";
const char kText[] = "text"; constexpr char kActionInitialize[] = "initialize";
const char kActionInitialize[] = "initialize"; constexpr char kActionSetMetricsMode[] = "setMetricsMode";
const char kActionSetMetricsMode[] = "setMetricsMode"; constexpr char kActionBackupAndRestoreMode[] = "setBackupAndRestoreMode";
const char kActionBackupAndRestoreMode[] = "setBackupAndRestoreMode"; constexpr char kActionLocationServiceMode[] = "setLocationServiceMode";
const char kActionLocationServiceMode[] = "setLocationServiceMode"; constexpr char kActionSetWindowBounds[] = "setWindowBounds";
const char kActionSetWindowBounds[] = "setWindowBounds"; constexpr char kActionCloseWindow[] = "closeWindow";
const char kActionStartLso[] = "startLso"; constexpr char kActionShowPage[] = "showPage";
const char kActionSetAuthCode[] = "setAuthCode";
const char kActionEnableMetrics[] = "enableMetrics"; // The JSON data sent from the extension should have at least "event" field.
const char kActionSendFeedback[] = "sendFeedback"; // Each event data is defined below.
const char kActionSetBackupRestore[] = "setBackupRestore"; // The key of the event type.
const char kActionSetLocationService[] = "setLocationService"; constexpr char kEvent[] = "event";
const char kActionCloseWindow[] = "closeWindow";
const char kActionShowPage[] = "showPage"; // "onWindowClosed" is fired when the extension window is closed.
// No data will be provided.
// Fired when the extension window is closed. constexpr char kEventOnWindowClosed[] = "onWindowClosed";
const char kActionOnWindowClosed[] = "onWindowClosed";
// "onAuthSucceeded" is fired when successfully done to LSO authorization in
// extension.
// The auth token is passed via "code" field.
constexpr char kEventOnAuthSuccedded[] = "onAuthSucceeded";
constexpr char kCode[] = "code";
// "onAgree" is fired when a user clicks "Agree" button.
// The message should have the following three fields:
// - isMetricsEnabled
// - isBackupRestoreEnabled
// - isLocationServiceEnabled
constexpr char kEventOnAgreed[] = "onAgreed";
constexpr char kIsMetricsEnabled[] = "isMetricsEnabled";
constexpr char kIsBackupRestoreEnabled[] = "isBackupRestoreEnabled";
constexpr char kIsLocationServiceEnabled[] = "isLocationServiceEnabled";
// "onSendFeedbackClicked" is fired when a user clicks "Send Feedback" button.
constexpr char kEventOnSendFeedbackClicked[] = "onSendFeedbackClicked";
} // namespace } // namespace
...@@ -347,60 +365,55 @@ void ArcSupportHost::EnableLocationService(bool is_enabled) { ...@@ -347,60 +365,55 @@ void ArcSupportHost::EnableLocationService(bool is_enabled) {
pref_service->SetBoolean(prefs::kArcLocationServiceEnabled, is_enabled); pref_service->SetBoolean(prefs::kArcLocationServiceEnabled, is_enabled);
} }
void ArcSupportHost::OnMessage(const std::string& request_string) { void ArcSupportHost::OnMessage(const std::string& message_string) {
std::unique_ptr<base::Value> request_value = std::unique_ptr<base::Value> message_value =
base::JSONReader::Read(request_string); base::JSONReader::Read(message_string);
base::DictionaryValue* request; base::DictionaryValue* message;
if (!request_value || !request_value->GetAsDictionary(&request)) { if (!message_value || !message_value->GetAsDictionary(&message)) {
NOTREACHED(); NOTREACHED();
return; return;
} }
std::string action; std::string event;
if (!request->GetString(kAction, &action)) { if (!message->GetString(kEvent, &event)) {
NOTREACHED(); NOTREACHED();
return; return;
} }
// TODO(hidehiko): Replace by Observer.
arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
DCHECK(arc_auth_service); DCHECK(arc_auth_service);
if (action == kActionStartLso) { if (event == kEventOnWindowClosed) {
arc_auth_service->StartLso();
} else if (action == kActionSetAuthCode) {
std::string code;
if (!request->GetString(kCode, &code)) {
NOTREACHED();
return;
}
arc_auth_service->SetAuthCodeAndStartArc(code);
} else if (action == kActionOnWindowClosed) {
if (!close_requested_) if (!close_requested_)
arc_auth_service->CancelAuthCode(); arc_auth_service->CancelAuthCode();
} else if (action == kActionEnableMetrics) { } else if (event == kEventOnAuthSuccedded) {
bool is_enabled; std::string code;
if (!request->GetBoolean(kEnabled, &is_enabled)) { if (message->GetString(kCode, &code)) {
NOTREACHED(); arc_auth_service->SetAuthCodeAndStartArc(code);
return; } else {
}
EnableMetrics(is_enabled);
} else if (action == kActionSendFeedback) {
chrome::OpenFeedbackDialog(nullptr);
} else if (action == kActionSetBackupRestore) {
bool is_enabled;
if (!request->GetBoolean(kEnabled, &is_enabled)) {
NOTREACHED(); NOTREACHED();
return;
} }
EnableBackupRestore(is_enabled); } else if (event == kEventOnAgreed) {
} else if (action == kActionSetLocationService) { bool is_metrics_enabled;
bool is_enabled; bool is_backup_restore_enabled;
if (!request->GetBoolean(kEnabled, &is_enabled)) { bool is_location_service_enabled;
if (message->GetBoolean(kIsMetricsEnabled, &is_metrics_enabled) &&
message->GetBoolean(kIsBackupRestoreEnabled,
&is_backup_restore_enabled) &&
message->GetBoolean(kIsLocationServiceEnabled,
&is_location_service_enabled)) {
EnableMetrics(is_metrics_enabled);
EnableBackupRestore(is_backup_restore_enabled);
EnableLocationService(is_location_service_enabled);
arc_auth_service->StartLso();
} else {
NOTREACHED(); NOTREACHED();
return;
} }
EnableLocationService(is_enabled); } else if (event == kEventOnSendFeedbackClicked) {
chrome::OpenFeedbackDialog(nullptr);
} else { } else {
LOG(ERROR) << "Unknown message: " << message_string;
NOTREACHED(); NOTREACHED();
} }
} }
......
...@@ -79,11 +79,11 @@ var INNER_HEIGHT = 688; ...@@ -79,11 +79,11 @@ var INNER_HEIGHT = 688;
/** /**
* Sends a native message to ArcSupportHost. * Sends a native message to ArcSupportHost.
* @param {string} code The action code in message. * @param {string} event The event type in message.
* @param {Object=} opt_Props Extra properties for the message. * @param {Object=} opt_props Extra properties for the message.
*/ */
function sendNativeMessage(code, opt_Props) { function sendNativeMessage(event, opt_props) {
var message = Object.assign({'action': code}, opt_Props); var message = Object.assign({'event': event}, opt_props);
port.postMessage(message); port.postMessage(message);
} }
...@@ -511,7 +511,7 @@ chrome.app.runtime.onLaunched.addListener(function() { ...@@ -511,7 +511,7 @@ chrome.app.runtime.onLaunched.addListener(function() {
if (results && results.length == 1 && typeof results[0] == 'string' && if (results && results.length == 1 && typeof results[0] == 'string' &&
results[0].substring(0, authCodePrefix.length) == authCodePrefix) { results[0].substring(0, authCodePrefix.length) == authCodePrefix) {
var authCode = results[0].substring(authCodePrefix.length); var authCode = results[0].substring(authCodePrefix.length);
sendNativeMessage('setAuthCode', {code: authCode}); sendNativeMessage('onAuthSucceeded', {code: authCode});
} else { } else {
setErrorMessage(appWindow.contentWindow.loadTimeData.getString( setErrorMessage(appWindow.contentWindow.loadTimeData.getString(
'authorizationFailed')); 'authorizationFailed'));
...@@ -572,23 +572,13 @@ chrome.app.runtime.onLaunched.addListener(function() { ...@@ -572,23 +572,13 @@ chrome.app.runtime.onLaunched.addListener(function() {
termsAccepted = true; termsAccepted = true;
var enableMetrics = doc.getElementById('enable-metrics'); var enableMetrics = doc.getElementById('enable-metrics');
if (!enableMetrics.hidden) {
sendNativeMessage('enableMetrics', {
'enabled': enableMetrics.checked
});
}
var enableBackupRestore = doc.getElementById('enable-backup-restore'); var enableBackupRestore = doc.getElementById('enable-backup-restore');
sendNativeMessage('setBackupRestore', {
'enabled': enableBackupRestore.checked
});
var enableLocationService = doc.getElementById('enable-location-service'); var enableLocationService = doc.getElementById('enable-location-service');
sendNativeMessage('setLocationService', { sendNativeMessage('onAgreed', {
'enabled': enableLocationService.checked isMetricsEnabled: !enableMetrics.hidden && enableMetrics.checked,
isBackupRestoreEnabled: enableBackupRestore.checked,
isLocationServiceEnabled: enableLocationService.checked
}); });
sendNativeMessage('startLso');
}; };
var onCancel = function() { var onCancel = function() {
...@@ -599,14 +589,16 @@ chrome.app.runtime.onLaunched.addListener(function() { ...@@ -599,14 +589,16 @@ chrome.app.runtime.onLaunched.addListener(function() {
var onRetry = function() { var onRetry = function() {
if (termsAccepted) { if (termsAccepted) {
sendNativeMessage('startLso'); // Reuse the onAgree() in case that the user has already accepted
// the ToS.
onAgree();
} else { } else {
loadInitialTerms(); loadInitialTerms();
} }
}; };
var onSendFeedback = function() { var onSendFeedback = function() {
sendNativeMessage('sendFeedback'); sendNativeMessage('onSendFeedbackClicked');
}; };
doc.getElementById('button-agree').addEventListener('click', onAgree); doc.getElementById('button-agree').addEventListener('click', onAgree);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment