Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ Future<bool> surveyActionTaken() async {
/// Requires [setActiveSurvey] to have been called prior to calling this method.
Future<void> setSurveyActionTaken() async {
if (isDevToolsServerAvailable) {
final resp = await request(
'${SurveyApi.setSurveyActionTaken}'
'?$apiParameterValueKey=true',
);
final resp = await request(SurveyApi.setSurveyActionTaken);
if (resp == null || !resp.statusOk || !(json.decode(resp.body) as bool)) {
logWarning(resp, SurveyApi.setSurveyActionTaken);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools_extensions/lib/src/api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ enum DevToolsExtensionEventType {

/// An event that an extension can send to DevTools asking DevTools to copy
/// some content to the user's clipboard.
///
///
/// It is preferred that extensions send this event to DevTools to copy text
/// instead of calling `Clipboard.setData` directly because DevTools contains
/// additional logic for copying text from within an IDE-embedded web view.
Expand Down
6 changes: 3 additions & 3 deletions packages/devtools_shared/lib/src/devtools_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ const apiGetSurveyActionTaken = SurveyApi.getSurveyActionTaken;
const apiSetSurveyActionTaken = SurveyApi.setSurveyActionTaken;

@Deprecated(
'Use apiParameterValueKey for the query parameter of the '
'SurveyApi.setSurveyActionTaken request instead. '
'This query parameter is no longer required for the '
'SurveyApi.setSurveyActionTaken request. '
'This field will be removed in devtools_shared >= 11.0.0.',
)
const surveyActionTakenPropertyName = apiParameterValueKey;
const surveyActionTakenPropertyName = 'surveyActionTaken';

@Deprecated(
'Use SurveyApi.getSurveyShownCount instead. '
Expand Down
83 changes: 83 additions & 0 deletions packages/devtools_shared/lib/src/server/handlers/_survey.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2024 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: avoid_classes_with_only_static_members

part of '../server_api.dart';

abstract class _SurveyHandler {
static shelf.Response setActiveSurvey(
ServerApi api,
Map<String, String> queryParams,
DevToolsUsage devToolsStore,
) {
final missingRequiredParams = ServerApi._checkRequiredParameters(
[apiParameterValueKey],
queryParams: queryParams,
api: api,
requestName: ReleaseNotesApi.setLastReleaseNotesVersion,
);
if (missingRequiredParams != null) return missingRequiredParams;

final surveyName = queryParams[apiParameterValueKey]!;
devToolsStore.activeSurvey = surveyName;
return ServerApi._encodeResponse(true, api: api);
}

static shelf.Response getSurveyActionTaken(
ServerApi api,
DevToolsUsage devToolsStore,
) {
final activeSurveySet = _checkActiveSurveySet(api, devToolsStore);
if (activeSurveySet != null) return activeSurveySet;

return ServerApi._encodeResponse(devToolsStore.surveyActionTaken, api: api);
}

static shelf.Response setSurveyActionTaken(
ServerApi api,
DevToolsUsage devToolsStore,
) {
final activeSurveySet = _checkActiveSurveySet(api, devToolsStore);
if (activeSurveySet != null) return activeSurveySet;

devToolsStore.surveyActionTaken = true;
return ServerApi._encodeResponse(devToolsStore.surveyActionTaken, api: api);
}

static shelf.Response getSurveyShownCount(
ServerApi api,
DevToolsUsage devToolsStore,
) {
final activeSurveySet = _checkActiveSurveySet(api, devToolsStore);
if (activeSurveySet != null) return activeSurveySet;

return ServerApi._encodeResponse(devToolsStore.surveyShownCount, api: api);
}

static shelf.Response incrementSurveyShownCount(
ServerApi api,
DevToolsUsage devToolsStore,
) {
final activeSurveySet = _checkActiveSurveySet(api, devToolsStore);
if (activeSurveySet != null) return activeSurveySet;

devToolsStore.incrementSurveyShownCount();
return ServerApi._encodeResponse(devToolsStore.surveyShownCount, api: api);
}

static const _errorNoActiveSurvey = 'ERROR: setActiveSurvey not called.';

static shelf.Response? _checkActiveSurveySet(
ServerApi api,
DevToolsUsage devToolsStore,
) {
return devToolsStore.activeSurvey == null
? api.badRequest(
'$_errorNoActiveSurvey '
'- ${SurveyApi.getSurveyActionTaken}',
)
: null;
}
}
75 changes: 11 additions & 64 deletions packages/devtools_shared/lib/src/server/server_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ part 'handlers/_dtd.dart';
part 'handlers/_general.dart';
part 'handlers/_release_notes.dart';
part 'handlers/_storage.dart';
part 'handlers/_survey.dart';

/// The DevTools server API.
///
/// This defines endpoints that serve all requests that come in over api/.
class ServerApi {
static const logsKey = 'logs';
static const errorKey = 'error';
static const errorNoActiveSurvey = 'ERROR: setActiveSurvey not called.';

/// Determines whether or not [request] is an API call.
static bool canHandle(shelf.Request request) {
Expand Down Expand Up @@ -113,75 +113,22 @@ class ServerApi {
}
return _encodeResponse(_devToolsStore.analyticsEnabled, api: api);

// TODO(kenz): move all the handlers into a separate handler class as a
// follow up PR to preserve the diff.
// ----- DevTools survey store. -----
// ----- DevTools survey api. -----

case SurveyApi.setActiveSurvey:
// Assume failure.
bool result = false;

// Set the active survey used to store subsequent apiGetSurveyActionTaken,
// apiSetSurveyActionTaken, apiGetSurveyShownCount, and
// apiIncrementSurveyShownCount calls.
if (queryParams.keys.length == 1 &&
queryParams.containsKey(apiParameterValueKey)) {
final surveyName = queryParams[apiParameterValueKey]!;

// Set the current activeSurvey.
_devToolsStore.activeSurvey = surveyName;
result = true;
}
return _encodeResponse(result, api: api);
return _SurveyHandler.setActiveSurvey(api, queryParams, _devToolsStore);

case SurveyApi.getSurveyActionTaken:
// Request setActiveSurvey has not been requested.
if (_devToolsStore.activeSurvey == null) {
return api.badRequest(
'$errorNoActiveSurvey '
'- ${SurveyApi.getSurveyActionTaken}',
);
}
// SurveyActionTaken has the survey been acted upon (taken or dismissed)
return _encodeResponse(_devToolsStore.surveyActionTaken, api: api);
// TODO(terry): remove the query param logic for this request.
// setSurveyActionTaken should only be called with the value of true, so
// we can remove the extra complexity.
return _SurveyHandler.getSurveyActionTaken(api, _devToolsStore);

case SurveyApi.setSurveyActionTaken:
// Request setActiveSurvey has not been requested.
if (_devToolsStore.activeSurvey == null) {
return api.badRequest(
'$errorNoActiveSurvey '
'- ${SurveyApi.setSurveyActionTaken}',
);
}
// Set the SurveyActionTaken.
// Has the survey been taken or dismissed..
if (queryParams.containsKey(apiParameterValueKey)) {
_devToolsStore.surveyActionTaken =
json.decode(queryParams[apiParameterValueKey]!);
}
return _encodeResponse(_devToolsStore.surveyActionTaken, api: api);
return _SurveyHandler.setSurveyActionTaken(api, _devToolsStore);

case SurveyApi.getSurveyShownCount:
// Request setActiveSurvey has not been requested.
if (_devToolsStore.activeSurvey == null) {
return api.badRequest(
'$errorNoActiveSurvey '
'- ${SurveyApi.getSurveyShownCount}',
);
}
// SurveyShownCount how many times have we asked to take survey.
return _encodeResponse(_devToolsStore.surveyShownCount, api: api);
return _SurveyHandler.getSurveyShownCount(api, _devToolsStore);

case SurveyApi.incrementSurveyShownCount:
// Request setActiveSurvey has not been requested.
if (_devToolsStore.activeSurvey == null) {
return api.badRequest(
'$errorNoActiveSurvey '
'- ${SurveyApi.incrementSurveyShownCount}',
);
}
// Increment the SurveyShownCount, we've asked about the survey.
_devToolsStore.incrementSurveyShownCount();
return _encodeResponse(_devToolsStore.surveyShownCount, api: api);
return _SurveyHandler.incrementSurveyShownCount(api, _devToolsStore);

// ----- Release notes api. -----

Expand Down