Update wrt-commons_0.2.53
[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 #include <dpl/log/log.h>
34
35 namespace DPL {
36 namespace Popup {
37
38 class PopupManager : DPL::Noncopyable
39 {
40     template <class ArgType>
41     struct TemplatedPopupCallback
42     {
43         typedef void (*Type)(const AnswerCallbackData& answer, ArgType* data);
44     };
45
46   public:
47     PopupManager() : m_initialized(false) {}
48     ~PopupManager()
49     {
50         if (m_initialized) {
51             LogError("Destroyed without Deinitialize");
52         }
53     }
54     void Initialize (PopupRendererPtr creator);
55     void Deinitialize();
56     void SetPopupRenderer (PopupRendererPtr creator);
57     IPopupPtr CreatePopup();
58
59     template <class ArgType>
60     void RunAsyncWithArgType(IPopupPtr popup,
61             typename TemplatedPopupCallback<ArgType>::Type callback,
62             ArgType* argument)
63     {
64         Assert(callback);
65         WrapCbAndArg<ArgType>* wrapped =
66             new WrapCbAndArg<ArgType>(callback, argument);
67         popup->Show(&CallbackArgTypeTranslator<ArgType>, wrapped);
68     }
69
70     void Show(IPopupPtr popup,
71             IPopup::PopupCallbackType callback,
72             void* userdata)
73     {
74         popup->Show(callback, userdata);
75     }
76
77     void setExternalCanvas(void* externalCanvas)
78     {
79         Assert(m_initialized && "Manger should be initialized");
80         m_popupRenderer->setExternalCanvas(externalCanvas);
81     }
82
83   private:
84     template <class ArgType>
85     struct WrapCbAndArg
86     {
87         WrapCbAndArg(typename TemplatedPopupCallback<ArgType>::Type cb,
88                 ArgType* arg) :
89             callback(cb),
90             argument(arg)
91         {
92         }
93
94         typename TemplatedPopupCallback<ArgType>::Type callback;
95         ArgType* argument;
96     };
97
98     template <class ArgType>
99     static void CallbackArgTypeTranslator(const AnswerCallbackData & answer,
100             void* data)
101     {
102         WrapCbAndArg<ArgType>* wrapped =
103             static_cast< WrapCbAndArg<ArgType>* >(data);
104         wrapped->callback(answer, wrapped->argument);
105         delete wrapped;
106     }
107
108     bool m_initialized;
109     PopupRendererPtr m_popupRenderer;
110 };
111
112 typedef DPL::Singleton<Popup::PopupManager> PopupManagerSingleton;
113
114 } // namespace Popup
115 } // namespace DPL
116
117 #endif //WRT_SRC_POPUP_POPUP_MANAGER_H_