Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / CommonsJavaScript / JSPendingOperationPrivateObject.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  * @author      Karol Majewski (k.majewski@samsung.com)
18  * @version     0.1
19  * @brief
20  */
21
22 #ifndef WRTDEVICEAPIS_COMMONSJAVASCRIPT_JS_PENDING_OPERATION_PRIVATE_OBJECT_H_
23 #define WRTDEVICEAPIS_COMMONSJAVASCRIPT_JS_PENDING_OPERATION_PRIVATE_OBJECT_H_
24
25 #include <cassert>
26 #include <dpl/log/log.h>
27 #include <dpl/shared_ptr.h>
28 #include <Commons/EventReceiver.h>
29 #include <Commons/IExternEventCanceler.h>
30 #include <Commons/IEvent.h>
31
32 namespace WrtDeviceApis {
33 namespace CommonsJavaScript {
34 class IJSPendingOperationPrivateObject
35 {
36   public:
37     explicit IJSPendingOperationPrivateObject(
38         const DPL::SharedPtr<Commons::IEventController>
39         &event) :
40         m_event(event)
41     {
42         assert(NULL != m_event.Get());
43     }
44
45     virtual bool cancel()
46     {
47         LogDebug("PendingOperation tries to cancel the request");
48         return m_event->cancelRequest();
49     }
50
51     virtual ~IJSPendingOperationPrivateObject()
52     {}
53
54   protected:
55     DPL::SharedPtr<Commons::IEventController> m_event;
56 };
57
58 /*
59  * This class implements private object for JavaScript PendingOperation
60  * with support to external cancel function.
61  *
62  * When the cancel() method is invoked on PendingOperation,
63  * the OnCancelEvent(<EventType>) method is invoked on user object
64  * with apropriate * arguments.
65  * It allows user to perform an additional action when PendingOperation
66  * is destroyed.
67  *
68  * The Object which want to handle OnCancelEvent have to inherit from
69  * Platform::IExternEventCanceler<EventType>.
70  *
71  * The cancel's handler have to be passed as second argument
72  * */
73 template<class TemplateEvent>
74 class IJSExtCancelPendingOperationPrivateObject :
75     public IJSPendingOperationPrivateObject
76 {
77   public:
78     IJSExtCancelPendingOperationPrivateObject(
79         const DPL::SharedPtr< TemplateEvent> &event,
80         const DPL::SharedPtr<
81             Commons::IExternEventCanceler<TemplateEvent> > & cancel) :
82         IJSPendingOperationPrivateObject(
83             DPL::StaticPointerCast<Commons::IEventController>(event)),
84         m_canceler(cancel)
85     {
86         assert(NULL != m_canceler);
87     }
88
89     virtual bool cancel()
90     {
91         bool result = IJSPendingOperationPrivateObject::cancel();
92         if (!result) {
93             LogDebug("Controller can not cancel event, trying platform cancel");
94         } else {
95             return result;
96         }
97
98         if (m_canceler) {
99             LogDebug("Calling extern cancel");
100             m_canceler->OnCancelEvent(
101                 DPL::StaticPointerCast<TemplateEvent>(m_event));
102         }
103
104         return result;
105     }
106
107     virtual ~IJSExtCancelPendingOperationPrivateObject()
108     {}
109
110   protected:
111     DPL::SharedPtr< Commons::IExternEventCanceler<TemplateEvent> > m_canceler;
112 };
113 }
114 }
115 #endif /* _JS_WAC_PENDING_OPERATION_PRIVATE_OBJECT_H_ */