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; | |
6 | |
7 import android.content.Context; | |
8 import android.content.pm.ApplicationInfo; | |
9 import android.content.pm.PackageManager; | |
10 import android.content.pm.PackageManager.NameNotFoundException; | |
11 import android.util.Log; | |
12 | |
13 import org.chromium.base.CalledByNative; | |
14 import org.chromium.content_public.browser.WebContents; | |
15 | |
16 /** | |
17 * Tab Launcher to be used to launch new tabs from background Android Services,
when it is not | |
18 * known whether an activity is available. It will send an intent to launch the
activity. | |
19 * | |
20 * TODO(peter): Have the activity confirm that the tab has been launched. | |
21 */ | |
22 public abstract class ServiceTabLauncher { | |
23 private static final String TAG = ServiceTabLauncher.class.getSimpleName(); | |
24 private static final String SERVICE_TAB_LAUNCHER_KEY = | |
25 "org.chromium.chrome.browser.SERVICE_TAB_LAUNCHER"; | |
26 | |
27 // Name of the extra containing the Id of a tab launch request id. | |
28 public static final String LAUNCH_REQUEST_ID_EXTRA = | |
29 "org.chromium.chrome.browser.ServiceTabLauncher.LAUNCH_REQUEST_ID"; | |
30 | |
31 private static ServiceTabLauncher sInstance; | |
32 | |
33 /** | |
34 * Launches the browser activity and launches a tab for |url|. | |
35 * | |
36 * @param context The context using which the URL is being loaded. | |
37 * @param requestId Id of the request for launching this tab. | |
38 * @param incognito Whether the tab should be launched in incognito mode. | |
39 * @param url The URL which should be launched in a tab. | |
40 * @param disposition The disposition requested by the navigation source. | |
41 * @param referrerUrl URL of the referrer which is opening the page. | |
42 * @param referrerPolicy The referrer policy to consider when applying the r
eferrer. | |
43 * @param extraHeaders Extra headers to apply when requesting the tab's URL. | |
44 * @param postData Post-data to include in the tab URL's request body. | |
45 */ | |
46 @CalledByNative | |
47 public abstract void launchTab(Context context, int requestId, boolean incog
nito, String url, | |
48 int disposition, String referrerUrl, int refe
rrerPolicy, | |
49 String extraHeaders, byte[] postData); | |
50 | |
51 /** | |
52 * Returns the active instance of the ServiceTabLauncher. If none exists, a
meta-data key will | |
53 * be read from the AndroidManifest.xml file to determine the class which im
plements the | |
54 * service tab launcher activity. If that fails, NULL will be returned inste
ad. | |
55 * | |
56 * We need to do this run-around because the ServiceTabLauncher must be avai
lable from | |
57 * background services, where no activity may be alive. Furthermore, no brow
ser shell code has | |
58 * to be involved in the process at all, which means that there's no common
location where we | |
59 * can inject a delegate in the start-up paths. | |
60 * | |
61 * @param Context The application context for the running service. | |
62 */ | |
63 @CalledByNative | |
64 private static ServiceTabLauncher getInstance(Context context) throws Except
ion { | |
65 if (sInstance == null) { | |
66 Class<?> implementation = getServiceTabLauncherClassFromManifest(con
text); | |
67 if (implementation != null) { | |
68 sInstance = (ServiceTabLauncher) implementation.newInstance(); | |
69 } | |
70 } | |
71 | |
72 return sInstance; | |
73 } | |
74 | |
75 /** | |
76 * Reads the SERVICE_TAB_LAUNCHER_KEY from the manifest and returns the Clas
s it | |
77 * refers to. If the class cannot be found, NULL will be returned instead. | |
78 * | |
79 * @param context The application context used to get the package name and m
anager. | |
80 */ | |
81 private static Class<?> getServiceTabLauncherClassFromManifest(Context conte
xt) { | |
82 try { | |
83 ApplicationInfo info = context.getPackageManager().getApplicationInf
o( | |
84 context.getPackageName(), PackageManager.GET_META_DATA); | |
85 String className = info.metaData.getString(SERVICE_TAB_LAUNCHER_KEY)
; | |
86 | |
87 return Class.forName(className); | |
88 } catch (NameNotFoundException e) { | |
89 Log.e(TAG, "Context.getPackage() refers to an invalid package name."
); | |
90 } catch (ClassNotFoundException e) { | |
91 Log.e(TAG, "Invalid value for SERVICE_TAB_LAUNCHER in the Android ma
nifest file."); | |
92 } | |
93 | |
94 return null; | |
95 } | |
96 | |
97 /** | |
98 * To be called by the activity when the WebContents for |requestId| has bee
n created, or has | |
99 * been recycled from previous use. The |webContents| must not yet have star
ted provisional | |
100 * load for the main frame. | |
101 * | |
102 * @param requestId Id of the tab launching request which has been fulfilled
. | |
103 * @param webContents The WebContents instance associated with this request. | |
104 */ | |
105 public static void onWebContentsForRequestAvailable(int requestId, WebConten
ts webContents) { | |
106 nativeOnWebContentsForRequestAvailable(requestId, webContents); | |
107 } | |
108 | |
109 private static native void nativeOnWebContentsForRequestAvailable( | |
110 int requestId, WebContents webContents); | |
111 } | |
OLD | NEW |