1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74

ash / wm / window_restore / informed_restore_context_menu_model.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/wm/window_restore/informed_restore_context_menu_model.h"

#include "ash/constants/ash_pref_names.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/wm/window_restore/window_restore_util.h"
#include "base/notreached.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"

namespace ash {

InformedRestoreContextMenuModel::InformedRestoreContextMenuModel()
    : ui::SimpleMenuModel(this) {
  const int group = 0;
  AddTitleWithStringId(IDS_ASH_INFORMED_RESTORE_DIALOG_TITLE);
  AddRadioItemWithStringId(
      static_cast<int>(full_restore::RestoreOption::kAskEveryTime),
      IDS_ASH_INFORMED_RESTORE_DIALOG_CONTEXT_MENU_ASK_OPTION, group);
  AddRadioItemWithStringId(
      static_cast<int>(full_restore::RestoreOption::kAlways),
      IDS_ASH_INFORMED_RESTORE_DIALOG_CONTEXT_MENU_ALWAYS_OPTION, group);
  AddRadioItemWithStringId(
      static_cast<int>(full_restore::RestoreOption::kDoNotRestore),
      IDS_ASH_INFORMED_RESTORE_DIALOG_CONTEXT_MENU_NEVER_OPTION, group);
  AddSeparator(ui::MenuSeparatorType::NORMAL_SEPARATOR);
}

InformedRestoreContextMenuModel::~InformedRestoreContextMenuModel() = default;

bool InformedRestoreContextMenuModel::IsCommandIdChecked(int command_id) const {
  CHECK_GE(static_cast<int>(full_restore::RestoreOption::kMaxValue),
           command_id);

  PrefService* pref_service =
      Shell::Get()->session_controller()->GetActivePrefService();
  CHECK(pref_service);

  // Get the current restore behavior from preferences and check it against the
  // command id.
  const int restore_behavior =
      pref_service->GetInteger(prefs::kRestoreAppsAndPagesPrefName);
  switch (static_cast<full_restore::RestoreOption>(restore_behavior)) {
    case full_restore::RestoreOption::kAskEveryTime:
    case full_restore::RestoreOption::kAlways:
    case full_restore::RestoreOption::kDoNotRestore:
      return command_id == restore_behavior;
  }
}

void InformedRestoreContextMenuModel::ExecuteCommand(int command_id,
                                                     int event_flags) {
  CHECK_GE(static_cast<int>(full_restore::RestoreOption::kMaxValue),
           command_id);

  switch (static_cast<full_restore::RestoreOption>(command_id)) {
    case full_restore::RestoreOption::kAskEveryTime:
    case full_restore::RestoreOption::kAlways:
    case full_restore::RestoreOption::kDoNotRestore:
      // Set the restore behavior in preferences.
      PrefService* pref_service =
          Shell::Get()->session_controller()->GetActivePrefService();
      CHECK(pref_service);
      pref_service->SetInteger(prefs::kRestoreAppsAndPagesPrefName, command_id);
      break;
  }
}

}  // namespace ash