faed7ea4d761e350df620006d6a79de3030b80b2
[platform/framework/web/crosswalk.git] / src / xwalk / application / extension / application_event_extension.h
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef XWALK_APPLICATION_EXTENSION_APPLICATION_EVENT_EXTENSION_H_
6 #define XWALK_APPLICATION_EXTENSION_APPLICATION_EVENT_EXTENSION_H_
7
8 #include <map>
9 #include <string>
10
11 #include "xwalk/application/browser/event_observer.h"
12 #include "xwalk/extensions/browser/xwalk_extension_function_handler.h"
13 #include "xwalk/extensions/common/xwalk_extension.h"
14
15 namespace xwalk {
16 namespace application {
17 class ApplicationEventManager;
18 class ApplicationStorage;
19 class Application;
20 class AppEventExtensionInstance;
21
22 using extensions::XWalkExtension;
23 using extensions::XWalkExtensionFunctionHandler;
24 using extensions::XWalkExtensionFunctionInfo;
25 using extensions::XWalkExtensionInstance;
26
27 class ApplicationEventExtension : public XWalkExtension {
28  public:
29   ApplicationEventExtension(ApplicationEventManager* event_manager,
30                             ApplicationStorage* app_storage,
31                             Application* application);
32
33   // XWalkExtension implementation.
34   virtual XWalkExtensionInstance* CreateInstance() OVERRIDE;
35
36  private:
37   ApplicationEventManager* event_manager_;
38   ApplicationStorage* app_storage_;
39   Application* application_;
40 };
41
42 class AppEventExtensionInstance : public XWalkExtensionInstance,
43                                   public EventObserver {
44  public:
45   AppEventExtensionInstance(ApplicationEventManager* event_manager,
46                             ApplicationStorage* app_storage,
47                             Application* application,
48                             int main_routing_id);
49
50   virtual ~AppEventExtensionInstance();
51   // XWalkExtensionInstance implementation.
52   virtual void HandleMessage(scoped_ptr<base::Value> msg) OVERRIDE;
53
54   // EventObserver implementation.
55   virtual void Observe(const std::string& app_id,
56                        scoped_refptr<Event> event) OVERRIDE;
57
58  private:
59   // Registered handlers for incoming JS messages.
60   void OnRegisterEvent(scoped_ptr<XWalkExtensionFunctionInfo> info);
61   void OnUnregisterEvent(scoped_ptr<XWalkExtensionFunctionInfo> info);
62   void OnDispatchEventFinish(scoped_ptr<XWalkExtensionFunctionInfo> info);
63
64   typedef std::map<std::string, XWalkExtensionFunctionInfo::PostResultCallback>
65       EventCallbackMap;
66   EventCallbackMap registered_events_;
67   ApplicationStorage* app_storage_;
68   Application* application_;
69   int main_routing_id_;  // routing id of the main document.
70
71   XWalkExtensionFunctionHandler handler_;
72 };
73
74 }  // namespace application
75 }  // namespace xwalk
76
77 #endif  // XWALK_APPLICATION_EXTENSION_APPLICATION_EVENT_EXTENSION_H_