OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.chromium.chrome.browser.prerender; |
| 6 |
| 7 import android.app.Service; |
| 8 import android.content.Context; |
| 9 import android.content.Intent; |
| 10 import android.os.AsyncTask; |
| 11 import android.os.IBinder; |
| 12 import android.os.Message; |
| 13 import android.os.Messenger; |
| 14 import android.text.TextUtils; |
| 15 import android.util.Log; |
| 16 |
| 17 import com.google.android.apps.chrome.R; |
| 18 |
| 19 import org.chromium.base.ThreadUtils; |
| 20 import org.chromium.base.annotations.SuppressFBWarnings; |
| 21 import org.chromium.base.library_loader.ProcessInitException; |
| 22 import org.chromium.chrome.browser.ApplicationInitialization; |
| 23 import org.chromium.chrome.browser.ChromeVersionInfo; |
| 24 import org.chromium.chrome.browser.ChromiumApplication; |
| 25 import org.chromium.chrome.browser.WarmupManager; |
| 26 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; |
| 27 import org.chromium.chrome.browser.externalauth.VerifiedHandler; |
| 28 import org.chromium.content.browser.ChildProcessLauncher; |
| 29 |
| 30 /** |
| 31 * A bound service that warms up Chrome and performs actions related with preren
dering urls. |
| 32 */ |
| 33 public class ChromePrerenderService extends Service { |
| 34 public static final int MSG_PRERENDER_URL = 1; |
| 35 public static final int MSG_CANCEL_PRERENDER = 2; |
| 36 public static final String KEY_PRERENDERED_URL = "url_to_prerender"; |
| 37 public static final String KEY_PRERENDER_WIDTH = "prerender_width"; |
| 38 public static final String KEY_PRERENDER_HEIGHT = "prerender_height"; |
| 39 public static final String KEY_REFERRER = "referrer"; |
| 40 |
| 41 private static class LauncherWarmUpTask extends AsyncTask<Context, Void, Voi
d> { |
| 42 @Override |
| 43 protected Void doInBackground(Context... args) { |
| 44 ChildProcessLauncher.warmUp(args[0]); |
| 45 return null; |
| 46 } |
| 47 } |
| 48 |
| 49 /** |
| 50 * Handler of incoming messages from clients. |
| 51 */ |
| 52 class IncomingHandler extends VerifiedHandler { |
| 53 IncomingHandler(Context context) { |
| 54 super(context, ChromeVersionInfo.isLocalBuild() |
| 55 ? 0 : ExternalAuthUtils.FLAG_SHOULD_BE_GOOGLE_SIGNED); |
| 56 } |
| 57 |
| 58 @Override |
| 59 public void handleMessage(Message msg) { |
| 60 ChromePrerenderService.this.handleMessage(msg); |
| 61 } |
| 62 } |
| 63 |
| 64 /** |
| 65 * Target we publish for clients to send messages to IncomingHandler. |
| 66 */ |
| 67 private Messenger mMessenger; |
| 68 |
| 69 /** |
| 70 * When binding to the service, we return an interface to our messenger |
| 71 * for sending messages to the service. |
| 72 */ |
| 73 @SuppressFBWarnings("DM_EXIT") |
| 74 @Override |
| 75 public IBinder onBind(Intent intent) { |
| 76 mMessenger = new Messenger(new IncomingHandler(getApplicationContext()))
; |
| 77 |
| 78 try { |
| 79 new LauncherWarmUpTask().execute(getApplicationContext()); |
| 80 ((ChromiumApplication) getApplication()) |
| 81 .startBrowserProcessesAndLoadLibrariesSync(getApplicationCon
text(), true); |
| 82 |
| 83 ApplicationInitialization.enableFullscreenFlags( |
| 84 getApplicationContext().getResources(), |
| 85 getApplicationContext(), R.dimen.control_container_height); |
| 86 } catch (ProcessInitException e) { |
| 87 Log.e(this.getClass().toString(), |
| 88 "ProcessInitException while starting the browser process"); |
| 89 // Since the library failed to initialize nothing in the application |
| 90 // can work, so kill the whole application not just the activity |
| 91 System.exit(-1); |
| 92 } |
| 93 return mMessenger.getBinder(); |
| 94 } |
| 95 |
| 96 /** |
| 97 * Handle and incoming message from the messenger. Child classes adding new
messages |
| 98 * should extend this call and handle them separately. |
| 99 * @param msg The message to be handled. |
| 100 */ |
| 101 protected void handleMessage(Message msg) { |
| 102 switch (msg.what) { |
| 103 case MSG_PRERENDER_URL: |
| 104 final String url = msg.getData().getString(KEY_PRERENDERED_URL); |
| 105 final String referrer = msg.getData().getString(KEY_REFERRER, ""
); |
| 106 final int width = msg.getData().getInt(KEY_PRERENDER_WIDTH, 0); |
| 107 final int height = msg.getData().getInt(KEY_PRERENDER_HEIGHT, 0)
; |
| 108 if (!TextUtils.isEmpty(url)) { |
| 109 ThreadUtils.runOnUiThread(new Runnable() { |
| 110 @Override |
| 111 public void run() { |
| 112 prerenderUrl(url, referrer, width, height); |
| 113 } |
| 114 }); |
| 115 } |
| 116 break; |
| 117 case MSG_CANCEL_PRERENDER: |
| 118 ThreadUtils.runOnUiThread(new Runnable() { |
| 119 @Override |
| 120 public void run() { |
| 121 WarmupManager.getInstance().cancelCurrentPrerender(); |
| 122 } |
| 123 }); |
| 124 break; |
| 125 default: |
| 126 break; |
| 127 } |
| 128 } |
| 129 |
| 130 private void prerenderUrl(String url, String referrer, int width, int height
) { |
| 131 WarmupManager.getInstance().prerenderUrl(url, referrer, width, height); |
| 132 } |
| 133 } |
OLD | NEW |