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.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_VIEW_SCREEN_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_VIEW_SCREEN_H_
7 #pragma once
8
9 #include "base/message_loop.h"
10 #include "chrome/browser/chromeos/login/helper.h"
11 #include "chrome/browser/chromeos/login/wizard_screen.h"
12 #include "ui/gfx/size.h"
13
14 template <class V>
15 class ViewScreen : public WizardScreen {
16 public:
17 // Create screen with default size.
18 explicit ViewScreen(WizardScreenDelegate* delegate);
19
20 // Create screen with the specified size.
21 ViewScreen(WizardScreenDelegate* delegate, int width, int height);
22 virtual ~ViewScreen();
23
24 // Overridden from WizardScreen:
25 virtual void Show();
26 virtual void Hide();
GetScreenSize()27 virtual gfx::Size GetScreenSize() const { return size_; }
28
view()29 V* view() { return view_; }
30
31 protected:
32 // Creates view object and adds it to views hierarchy.
33 virtual void CreateView();
34 // Creates view object.
35 virtual V* AllocateView() = 0;
36
37 // Refresh screen state.
Refresh()38 virtual void Refresh() {}
39
40 private:
41 // For testing automation
42 friend class AutomationProvider;
43
44 V* view_;
45
46 // Size of the screen.
47 gfx::Size size_;
48
49 DISALLOW_COPY_AND_ASSIGN(ViewScreen);
50 };
51
52 template <class V>
53 class DefaultViewScreen : public ViewScreen<V> {
54 public:
DefaultViewScreen(WizardScreenDelegate * delegate)55 explicit DefaultViewScreen(WizardScreenDelegate* delegate)
56 : ViewScreen<V>(delegate) {}
DefaultViewScreen(WizardScreenDelegate * delegate,int width,int height)57 DefaultViewScreen(WizardScreenDelegate* delegate, int width, int height)
58 : ViewScreen<V>(delegate, width, height) {}
AllocateView()59 V* AllocateView() {
60 return new V(ViewScreen<V>::delegate()->GetObserver(this));
61 }
62 };
63
64 ///////////////////////////////////////////////////////////////////////////////
65 // ViewScreen, public:
66 template <class V>
ViewScreen(WizardScreenDelegate * delegate)67 ViewScreen<V>::ViewScreen(WizardScreenDelegate* delegate)
68 : WizardScreen(delegate),
69 view_(NULL),
70 size_(chromeos::login::kWizardScreenWidth,
71 chromeos::login::kWizardScreenHeight) {
72 }
73
74 template <class V>
ViewScreen(WizardScreenDelegate * delegate,int width,int height)75 ViewScreen<V>::ViewScreen(WizardScreenDelegate* delegate, int width, int height)
76 : WizardScreen(delegate),
77 view_(NULL),
78 size_(width, height) {
79 }
80
81 template <class V>
~ViewScreen()82 ViewScreen<V>::~ViewScreen() {
83 // Delete the view now. So we do not worry the View outlives its
84 // controller.
85 if (view_) {
86 delete view_;
87 view_ = NULL;
88 }
89 }
90
91 ///////////////////////////////////////////////////////////////////////////////
92 // ViewScreen, WizardScreen implementation:
93 template <class V>
Show()94 void ViewScreen<V>::Show() {
95 if (!view_) {
96 CreateView();
97 }
98 view_->SetVisible(true);
99 // After screen is initialized and shown refresh its model.
100 // Refresh() is called after SetVisible(true) because screen handler
101 // could exit right away.
102 Refresh();
103 }
104
105 template <class V>
Hide()106 void ViewScreen<V>::Hide() {
107 if (view_) {
108 delegate()->GetWizardView()->RemoveChildView(view_);
109 // RemoveChildView doesn't delete the view and we also can't delete it here
110 // becuase we are in message processing for the view.
111 MessageLoop::current()->DeleteSoon(FROM_HERE, view_);
112 view_ = NULL;
113 }
114 }
115
116 ///////////////////////////////////////////////////////////////////////////////
117 // ViewScreen, protected:
118 template <class V>
CreateView()119 void ViewScreen<V>::CreateView() {
120 view_ = AllocateView();
121 view_->set_parent_owned(false); // ViewScreen owns the view.
122 delegate()->GetWizardView()->AddChildView(view_);
123 view_->Init();
124 view_->SetVisible(false);
125 }
126
127 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_VIEW_SCREEN_H_
128