Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.browser; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.os.Handler; |
| 20 | import android.os.Looper; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 21 | import android.util.Log; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 22 | |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | /** |
| 27 | * Singleton class for handling preload requests. |
| 28 | */ |
| 29 | public class Preloader { |
| 30 | |
| 31 | private final static String LOGTAG = "browser.preloader"; |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 32 | private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 33 | |
| 34 | private static final int PRERENDER_TIMEOUT_MILLIS = 30 * 1000; // 30s |
| 35 | |
| 36 | private static Preloader sInstance; |
| 37 | |
| 38 | private final Context mContext; |
| 39 | private final Handler mHandler; |
| 40 | private final BrowserWebViewFactory mFactory; |
| 41 | private final HashMap<String, PreloaderSession> mSessions; |
| 42 | |
| 43 | public static void initialize(Context context) { |
| 44 | sInstance = new Preloader(context); |
| 45 | } |
| 46 | |
| 47 | public static Preloader getInstance() { |
| 48 | return sInstance; |
| 49 | } |
| 50 | |
| 51 | private Preloader(Context context) { |
| 52 | mContext = context; |
| 53 | mHandler = new Handler(Looper.getMainLooper()); |
| 54 | mSessions = new HashMap<String, PreloaderSession>(); |
| 55 | mFactory = new BrowserWebViewFactory(context); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | private PreloaderSession getSession(String id) { |
| 60 | PreloaderSession s = mSessions.get(id); |
| 61 | if (s == null) { |
| 62 | if (LOGD_ENABLED) Log.d(LOGTAG, "Create new preload session " + id); |
| 63 | s = new PreloaderSession(id); |
| 64 | mSessions.put(id, s); |
| 65 | } |
| 66 | return s; |
| 67 | } |
| 68 | |
| 69 | private PreloaderSession takeSession(String id) { |
| 70 | PreloaderSession s = mSessions.remove(id); |
| 71 | if (s != null) { |
| 72 | s.cancelTimeout(); |
| 73 | } |
| 74 | return s; |
| 75 | } |
| 76 | |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 77 | public void handlePreloadRequest(String id, String url, Map<String, String> headers, |
| 78 | String searchBoxQuery) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 79 | PreloaderSession s = getSession(id); |
| 80 | s.touch(); // reset timer |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 81 | PreloadedTabControl tab = s.getTabControl(); |
| 82 | if (searchBoxQuery != null) { |
| 83 | tab.loadUrlIfChanged(url, headers); |
| 84 | tab.setQuery(searchBoxQuery); |
| 85 | } else { |
| 86 | tab.loadUrl(url, headers); |
| 87 | } |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | public void discardPreload(String id) { |
| 91 | PreloaderSession s = takeSession(id); |
| 92 | if (s != null) { |
| 93 | if (LOGD_ENABLED) Log.d(LOGTAG, "Discard preload session " + id); |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 94 | PreloadedTabControl t = s.getTabControl(); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 95 | t.destroy(); |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 96 | } else { |
| 97 | if (LOGD_ENABLED) Log.d(LOGTAG, "Ignored discard request " + id); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return a preloaded tab, and remove it from the preloader. This is used when the |
| 103 | * view is about to be displayed. |
| 104 | */ |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 105 | public PreloadedTabControl getPreloadedTab(String id) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 106 | PreloaderSession s = takeSession(id); |
| 107 | if (LOGD_ENABLED) Log.d(LOGTAG, "Showing preload session " + id + "=" + s); |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 108 | return s == null ? null : s.getTabControl(); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | private class PreloaderSession { |
| 112 | private final String mId; |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 113 | private final PreloadedTabControl mTabControl; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 114 | |
| 115 | private final Runnable mTimeoutTask = new Runnable(){ |
| 116 | @Override |
| 117 | public void run() { |
| 118 | if (LOGD_ENABLED) Log.d(LOGTAG, "Preload session timeout " + mId); |
| 119 | discardPreload(mId); |
| 120 | }}; |
| 121 | |
| 122 | public PreloaderSession(String id) { |
| 123 | mId = id; |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 124 | mTabControl = new PreloadedTabControl( |
| 125 | new Tab(new PreloadController(mContext), mFactory.createWebView(false))); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 126 | touch(); |
| 127 | } |
| 128 | |
| 129 | public void cancelTimeout() { |
| 130 | mHandler.removeCallbacks(mTimeoutTask); |
| 131 | } |
| 132 | |
| 133 | public void touch() { |
| 134 | cancelTimeout(); |
| 135 | mHandler.postDelayed(mTimeoutTask, PRERENDER_TIMEOUT_MILLIS); |
| 136 | } |
| 137 | |
Mathew Inwood | 29721c2 | 2011-06-29 17:55:24 +0100 | [diff] [blame] | 138 | public PreloadedTabControl getTabControl() { |
| 139 | return mTabControl; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | } |