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.
5 package org.xwalk.core;
7 import android.os.Handler;
8 import android.os.Looper;
9 import android.os.Message;
10 import android.util.Log;
12 import org.chromium.content.browser.ContentViewCore;
15 * This class is responsible for calling certain client callbacks on the UI thread.
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.
20 class XWalkContentsClientCallbackHelper {
22 // TODO(boliu): Consider removing DownloadInfo and LoginRequestInfo by using native
23 // MessageLoop to post directly to XWalkContent.
25 private static class DownloadInfo {
27 final String mUserAgent;
28 final String mContentDisposition;
29 final String mMimeType;
30 final long mContentLength;
32 DownloadInfo(String url,
34 String contentDisposition,
38 mUserAgent = userAgent;
39 mContentDisposition = contentDisposition;
41 mContentLength = contentLength;
45 private static class LoginRequestInfo {
47 final String mAccount;
50 LoginRequestInfo(String realm, String account, String args) {
57 private static class OnReceivedErrorInfo {
59 final String mDescription;
60 final String mFailingUrl;
62 OnReceivedErrorInfo(int errorCode, String description, String failingUrl) {
63 mErrorCode = errorCode;
64 mDescription = description;
65 mFailingUrl = failingUrl;
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;
76 private final XWalkContentsClient mContentsClient;
78 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
80 public void handleMessage(Message msg) {
82 case MSG_ON_LOAD_RESOURCE: {
83 final String url = (String) msg.obj;
84 mContentsClient.onLoadResource(url);
87 case MSG_ON_PAGE_STARTED: {
88 final String url = (String) msg.obj;
89 mContentsClient.onPageStarted(url);
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);
98 case MSG_ON_RECEIVED_LOGIN_REQUEST: {
99 LoginRequestInfo info = (LoginRequestInfo) msg.obj;
100 mContentsClient.onReceivedLoginRequest(info.mRealm, info.mAccount, info.mArgs);
103 case MSG_ON_RECEIVED_ERROR: {
104 OnReceivedErrorInfo info = (OnReceivedErrorInfo) msg.obj;
105 mContentsClient.onReceivedError(info.mErrorCode, info.mDescription,
109 case MSG_ON_RESOURCE_LOAD_STARTED: {
110 final String url = (String) msg.obj;
111 mContentsClient.onResourceLoadStarted(url);
115 throw new IllegalStateException(
116 "XWalkContentsClientCallbackHelper: unhandled message " + msg.what);
121 public XWalkContentsClientCallbackHelper(XWalkContentsClient contentsClient) {
122 mContentsClient = contentsClient;
125 public void postOnLoadResource(String url) {
126 mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_LOAD_RESOURCE, url));
129 public void postOnPageStarted(String url) {
130 mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_PAGE_STARTED, url));
133 public void postOnDownloadStart(String url, String userAgent, String contentDisposition,
134 String mimeType, long contentLength) {
135 DownloadInfo info = new DownloadInfo(url, userAgent, contentDisposition, mimeType,
137 mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_DOWNLOAD_START, info));
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));
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));
150 public void postOnResourceLoadStarted(String url) {
151 mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_RESOURCE_LOAD_STARTED, url));