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 @@
#include "ui/display/screen.h"
namespace {
const char kAction[] = "action";
const char kArcManaged[] = "arcManaged";
const char kCanEnable[] = "canEnable";
const char kCode[] = "code";
const char kData[] = "data";
const char kDeviceId[] = "deviceId";
const char kEnabled[] = "enabled";
const char kManaged[] = "managed";
const char kOn[] = "on";
const char kPage[] = "page";
const char kStatus[] = "status";
const char kText[] = "text";
const char kActionInitialize[] = "initialize";
const char kActionSetMetricsMode[] = "setMetricsMode";
const char kActionBackupAndRestoreMode[] = "setBackupAndRestoreMode";
const char kActionLocationServiceMode[] = "setLocationServiceMode";
const char kActionSetWindowBounds[] = "setWindowBounds";
const char kActionStartLso[] = "startLso";
const char kActionSetAuthCode[] = "setAuthCode";
const char kActionEnableMetrics[] = "enableMetrics";
const char kActionSendFeedback[] = "sendFeedback";
const char kActionSetBackupRestore[] = "setBackupRestore";
const char kActionSetLocationService[] = "setLocationService";
const char kActionCloseWindow[] = "closeWindow";
const char kActionShowPage[] = "showPage";
// Fired when the extension window is closed.
const char kActionOnWindowClosed[] = "onWindowClosed";
constexpr char kAction[] = "action";
constexpr char kArcManaged[] = "arcManaged";
constexpr char kCanEnable[] = "canEnable";
constexpr char kData[] = "data";
constexpr char kDeviceId[] = "deviceId";
constexpr char kEnabled[] = "enabled";
constexpr char kManaged[] = "managed";
constexpr char kOn[] = "on";
constexpr char kPage[] = "page";
constexpr char kStatus[] = "status";
constexpr char kText[] = "text";
constexpr char kActionInitialize[] = "initialize";
constexpr char kActionSetMetricsMode[] = "setMetricsMode";
constexpr char kActionBackupAndRestoreMode[] = "setBackupAndRestoreMode";
constexpr char kActionLocationServiceMode[] = "setLocationServiceMode";
constexpr char kActionSetWindowBounds[] = "setWindowBounds";
constexpr char kActionCloseWindow[] = "closeWindow";
constexpr char kActionShowPage[] = "showPage";
// The JSON data sent from the extension should have at least "event" field.
// Each event data is defined below.
// The key of the event type.
constexpr char kEvent[] = "event";
// "onWindowClosed" is fired when the extension window is closed.
// No data will be provided.
constexpr char kEventOnWindowClosed[] = "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
......@@ -347,60 +365,55 @@ void ArcSupportHost::EnableLocationService(bool is_enabled) {
pref_service->SetBoolean(prefs::kArcLocationServiceEnabled, is_enabled);
}
void ArcSupportHost::OnMessage(const std::string& request_string) {
std::unique_ptr<base::Value> request_value =
base::JSONReader::Read(request_string);
base::DictionaryValue* request;
if (!request_value || !request_value->GetAsDictionary(&request)) {
void ArcSupportHost::OnMessage(const std::string& message_string) {
std::unique_ptr<base::Value> message_value =
base::JSONReader::Read(message_string);
base::DictionaryValue* message;
if (!message_value || !message_value->GetAsDictionary(&message)) {
NOTREACHED();
return;
}
std::string action;
if (!request->GetString(kAction, &action)) {
std::string event;
if (!message->GetString(kEvent, &event)) {
NOTREACHED();
return;
}
// TODO(hidehiko): Replace by Observer.
arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get();
DCHECK(arc_auth_service);
if (action == kActionStartLso) {
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 (event == kEventOnWindowClosed) {
if (!close_requested_)
arc_auth_service->CancelAuthCode();
} else if (action == kActionEnableMetrics) {
bool is_enabled;
if (!request->GetBoolean(kEnabled, &is_enabled)) {
NOTREACHED();
return;
}
EnableMetrics(is_enabled);
} else if (action == kActionSendFeedback) {
chrome::OpenFeedbackDialog(nullptr);
} else if (action == kActionSetBackupRestore) {
bool is_enabled;
if (!request->GetBoolean(kEnabled, &is_enabled)) {
} else if (event == kEventOnAuthSuccedded) {
std::string code;
if (message->GetString(kCode, &code)) {
arc_auth_service->SetAuthCodeAndStartArc(code);
} else {
NOTREACHED();
return;
}
EnableBackupRestore(is_enabled);
} else if (action == kActionSetLocationService) {
bool is_enabled;
if (!request->GetBoolean(kEnabled, &is_enabled)) {
} else if (event == kEventOnAgreed) {
bool is_metrics_enabled;
bool is_backup_restore_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();
return;
}
EnableLocationService(is_enabled);
} else if (event == kEventOnSendFeedbackClicked) {
chrome::OpenFeedbackDialog(nullptr);
} else {
LOG(ERROR) << "Unknown message: " << message_string;
NOTREACHED();
}
}
......
......@@ -79,11 +79,11 @@ var INNER_HEIGHT = 688;
/**
* Sends a native message to ArcSupportHost.
* @param {string} code The action code in message.
* @param {Object=} opt_Props Extra properties for the message.
* @param {string} event The event type in message.
* @param {Object=} opt_props Extra properties for the message.
*/
function sendNativeMessage(code, opt_Props) {
var message = Object.assign({'action': code}, opt_Props);
function sendNativeMessage(event, opt_props) {
var message = Object.assign({'event': event}, opt_props);
port.postMessage(message);
}
......@@ -511,7 +511,7 @@ chrome.app.runtime.onLaunched.addListener(function() {
if (results && results.length == 1 && typeof results[0] == 'string' &&
results[0].substring(0, authCodePrefix.length) == authCodePrefix) {
var authCode = results[0].substring(authCodePrefix.length);
sendNativeMessage('setAuthCode', {code: authCode});
sendNativeMessage('onAuthSucceeded', {code: authCode});
} else {
setErrorMessage(appWindow.contentWindow.loadTimeData.getString(
'authorizationFailed'));
......@@ -572,23 +572,13 @@ chrome.app.runtime.onLaunched.addListener(function() {
termsAccepted = true;
var enableMetrics = doc.getElementById('enable-metrics');
if (!enableMetrics.hidden) {
sendNativeMessage('enableMetrics', {
'enabled': enableMetrics.checked
});
}
var enableBackupRestore = doc.getElementById('enable-backup-restore');
sendNativeMessage('setBackupRestore', {
'enabled': enableBackupRestore.checked
});
var enableLocationService = doc.getElementById('enable-location-service');
sendNativeMessage('setLocationService', {
'enabled': enableLocationService.checked
sendNativeMessage('onAgreed', {
isMetricsEnabled: !enableMetrics.hidden && enableMetrics.checked,
isBackupRestoreEnabled: enableBackupRestore.checked,
isLocationServiceEnabled: enableLocationService.checked
});
sendNativeMessage('startLso');
};
var onCancel = function() {
......@@ -599,14 +589,16 @@ chrome.app.runtime.onLaunched.addListener(function() {
var onRetry = function() {
if (termsAccepted) {
sendNativeMessage('startLso');
// Reuse the onAgree() in case that the user has already accepted
// the ToS.
onAgree();
} else {
loadInitialTerms();
}
};
var onSendFeedback = function() {
sendNativeMessage('sendFeedback');
sendNativeMessage('onSendFeedbackClicked');
};
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