512ddd63c3c37d10981521ab0b5f67680b09403d
[framework/web/wrt-commons.git] / modules / popup / include / dpl / popup / popup_manager.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        popup_manager.h
18  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version     1.0
20  * @brief       This is popup_manager declaration file
21  */
22
23 #ifndef WRT_SRC_POPUP_POPUP_MANAGER_H_
24 #define WRT_SRC_POPUP_POPUP_MANAGER_H_
25
26 #include <dpl/assert.h>
27 #include <dpl/shared_ptr.h>
28 #include <dpl/noncopyable.h>
29 #include <dpl/singleton.h>
30 #include <dpl/optional.h>
31 #include <dpl/popup/popup.h>
32 #include <dpl/popup/popup_renderer.h>
33
34 namespace DPL {
35 namespace Popup {
36
37 class PopupManager : DPL::Noncopyable
38 {
39     template <class ArgType>
40     struct TemplatedPopupCallback
41     {
42         typedef void (*Type)(const AnswerCallbackData& answer, ArgType* data);
43     };
44
45   public:
46     PopupManager() : m_initialized(false) {}
47     ~PopupManager()
48     {
49         Assert(!m_initialized);
50     }
51     void Initialize (PopupRendererPtr creator);
52     void Deinitialize();
53     void SetPopupRenderer (PopupRendererPtr creator);
54     IPopupPtr CreatePopup();
55
56     template <class ArgType>
57     void RunAsyncWithArgType(IPopupPtr popup,
58             typename TemplatedPopupCallback<ArgType>::Type callback,
59             ArgType* argument)
60     {
61         Assert(callback);
62         WrapCbAndArg<ArgType>* wrapped =
63             new WrapCbAndArg<ArgType>(callback, argument);
64         popup->Show(&CallbackArgTypeTranslator<ArgType>, wrapped);
65     }
66
67     void Show(IPopupPtr popup,
68             IPopup::PopupCallbackType callback,
69             void* userdata)
70     {
71         popup->Show(callback, userdata);
72     }
73
74     void setExternalCanvas(void* externalCanvas)
75     {
76         Assert(m_initialized && "Manger should be initialized");
77         m_popupRenderer->setExternalCanvas(externalCanvas);
78     }
79
80   private:
81     template <class ArgType>
82     struct WrapCbAndArg
83     {
84         WrapCbAndArg(typename TemplatedPopupCallback<ArgType>::Type cb,
85                 ArgType* arg) :
86             callback(cb),
87             argument(arg)
88         {
89         }
90
91         typename TemplatedPopupCallback<ArgType>::Type callback;
92         ArgType* argument;
93     };
94
95     template <class ArgType>
96     static void CallbackArgTypeTranslator(const AnswerCallbackData & answer,
97             void* data)
98     {
99         WrapCbAndArg<ArgType>* wrapped =
100             static_cast< WrapCbAndArg<ArgType>* >(data);
101         wrapped->callback(answer, wrapped->argument);
102         delete wrapped;
103     }
104
105     bool m_initialized;
106     PopupRendererPtr m_popupRenderer;
107 };
108
109 typedef DPL::Singleton<Popup::PopupManager> PopupManagerSingleton;
110
111 } // namespace Popup
112 } // namespace DPL
113
114 #endif //WRT_SRC_POPUP_POPUP_MANAGER_H_