1 /*
2  * Copyright 2024 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 
17 package androidx.webkit;
18 
19 import org.jspecify.annotations.NonNull;
20 
21 import java.util.concurrent.Executor;
22 
23 /**
24  * Configuration object for
25  * {@link WebViewCompat#startUpWebView(WebViewStartUpConfig, WebViewCompat.WebViewStartUpCallback)}.
26  * <p>
27  * This is different from {@link ProcessGlobalConfig}. This object defines the configuration for
28  * a particular call to
29  * {@link WebViewCompat#startUpWebView(WebViewStartUpConfig, WebViewCompat.WebViewStartUpCallback)}.
30  */
31 @WebViewCompat.ExperimentalAsyncStartUp
32 public final class WebViewStartUpConfig {
33     private final Executor mExecutor;
34     private final boolean mShouldRunUiThreadStartUpTasks;
35 
WebViewStartUpConfig( @onNull Executor executor, boolean shouldRunUiThreadStartUpTasks)36     private WebViewStartUpConfig(
37             @NonNull Executor executor, boolean shouldRunUiThreadStartUpTasks) {
38         mExecutor = executor;
39         mShouldRunUiThreadStartUpTasks = shouldRunUiThreadStartUpTasks;
40     }
41 
getBackgroundExecutor()42     public @NonNull Executor getBackgroundExecutor() {
43         return mExecutor;
44     }
45 
46     /**
47      * Whether to run only parts of startup that doesn't block the UI thread.
48      * <p>
49      * WebView startup tasks that are required to run on the UI thread are not attempted when
50      * {@link WebViewCompat#startUpWebView(WebViewStartUpConfig, WebViewCompat.WebViewStartUpCallback)}
51      * is called if set to {@code false}.
52      * <p>
53      * Defaults to `true`. If not set to `false`, UI thread startup tasks will be
54      * run.
55      */
shouldRunUiThreadStartUpTasks()56     public boolean shouldRunUiThreadStartUpTasks() {
57         return mShouldRunUiThreadStartUpTasks;
58     }
59 
60     @WebViewCompat.ExperimentalAsyncStartUp
61     public static final class Builder {
62         private final Executor mExecutor;
63         private boolean mShouldRunUiThreadStartUpTasks = true;
64 
65         /**
66          * Builder for {@link WebViewStartUpConfig}.
67          *
68          * @param executor The portions of WebView startup that can run on a background
69          * thread are scheduled on this executor. Blocking tasks will be run on the executor.
70          */
Builder(@onNull Executor executor)71         public Builder(@NonNull Executor executor) {
72             mExecutor = executor;
73         }
74 
75         /**
76          * Setter to run only parts of startup that doesn't block the UI thread.
77          * <p>
78          * WebView startup tasks that are required to run on the UI thread are not attempted when
79          * {@link WebViewCompat#startUpWebView(WebViewStartUpConfig, WebViewCompat.WebViewStartUpCallback)}
80          * is called if set to {@code false}.
81          * <p>
82          * Defaults to `true`. If not set to `false`, UI thread startup tasks will be
83          * run.
84          */
setShouldRunUiThreadStartUpTasks( boolean shouldRunUiThreadStartUpTasks)85         public @NonNull Builder setShouldRunUiThreadStartUpTasks(
86                 boolean shouldRunUiThreadStartUpTasks) {
87             mShouldRunUiThreadStartUpTasks = shouldRunUiThreadStartUpTasks;
88             return this;
89         }
90 
91         /**
92          * Build and return a {@link WebViewStartUpConfig} object.
93          *
94          * @return immutable {@link WebViewStartUpConfig} object.
95          */
build()96         public @NonNull WebViewStartUpConfig build() {
97             return new WebViewStartUpConfig(mExecutor, mShouldRunUiThreadStartUpTasks);
98         }
99     }
100 }
101