Roll src/third_party/WebKit 46ff567:25f83be (svn 200502:200512)
[chromium-blink-merge.git] / android_webview / native / aw_message_port_service_impl.h
blob7f42421bae70957862875b05cf9b7f5083865e10
1 // Copyright 2015 The Chromium Authors. 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 ANDROID_WEBVIEW_NATIVE_AW_MESSAGE_PORT_SERVICE_IMPL_H_
6 #define ANDROID_WEBVIEW_NATIVE_AW_MESSAGE_PORT_SERVICE_IMPL_H_
8 #include <jni.h>
9 #include <map>
11 #include "android_webview/browser/aw_message_port_service.h"
12 #include "base/android/jni_weak_ref.h"
13 #include "base/basictypes.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/strings/string16.h"
17 namespace android_webview {
19 // This class is the native peer of AwMessagePortService.java. Please see the
20 // java class for an explanation of use, ownership and lifetime.
22 // Threading: Created and initialized on UI thread. For other methods, see
23 // the method level DCHECKS or documentation.
24 class AwMessagePortServiceImpl : public AwMessagePortService {
25 public:
26 static AwMessagePortServiceImpl* GetInstance();
28 AwMessagePortServiceImpl();
29 ~AwMessagePortServiceImpl() override;
30 void Init(JNIEnv* env, jobject object);
32 void CreateMessageChannel(JNIEnv* env, jobjectArray ports,
33 scoped_refptr<AwMessagePortMessageFilter> filter);
35 // AwMessagePortService implementation
36 void OnConvertedWebToAppMessage(
37 int message_port_id,
38 const base::ListValue& message,
39 const std::vector<int>& sent_message_port_ids) override;
40 void OnMessagePortMessageFilterClosing(
41 AwMessagePortMessageFilter* filter) override;
42 void CleanupPort(int message_port_id) override;
44 // Methods called from Java.
45 void PostAppToWebMessage(JNIEnv* env, jobject object, int sender_id,
46 jstring message, jintArray sent_ports);
47 void ClosePort(JNIEnv* env, jobject object, int message_port_id);
48 void ReleaseMessages(JNIEnv* env, jobject object, int message_port_id);
50 void RemoveSentPorts(const std::vector<int>& sent_ports);
52 private:
53 void PostAppToWebMessageOnIOThread(
54 int sender_id,
55 base::string16* message,
56 std::vector<int>* sent_ports);
57 void CreateMessageChannelOnIOThread(
58 scoped_refptr<AwMessagePortMessageFilter> filter,
59 int* port1,
60 int* port2);
61 void OnMessageChannelCreated(
62 base::android::ScopedJavaGlobalRef<jobjectArray>* ports,
63 int* port1,
64 int* port2);
65 void AddPort(int message_port_id, AwMessagePortMessageFilter* filter);
66 void PostClosePortMessage(int message_port_id);
68 JavaObjectWeakGlobalRef java_ref_;
69 typedef std::map<int, AwMessagePortMessageFilter*> MessagePorts;
70 MessagePorts ports_; // Access on IO thread
72 DISALLOW_COPY_AND_ASSIGN(AwMessagePortServiceImpl);
75 bool RegisterAwMessagePortService(JNIEnv* env);
77 } // namespace android_webview
79 #endif // ANDROID_WEBVIEW_NATIVE_AW_MESSAGE_PORT_SERVICE_IMPL_H_