Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / wrt-popup / wrt / popup-bin / renderer / 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 <memory>
27 #include <dpl/assert.h>
28 #include <dpl/noncopyable.h>
29 #include <dpl/singleton.h>
30 #include <dpl/optional.h>
31 #include <dpl/log/log.h>
32 #include "popup.h"
33 #include "popup_renderer.h"
34
35 namespace Wrt {
36 namespace Popup {
37 namespace Renderer {
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(
61         IPopupPtr popup,
62         typename TemplatedPopupCallback<ArgType>::Type
63         callback,
64         ArgType* argument)
65     {
66         Assert(callback);
67         WrapCbAndArg<ArgType>* wrapped =
68             new WrapCbAndArg<ArgType>(callback, argument);
69         popup->Show(&CallbackArgTypeTranslator<ArgType>, wrapped);
70     }
71
72     void Show(IPopupPtr popup,
73               IPopup::PopupCallbackType callback,
74               void* userdata)
75     {
76         popup->Show(callback, userdata);
77     }
78
79     void setExternalCanvas(void* externalCanvas)
80     {
81         Assert(m_initialized && "Manger should be initialized");
82         m_popupRenderer->setExternalCanvas(externalCanvas);
83     }
84
85   private:
86     template <class ArgType>
87     struct WrapCbAndArg
88     {
89         WrapCbAndArg(typename TemplatedPopupCallback<ArgType>::Type cb,
90                      ArgType* arg) :
91             callback(cb),
92             argument(arg)
93         {}
94
95         typename TemplatedPopupCallback<ArgType>::Type callback;
96         ArgType* argument;
97     };
98
99     template <class ArgType>
100     static void CallbackArgTypeTranslator(const AnswerCallbackData & answer,
101                                           void* data)
102     {
103         WrapCbAndArg<ArgType>* wrapped =
104             static_cast< WrapCbAndArg<ArgType>* >(data);
105         wrapped->callback(answer, wrapped->argument);
106         delete wrapped;
107     }
108
109     bool m_initialized;
110     PopupRendererPtr m_popupRenderer;
111 };
112
113 typedef DPL::Singleton<PopupManager> PopupManagerSingleton;
114 }
115 } // namespace Popup
116 } // namespace Wrt
117
118 #endif //WRT_SRC_POPUP_POPUP_MANAGER_H_