Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkContentsClientCallbackHelper.java
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 package org.xwalk.core;
6
7 import android.os.Handler;
8 import android.os.Looper;
9 import android.os.Message;
10 import android.util.Log;
11
12 import org.chromium.content.browser.ContentViewCore;
13
14 /**
15  * This class is responsible for calling certain client callbacks on the UI thread.
16  *
17  * Most callbacks do no go through here, but get forwarded to XWalkContentsClient directly. The
18  * messages processed here may originate from the IO or UI thread.
19  */
20 class XWalkContentsClientCallbackHelper {
21
22     // TODO(boliu): Consider removing DownloadInfo and LoginRequestInfo by using native
23     // MessageLoop to post directly to XWalkContent.
24
25     private static class DownloadInfo {
26         final String mUrl;
27         final String mUserAgent;
28         final String mContentDisposition;
29         final String mMimeType;
30         final long mContentLength;
31
32         DownloadInfo(String url,
33                      String userAgent,
34                      String contentDisposition,
35                      String mimeType,
36                      long contentLength) {
37             mUrl = url;
38             mUserAgent = userAgent;
39             mContentDisposition = contentDisposition;
40             mMimeType = mimeType;
41             mContentLength = contentLength;
42         }
43     }
44
45     private static class LoginRequestInfo {
46         final String mRealm;
47         final String mAccount;
48         final String mArgs;
49
50         LoginRequestInfo(String realm, String account, String args) {
51           mRealm = realm;
52           mAccount = account;
53           mArgs = args;
54         }
55     }
56
57     private static class OnReceivedErrorInfo {
58         final int mErrorCode;
59         final String mDescription;
60         final String mFailingUrl;
61
62         OnReceivedErrorInfo(int errorCode, String description, String failingUrl) {
63             mErrorCode = errorCode;
64             mDescription = description;
65             mFailingUrl = failingUrl;
66         }
67     }
68
69     private final static int MSG_ON_LOAD_RESOURCE = 1;
70     private final static int MSG_ON_PAGE_STARTED = 2;
71     private final static int MSG_ON_DOWNLOAD_START = 3;
72     private final static int MSG_ON_RECEIVED_LOGIN_REQUEST = 4;
73     private final static int MSG_ON_RECEIVED_ERROR = 5;
74
75     private final XWalkContentsClient mContentsClient;
76
77     private final Handler mHandler = new Handler(Looper.getMainLooper()) {
78         @Override
79         public void handleMessage(Message msg) {
80             switch(msg.what) {
81                 case MSG_ON_LOAD_RESOURCE: {
82                     final String url = (String) msg.obj;
83                     mContentsClient.onLoadResource(url);
84                     break;
85                 }
86                 case MSG_ON_PAGE_STARTED: {
87                     final String url = (String) msg.obj;
88                     mContentsClient.onPageStarted(url);
89                     break;
90                 }
91                 case MSG_ON_DOWNLOAD_START: {
92                     DownloadInfo info = (DownloadInfo) msg.obj;
93                     mContentsClient.onDownloadStart(info.mUrl, info.mUserAgent,
94                             info.mContentDisposition, info.mMimeType, info.mContentLength);
95                     break;
96                 }
97                 case MSG_ON_RECEIVED_LOGIN_REQUEST: {
98                     LoginRequestInfo info = (LoginRequestInfo) msg.obj;
99                     mContentsClient.onReceivedLoginRequest(info.mRealm, info.mAccount, info.mArgs);
100                     break;
101                 }
102                 case MSG_ON_RECEIVED_ERROR: {
103                     OnReceivedErrorInfo info = (OnReceivedErrorInfo) msg.obj;
104                     mContentsClient.onReceivedError(info.mErrorCode, info.mDescription,
105                             info.mFailingUrl);
106                     break;
107                 }
108                 default:
109                     throw new IllegalStateException(
110                             "XWalkContentsClientCallbackHelper: unhandled message " + msg.what);
111             }
112         }
113     };
114
115     public XWalkContentsClientCallbackHelper(XWalkContentsClient contentsClient) {
116         mContentsClient = contentsClient;
117     }
118
119     public void postOnLoadResource(String url) {
120         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_LOAD_RESOURCE, url));
121     }
122
123     public void postOnPageStarted(String url) {
124         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_PAGE_STARTED, url));
125     }
126
127     public void postOnDownloadStart(String url, String userAgent, String contentDisposition,
128             String mimeType, long contentLength) {
129         DownloadInfo info = new DownloadInfo(url, userAgent, contentDisposition, mimeType,
130                 contentLength);
131         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_DOWNLOAD_START, info));
132     }
133
134     public void postOnReceivedLoginRequest(String realm, String account, String args) {
135         LoginRequestInfo info = new LoginRequestInfo(realm, account, args);
136         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_RECEIVED_LOGIN_REQUEST, info));
137     }
138
139     public void postOnReceivedError(int errorCode, String description, String failingUrl) {
140         OnReceivedErrorInfo info = new OnReceivedErrorInfo(errorCode, description, failingUrl);
141         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_RECEIVED_ERROR, info));
142     }
143 }