tizen beta release
[framework/web/wrt-plugins-common.git] / src / 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);
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
55   protected:
56     DPL::SharedPtr<Commons::IEventController> m_event;
57 };
58
59 /*
60  * This class implements private object for JavaScript PendingOperation
61  * with support to external cancel function.
62  *
63  * When the cancel() method is invoked on PendingOperation,
64  * the OnCancelEvent(<EventType>) method is invoked on user object
65  * with apropriate * arguments.
66  * It allows user to perform an additional action when PendingOperation
67  * is destroyed.
68  *
69  * The Object which want to handle OnCancelEvent have to inherit from
70  * Platform::IExternEventCanceler<EventType>.
71  *
72  * The cancel's handler have to be passed as second argument
73  * */
74 template<class TemplateEvent>
75 class IJSExtCancelPendingOperationPrivateObject :
76     public IJSPendingOperationPrivateObject
77 {
78   public:
79     IJSExtCancelPendingOperationPrivateObject(
80             const DPL::SharedPtr< TemplateEvent> &event,
81             const DPL::SharedPtr<
82                 Commons::IExternEventCanceler<TemplateEvent> > & cancel) :
83         IJSPendingOperationPrivateObject(
84             DPL::StaticPointerCast<Commons::IEventController>(event)),
85         m_canceler(cancel)
86     {
87         assert(NULL != m_canceler);
88     }
89
90     virtual bool cancel()
91     {
92         bool result = IJSPendingOperationPrivateObject::cancel();
93         if (!result) {
94             LogDebug("Controller can not cancel event, trying platform cancel");
95         } else {
96             return result;
97         }
98
99         if (m_canceler) {
100             LogDebug("Calling extern cancel");
101             m_canceler->OnCancelEvent(
102                 DPL::StaticPointerCast<TemplateEvent>(m_event));
103         }
104
105         return result;
106     }
107
108     virtual ~IJSExtCancelPendingOperationPrivateObject()
109     {
110     }
111
112   protected:
113     DPL::SharedPtr< Commons::IExternEventCanceler<TemplateEvent> > m_canceler;
114 };
115 }
116 }
117 #endif /* _JS_WAC_PENDING_OPERATION_PRIVATE_OBJECT_H_ */