Upstream version 8.36.156.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     private final static int MSG_ON_RESOURCE_LOAD_STARTED = 6;
75
76     private final XWalkContentsClient mContentsClient;
77
78     private final Handler mHandler = new Handler(Looper.getMainLooper()) {
79         @Override
80         public void handleMessage(Message msg) {
81             switch(msg.what) {
82                 case MSG_ON_LOAD_RESOURCE: {
83                     final String url = (String) msg.obj;
84                     mContentsClient.onLoadResource(url);
85                     break;
86                 }
87                 case MSG_ON_PAGE_STARTED: {
88                     final String url = (String) msg.obj;
89                     mContentsClient.onPageStarted(url);
90                     break;
91                 }
92                 case MSG_ON_DOWNLOAD_START: {
93                     DownloadInfo info = (DownloadInfo) msg.obj;
94                     mContentsClient.onDownloadStart(info.mUrl, info.mUserAgent,
95                             info.mContentDisposition, info.mMimeType, info.mContentLength);
96                     break;
97                 }
98                 case MSG_ON_RECEIVED_LOGIN_REQUEST: {
99                     LoginRequestInfo info = (LoginRequestInfo) msg.obj;
100                     mContentsClient.onReceivedLoginRequest(info.mRealm, info.mAccount, info.mArgs);
101                     break;
102                 }
103                 case MSG_ON_RECEIVED_ERROR: {
104                     OnReceivedErrorInfo info = (OnReceivedErrorInfo) msg.obj;
105                     mContentsClient.onReceivedError(info.mErrorCode, info.mDescription,
106                             info.mFailingUrl);
107                     break;
108                 }
109                 case MSG_ON_RESOURCE_LOAD_STARTED: {
110                     final String url = (String) msg.obj;
111                     mContentsClient.onResourceLoadStarted(url);
112                     break;
113                 }
114                 default:
115                     throw new IllegalStateException(
116                             "XWalkContentsClientCallbackHelper: unhandled message " + msg.what);
117             }
118         }
119     };
120
121     public XWalkContentsClientCallbackHelper(XWalkContentsClient contentsClient) {
122         mContentsClient = contentsClient;
123     }
124
125     public void postOnLoadResource(String url) {
126         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_LOAD_RESOURCE, url));
127     }
128
129     public void postOnPageStarted(String url) {
130         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_PAGE_STARTED, url));
131     }
132
133     public void postOnDownloadStart(String url, String userAgent, String contentDisposition,
134             String mimeType, long contentLength) {
135         DownloadInfo info = new DownloadInfo(url, userAgent, contentDisposition, mimeType,
136                 contentLength);
137         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_DOWNLOAD_START, info));
138     }
139
140     public void postOnReceivedLoginRequest(String realm, String account, String args) {
141         LoginRequestInfo info = new LoginRequestInfo(realm, account, args);
142         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_RECEIVED_LOGIN_REQUEST, info));
143     }
144
145     public void postOnReceivedError(int errorCode, String description, String failingUrl) {
146         OnReceivedErrorInfo info = new OnReceivedErrorInfo(errorCode, description, failingUrl);
147         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_RECEIVED_ERROR, info));
148     }
149
150     public void postOnResourceLoadStarted(String url) {
151         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_RESOURCE_LOAD_STARTED, url));
152     }
153 }