d4fd47991b69a0570cafdda8308b3050cd4c31f6
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / android / xwalk_contents_client_bridge.h
1 // Copyright (c) 2013 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.
4
5 #ifndef XWALK_RUNTIME_BROWSER_ANDROID_XWALK_CONTENTS_CLIENT_BRIDGE_H_
6 #define XWALK_RUNTIME_BROWSER_ANDROID_XWALK_CONTENTS_CLIENT_BRIDGE_H_
7
8 #include <jni.h>
9 #include <map>
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 #include "base/android/jni_weak_ref.h"
15 #include "base/android/scoped_java_ref.h"
16 #include "base/callback.h"
17 #include "base/id_map.h"
18 #include "content/public/browser/javascript_dialog_manager.h"
19 #include "xwalk/runtime/browser/android/xwalk_contents_client_bridge_base.h"
20
21 namespace gfx {
22 class Size;
23 }
24
25 namespace net {
26 class X509Certificate;
27 }
28
29 class SkBitmap;
30
31 namespace xwalk {
32
33 // A class that handles the Java<->Native communication for the
34 // XWalkContentsClient. XWalkContentsClientBridge is created and owned by
35 // native XWalkViewContents class and it only has a weak reference to the
36 // its Java peer. Since the Java XWalkContentsClientBridge can have
37 // indirect refs from the Application (via callbacks) and so can outlive
38 // XWalkView, this class notifies it before being destroyed and to nullify
39 // any references.
40 class XWalkContentsClientBridge : public XWalkContentsClientBridgeBase {
41  public:
42   XWalkContentsClientBridge(JNIEnv* env, jobject obj);
43   virtual ~XWalkContentsClientBridge();
44
45   // XWalkContentsClientBridgeBase implementation
46   virtual void AllowCertificateError(int cert_error,
47                                      net::X509Certificate* cert,
48                                      const GURL& request_url,
49                                      const base::Callback<void(bool)>& callback, // NOLINT
50                                      bool* cancel_request) OVERRIDE;
51
52   virtual void RunJavaScriptDialog(
53       content::JavaScriptMessageType message_type,
54       const GURL& origin_url,
55       const base::string16& message_text,
56       const base::string16& default_prompt_text,
57       const content::JavaScriptDialogManager::DialogClosedCallback& callback)
58       OVERRIDE;
59   virtual void RunBeforeUnloadDialog(
60       const GURL& origin_url,
61       const base::string16& message_text,
62       const content::JavaScriptDialogManager::DialogClosedCallback& callback)
63       OVERRIDE;
64   virtual void ShowNotification(
65       const content::ShowDesktopNotificationHostMsgParams& params,
66       content::RenderFrameHost* render_frame_host,
67       content::DesktopNotificationDelegate* delegate,
68       base::Closure* cancel_callback)
69       OVERRIDE;
70   virtual void UpdateNotificationIcon(
71       int notification_id,
72       const SkBitmap& icon)
73       OVERRIDE;
74
75   bool OnReceivedHttpAuthRequest(const base::android::JavaRef<jobject>& handler,
76                                  const std::string& host,
77                                  const std::string& realm);
78
79   void OnNotificationIconDownloaded(
80       int id,
81       int http_status_code,
82       const GURL& image_url,
83       const std::vector<SkBitmap>& bitmaps,
84       const std::vector<gfx::Size>& original_bitmap_sizes);
85
86   // Methods called from Java.
87   void ProceedSslError(JNIEnv* env, jobject obj, jboolean proceed, jint id);
88   void ConfirmJsResult(JNIEnv*, jobject, int id, jstring prompt);
89   void CancelJsResult(JNIEnv*, jobject, int id);
90   void ExitFullscreen(JNIEnv*, jobject, jlong web_contents);
91   void NotificationDisplayed(JNIEnv*, jobject, jlong delegate);
92   void NotificationError(JNIEnv*, jobject, jlong delegate);
93   void NotificationClicked(JNIEnv*, jobject, jint id, jlong delegate);
94   void NotificationClosed(JNIEnv*, jobject, jint id, bool by_user,
95     jlong delegate);
96   void OnFilesSelected(
97       JNIEnv*, jobject, int process_id, int render_id,
98       int mode, jstring filepath, jstring display_name);
99   void OnFilesNotSelected(
100       JNIEnv*, jobject, int process_id, int render_id, int mode);
101
102  private:
103   JavaObjectWeakGlobalRef java_ref_;
104
105   typedef const base::Callback<void(bool)> CertErrorCallback; // NOLINT
106   IDMap<CertErrorCallback, IDMapOwnPointer> pending_cert_error_callbacks_;
107   IDMap<content::JavaScriptDialogManager::DialogClosedCallback, IDMapOwnPointer>
108       pending_js_dialog_callbacks_;
109
110   typedef std::pair<int, content::RenderFrameHost*>
111     NotificationDownloadRequestInfos;
112   typedef std::map<int, NotificationDownloadRequestInfos >
113     NotificationDownloadRequestIdMap;
114   NotificationDownloadRequestIdMap downloading_icon_notifications_;
115 };
116
117 bool RegisterXWalkContentsClientBridge(JNIEnv* env);
118
119 }  // namespace xwalk
120
121 #endif  // XWALK_RUNTIME_BROWSER_ANDROID_XWALK_CONTENTS_CLIENT_BRIDGE_H_