Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application_event_manager.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_BROWSER_APPLICATION_EVENT_MANAGER_H_
6 #define XWALK_APPLICATION_BROWSER_APPLICATION_EVENT_MANAGER_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
12 #include "base/callback.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/values.h"
17 #include "xwalk/application/browser/application_service.h"
18 #include "xwalk/application/browser/event_observer.h"
19
20 namespace content {
21 class WebContents;
22 }
23
24 namespace xwalk {
25 namespace application {
26
27 class ApplicationEventRouter;
28
29 class Event : public base::RefCounted<Event> {
30  public:
31   static scoped_refptr<Event> CreateEvent(
32       const std::string& event_name, scoped_ptr<base::ListValue> event_args);
33
34   const std::string& name() const { return name_; }
35   base::ListValue* args() const { return args_.get(); }
36
37  private:
38   friend class base::RefCounted<Event>;
39   Event(const std::string& event_name, scoped_ptr<base::ListValue> event_args);
40   ~Event();
41
42   // The event to dispatch.
43   std::string name_;
44   // Arguments to send to the event handler.
45   scoped_ptr<base::ListValue> args_;
46 };
47
48 // This's the service class manages all application event routers.
49 class ApplicationEventManager : public ApplicationService::Observer {
50  public:
51   ApplicationEventManager();
52   virtual ~ApplicationEventManager();
53
54   // Create app router when app is loaded.
55   void AddEventRouterForApp(scoped_refptr<ApplicationData> app_data);
56   // Destroy app router when app is unloaded.
57   void RemoveEventRouterForApp(scoped_refptr<ApplicationData> app_data);
58
59   void SendEvent(const std::string& app_id,
60                  scoped_refptr<Event> event);
61   // TODO(xiang): Dispatch event to all loaded applications.
62   void BroadcastEvent(scoped_refptr<Event> event) { NOTIMPLEMENTED(); }
63
64   void AttachObserver(const std::string& app_id,
65                       const std::string& event_name,
66                       EventObserver* observer);
67   void DetachObserver(const std::string& app_id,
68                       const std::string& event_name,
69                       EventObserver* observer);
70   void DetachObserver(EventObserver* observer);
71
72  private:
73   // Implementation of ApplicationService::Observer.
74   virtual void DidLaunchApplication(Application* app) OVERRIDE;
75   virtual void WillDestroyApplication(Application* app) OVERRIDE;
76
77   ApplicationEventRouter* GetAppRouter(const std::string& app_id);
78
79   typedef std::map<std::string, linked_ptr<ApplicationEventRouter> >
80       AppRouterMap;
81   AppRouterMap app_routers_;
82
83   DISALLOW_COPY_AND_ASSIGN(ApplicationEventManager);
84 };
85
86 }  // namespace application
87 }  // namespace xwalk
88
89 #endif  // XWALK_APPLICATION_BROWSER_APPLICATION_EVENT_MANAGER_H_