8e21d7c174a33b89b986163f1666e01fd5c70393
[platform/framework/web/crosswalk.git] / src / xwalk / extensions / renderer / xwalk_extension_client.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_EXTENSIONS_RENDERER_XWALK_EXTENSION_CLIENT_H_
6 #define XWALK_EXTENSIONS_RENDERER_XWALK_EXTENSION_CLIENT_H_
7
8 #include <stdint.h>
9 #include <map>
10 #include <string>
11 #include <vector>
12
13 #include "base/memory/scoped_ptr.h"
14 #include "base/values.h"
15 #include "ipc/ipc_listener.h"
16
17 namespace base {
18 class Value;
19 }
20
21 namespace IPC {
22 class Sender;
23 }
24
25 namespace xwalk {
26 namespace extensions {
27
28 // This class holds the JavaScript context of Extensions. It lives in the
29 // Render Process and communicates directly with its associated
30 // XWalkExtensionServer through an IPC channel.
31 //
32 // Users of this class post (and send sync) messages to specific instances and
33 // are able to handle messages from instances by implementing the
34 // InstanceHandler interface.
35 class XWalkExtensionClient : public IPC::Listener {
36  public:
37   struct InstanceHandler {
38     virtual void HandleMessageFromNative(const base::Value& msg) = 0;
39    protected:
40     ~InstanceHandler() {}
41   };
42
43   XWalkExtensionClient();
44   virtual ~XWalkExtensionClient();
45
46   int64_t CreateInstance(const std::string& extension_name,
47                          InstanceHandler* handler);
48   void DestroyInstance(int64_t instance_id);
49
50   void PostMessageToNative(int64_t instance_id, scoped_ptr<base::Value> msg);
51   scoped_ptr<base::Value> SendSyncMessageToNative(int64_t instance_id,
52       scoped_ptr<base::Value> msg);
53
54   void Initialize(IPC::Sender* sender);
55
56   // IPC::Listener Implementation.
57   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
58
59   struct ExtensionCodePoints {
60     ExtensionCodePoints();
61     ~ExtensionCodePoints();
62     std::string api;
63     std::vector<std::string> entry_points;
64   };
65
66   typedef std::map<std::string, ExtensionCodePoints*> ExtensionAPIMap;
67
68   const ExtensionAPIMap& extension_apis() const { return extension_apis_; }
69
70  private:
71   bool Send(IPC::Message* msg);
72
73   // Message Handlers.
74   void OnInstanceDestroyed(int64_t instance_id);
75   void OnPostMessageToJS(int64_t instance_id, const base::ListValue& msg);
76
77   IPC::Sender* sender_;
78   ExtensionAPIMap extension_apis_;
79
80   typedef std::map<int64_t, InstanceHandler*> HandlerMap;
81   HandlerMap handlers_;
82
83   int64_t next_instance_id_;
84 };
85
86 }  // namespace extensions
87 }  // namespace xwalk
88
89 #endif  // XWALK_EXTENSIONS_RENDERER_XWALK_EXTENSION_CLIENT_H_