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 ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_CLIENT_BRIDGE_H_
6#define ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_CLIENT_BRIDGE_H_
7
8#include <jni.h>
9
10#include "android_webview/browser/aw_contents_client_bridge_base.h"
11#include "base/android/jni_helper.h"
12#include "base/android/scoped_java_ref.h"
13#include "base/callback.h"
14#include "base/id_map.h"
15#include "content/public/browser/javascript_dialog_manager.h"
16
17namespace net {
18class X509Certificate;
19}
20
21namespace android_webview {
22
23// A class that handles the Java<->Native communication for the
24// AwContentsClient. AwContentsClientBridge is created and owned by
25// native AwContents class and it only has a weak reference to the
26// its Java peer. Since the Java AwContentsClientBridge can have
27// indirect refs from the Application (via callbacks) and so can outlive
28// webview, this class notifies it before being destroyed and to nullify
29// any references.
30class AwContentsClientBridge : public AwContentsClientBridgeBase {
31 public:
32
33  AwContentsClientBridge(JNIEnv* env, jobject obj);
34  virtual ~AwContentsClientBridge();
35
36  // AwContentsClientBridgeBase implementation
37  virtual void AllowCertificateError(int cert_error,
38                                     net::X509Certificate* cert,
39                                     const GURL& request_url,
40                                     const base::Callback<void(bool)>& callback,
41                                     bool* cancel_request) OVERRIDE;
42
43  virtual void RunJavaScriptDialog(
44      content::JavaScriptMessageType message_type,
45      const GURL& origin_url,
46      const string16& message_text,
47      const string16& default_prompt_text,
48      const content::JavaScriptDialogManager::DialogClosedCallback& callback)
49      OVERRIDE;
50  virtual void RunBeforeUnloadDialog(
51      const GURL& origin_url,
52      const string16& message_text,
53      const content::JavaScriptDialogManager::DialogClosedCallback& callback)
54      OVERRIDE;
55  virtual bool ShouldOverrideUrlLoading(const base::string16& url) OVERRIDE;
56
57  // Methods called from Java.
58  void ProceedSslError(JNIEnv* env, jobject obj, jboolean proceed, jint id);
59  void ConfirmJsResult(JNIEnv*, jobject, int id, jstring prompt);
60  void CancelJsResult(JNIEnv*, jobject, int id);
61
62 private:
63  JavaObjectWeakGlobalRef java_ref_;
64
65  typedef const base::Callback<void(bool)> CertErrorCallback;
66  IDMap<CertErrorCallback, IDMapOwnPointer> pending_cert_error_callbacks_;
67  IDMap<content::JavaScriptDialogManager::DialogClosedCallback, IDMapOwnPointer>
68      pending_js_dialog_callbacks_;
69};
70
71bool RegisterAwContentsClientBridge(JNIEnv* env);
72
73}  // namespace android_webview
74
75#endif  // ANDROID_WEBVIEW_NATIVE_AW_CONTENTS_CLIENT_BRIDGE_H_
76