1 // Copyright (c) 2011 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.
5 #include "chrome/browser/chromeos/login/screens/screen_manager.h"
8 #include "base/logging.h"
9 #include "chrome/browser/chromeos/login/oobe_display.h"
10 #include "chrome/browser/chromeos/login/screens/base_screen.h"
11 #include "chrome/browser/chromeos/login/screens/screen_factory.h"
12 #include "chrome/browser/chromeos/login/screens/screen_flow.h"
16 ScreenManager::ScreenManager(ScreenFactory
* factory
,
17 OobeDisplay
* oobe_display
,
18 ScreenFlow
* initial_flow
)
20 display_(oobe_display
),
23 js_is_ready_
= display_
->IsJSReady(
24 base::Bind(&ScreenManager::OnDisplayIsReady
, weak_factory_
.GetWeakPtr()));
26 // TODO({antrim|ygorshenin}@): register ScreenManager as delegate in
27 // ScreenManagerHandler instance.
30 ScreenManager::~ScreenManager() {
33 void ScreenManager::WarmupScreen(const std::string
& id
,
34 ScreenContext
* context
) {
38 void ScreenManager::ShowScreen(const std::string
& id
) {
39 ShowScreenImpl(id
, new ScreenContext(), false);
42 void ScreenManager::ShowScreenWithParameters(const std::string
& id
,
43 ScreenContext
* context
) {
44 ShowScreenImpl(id
, context
, false);
47 void ScreenManager::PopupScreen(const std::string
& id
) {
48 ShowScreenImpl(id
, new ScreenContext(), true);
51 void ScreenManager::PopupScreenWithParameters(const std::string
& id
,
52 ScreenContext
* context
) {
53 ShowScreenImpl(id
, context
, true);
56 void ScreenManager::HidePopupScreen(const std::string
& screen_id
) {
57 DCHECK(!screen_stack_
.empty());
58 DCHECK_EQ(screen_stack_
.top(), screen_id
);
60 // TODO(antrim): check that id really exist in stack.
61 std::string previous_screen
= GetCurrentScreenId();
62 GetTopmostScreen()->OnHide();
63 TearDownTopmostScreen();
66 std::string screen_below
;
67 if (!screen_stack_
.empty()) {
68 screen_below
= GetCurrentScreenId();
69 GetTopmostScreen()->OnShow();
71 TransitionScreen(previous_screen
, screen_below
);
74 std::string
ScreenManager::GetCurrentScreenId() {
75 DCHECK(!screen_stack_
.empty());
76 return screen_stack_
.top();
79 void ScreenManager::SetScreenFlow(ScreenFlow
* flow
) {
81 flow
->set_screen_manager(this);
82 // TODO(antrim): delayed reset.
86 void ScreenManager::ShowScreenImpl(const std::string
& id
,
87 ScreenContext
* context
,
90 DCHECK(!start_screen_params_
.get());
91 start_screen_params_
.reset(context
);
92 start_screen_popup_
= popup
;
96 DCHECK(popup
|| screen_stack_
.size() <= 1);
97 BaseScreen
* screen
= FindOrCreateScreen(id
);
98 screen
->SetContext(context
);
99 screen
->Initialize(context
);
101 std::string previous_screen
;
102 if (!screen_stack_
.empty()) {
103 previous_screen
= GetCurrentScreenId();
104 GetTopmostScreen()->OnHide();
106 TearDownTopmostScreen();
109 DCHECK(popup
|| screen_stack_
.empty());
110 screen_stack_
.push(id
);
112 TransitionScreen(previous_screen
, id
);
115 void ScreenManager::TransitionScreen(const std::string
& from_id
,
116 const std::string
& to_id
) {
119 void ScreenManager::TearDownTopmostScreen() {
120 DCHECK(!screen_stack_
.empty());
121 std::string screen_id
= screen_stack_
.top();
123 ScreenMap::iterator i
= existing_screens_
.find(screen_id
);
125 if (i
== existing_screens_
.end()) {
129 BaseScreen
* screen
= i
->second
.get();
130 if (!screen
->IsPermanent()) {
132 existing_screens_
.erase(i
);
136 void ScreenManager::OnDisplayIsReady() {
140 start_screen_params_
.release(),
141 start_screen_popup_
);
144 BaseScreen
* ScreenManager::GetTopmostScreen() {
145 DCHECK(!screen_stack_
.empty());
146 return FindOrCreateScreen(screen_stack_
.top());
149 BaseScreen
* ScreenManager::FindOrCreateScreen(const std::string
& id
) {
150 ScreenMap::iterator i
= existing_screens_
.find(id
);
151 if (i
!= existing_screens_
.end())
152 return i
->second
.get();
153 BaseScreen
* result
= factory_
->CreateScreen(id
);
154 existing_screens_
[id
] = linked_ptr
<BaseScreen
>(result
);
158 void ScreenManager::OnButtonPressed(const std::string
& screen_name
,
159 const std::string
& button_id
) {
160 CallOnScreen
<const std::string
&>(screen_name
,
161 &BaseScreen::OnButtonPressed
,
165 void ScreenManager::OnContextChanged(const std::string
& screen_name
,
166 const base::DictionaryValue
* diff
) {
167 CallOnScreen
<const base::DictionaryValue
*>(screen_name
,
168 &BaseScreen::OnContextChanged
,
172 } // namespace chromeos