tablet_mode.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/public/cpp/tablet_mode.h"
  5. #include "ash/constants/ash_switches.h"
  6. #include "base/check_op.h"
  7. #include "base/command_line.h"
  8. namespace ash {
  9. namespace {
  10. TabletMode* g_instance = nullptr;
  11. }
  12. // static
  13. bool TabletMode::IsBoardTypeMarkedAsTabletCapable() {
  14. return base::CommandLine::ForCurrentProcess()->HasSwitch(
  15. switches::kAshEnableTabletMode);
  16. }
  17. TabletMode* TabletMode::Get() {
  18. return g_instance;
  19. }
  20. TabletMode::TabletMode() {
  21. DCHECK_EQ(nullptr, g_instance);
  22. g_instance = this;
  23. }
  24. TabletMode::~TabletMode() {
  25. DCHECK_EQ(this, g_instance);
  26. g_instance = nullptr;
  27. }
  28. TabletMode::Waiter::Waiter(bool enable)
  29. : enable_(enable), run_loop_(base::RunLoop::Type::kNestableTasksAllowed) {
  30. if (TabletMode::Get()->InTabletMode() == enable_)
  31. run_loop_.Quit();
  32. else
  33. TabletMode::Get()->AddObserver(this);
  34. }
  35. TabletMode::Waiter::~Waiter() {
  36. TabletMode::Get()->RemoveObserver(this);
  37. }
  38. void TabletMode::Waiter::Wait() {
  39. run_loop_.Run();
  40. }
  41. void TabletMode::Waiter::OnTabletModeStarted() {
  42. if (enable_)
  43. run_loop_.QuitWhenIdle();
  44. }
  45. void TabletMode::Waiter::OnTabletModeEnded() {
  46. if (!enable_)
  47. run_loop_.QuitWhenIdle();
  48. }
  49. bool TabletMode::IsInTabletMode() {
  50. const TabletMode* singleton = TabletMode::Get();
  51. return singleton && singleton->InTabletMode();
  52. }
  53. } // namespace ash