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;
75 private final XWalkContentsClient mContentsClient;
77 private final Handler mHandler = new Handler(Looper.getMainLooper()) {
79 public void handleMessage(Message msg) {
81 case MSG_ON_LOAD_RESOURCE: {
82 final String url = (String) msg.obj;
83 mContentsClient.onLoadResource(url);
86 case MSG_ON_PAGE_STARTED: {
87 final String url = (String) msg.obj;
88 mContentsClient.onPageStarted(url);
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);
97 case MSG_ON_RECEIVED_LOGIN_REQUEST: {
98 LoginRequestInfo info = (LoginRequestInfo) msg.obj;
99 mContentsClient.onReceivedLoginRequest(info.mRealm, info.mAccount, info.mArgs);
102 case MSG_ON_RECEIVED_ERROR: {
103 OnReceivedErrorInfo info = (OnReceivedErrorInfo) msg.obj;
104 mContentsClient.onReceivedError(info.mErrorCode, info.mDescription,
109 throw new IllegalStateException(
110 "XWalkContentsClientCallbackHelper: unhandled message " + msg.what);
115 public XWalkContentsClientCallbackHelper(XWalkContentsClient contentsClient) {
116 mContentsClient = contentsClient;
119 public void postOnLoadResource(String url) {
120 mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_LOAD_RESOURCE, url));
123 public void postOnPageStarted(String url) {
124 mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_PAGE_STARTED, url));
127 public void postOnDownloadStart(String url, String userAgent, String contentDisposition,
128 String mimeType, long contentLength) {
129 DownloadInfo info = new DownloadInfo(url, userAgent, contentDisposition, mimeType,
131 mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_DOWNLOAD_START, info));
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));
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));