OLD | NEW |
| (Empty) |
1 // Copyright 2016 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.content.browser; | |
6 | |
7 import android.content.Intent; | |
8 import android.util.SparseArray; | |
9 | |
10 import org.chromium.base.library_loader.LibraryProcessType; | |
11 | |
12 /** | |
13 * Allows specifying the package name for looking up child services | |
14 * configuration and classes into (if it differs from the application | |
15 * package name, like in the case of Android WebView). Also allows | |
16 * specifying additional child service binding flags. | |
17 */ | |
18 public class ChildProcessCreationParams { | |
19 /** ID used for the default params. */ | |
20 public static final int DEFAULT_ID = 0; | |
21 | |
22 private static final Object sLock = new Object(); | |
23 private static final SparseArray<ChildProcessCreationParams> sParamMap = new
SparseArray<>(); | |
24 private static int sNextId = 1; // 0 is reserved for DEFAULT_ID. | |
25 | |
26 /** Register default params. This should be called once on start up. */ | |
27 public static void registerDefault(ChildProcessCreationParams params) { | |
28 synchronized (sLock) { | |
29 // TODO(boliu): Assert not overwriting existing entry once WebApk is
fixed. | |
30 sParamMap.append(DEFAULT_ID, params); | |
31 } | |
32 } | |
33 | |
34 // TODO(boliu): Make package visible once WebApk is fixed. | |
35 public static ChildProcessCreationParams getDefault() { | |
36 return get(DEFAULT_ID); | |
37 } | |
38 | |
39 /** Register new params. Returns the allocated ID corresponding this params.
*/ | |
40 public static int register(ChildProcessCreationParams params) { | |
41 assert params != null; | |
42 int id = -1; | |
43 synchronized (sLock) { | |
44 id = sNextId++; | |
45 sParamMap.append(id, params); | |
46 } | |
47 assert id > 0; | |
48 return id; | |
49 } | |
50 | |
51 /** Releases param corresponding to this ID. Any future use of this ID will
crash. */ | |
52 public static void unregister(int id) { | |
53 assert id > DEFAULT_ID; // Not allowed to unregister default. | |
54 synchronized (sLock) { | |
55 sParamMap.delete(id); | |
56 } | |
57 } | |
58 | |
59 static ChildProcessCreationParams get(int id) { | |
60 assert id >= 0; | |
61 synchronized (sLock) { | |
62 return sParamMap.get(id); | |
63 } | |
64 } | |
65 | |
66 // Members should all be immutable to avoid worrying about thread safety. | |
67 private final String mPackageName; | |
68 private final boolean mIsExternalService; | |
69 private final int mLibraryProcessType; | |
70 | |
71 public ChildProcessCreationParams(String packageName, boolean isExternalServ
ice, | |
72 int libraryProcessType) { | |
73 mPackageName = packageName; | |
74 mIsExternalService = isExternalService; | |
75 mLibraryProcessType = libraryProcessType; | |
76 } | |
77 | |
78 String getPackageName() { | |
79 return mPackageName; | |
80 } | |
81 | |
82 boolean getIsExternalService() { | |
83 return mIsExternalService; | |
84 } | |
85 | |
86 int getLibraryProcessType() { | |
87 return mLibraryProcessType; | |
88 } | |
89 | |
90 void addIntentExtras(Intent intent) { | |
91 intent.putExtra(ChildProcessConstants.EXTRA_LIBRARY_PROCESS_TYPE, mLibra
ryProcessType); | |
92 } | |
93 | |
94 public static int getLibraryProcessType(Intent intent) { | |
95 return intent.getIntExtra( | |
96 ChildProcessConstants.EXTRA_LIBRARY_PROCESS_TYPE, LibraryProcess
Type.PROCESS_CHILD); | |
97 } | |
98 } | |
OLD | NEW |