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.
5 #ifndef XWALK_EXTENSIONS_RENDERER_XWALK_EXTENSION_CLIENT_H_
6 #define XWALK_EXTENSIONS_RENDERER_XWALK_EXTENSION_CLIENT_H_
13 #include "base/memory/scoped_ptr.h"
14 #include "base/values.h"
15 #include "ipc/ipc_listener.h"
26 namespace extensions {
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.
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 {
37 struct InstanceHandler {
38 virtual void HandleMessageFromNative(const base::Value& msg) = 0;
43 XWalkExtensionClient();
44 virtual ~XWalkExtensionClient();
46 int64_t CreateInstance(const std::string& extension_name,
47 InstanceHandler* handler);
48 void DestroyInstance(int64_t instance_id);
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);
54 void Initialize(IPC::Sender* sender);
56 // IPC::Listener Implementation.
57 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
59 struct ExtensionCodePoints {
60 ExtensionCodePoints();
61 ~ExtensionCodePoints();
63 std::vector<std::string> entry_points;
66 typedef std::map<std::string, ExtensionCodePoints*> ExtensionAPIMap;
68 const ExtensionAPIMap& extension_apis() const { return extension_apis_; }
71 bool Send(IPC::Message* msg);
74 void OnInstanceDestroyed(int64_t instance_id);
75 void OnPostMessageToJS(int64_t instance_id, const base::ListValue& msg);
78 ExtensionAPIMap extension_apis_;
80 typedef std::map<int64_t, InstanceHandler*> HandlerMap;
83 int64_t next_instance_id_;
86 } // namespace extensions
89 #endif // XWALK_EXTENSIONS_RENDERER_XWALK_EXTENSION_CLIENT_H_