modules/web/src/main/java/com/sun/webkit/network/NetworkContext.java

Print this page
rev 8410 : RT-39421: Security exception in Service.cancel when running sandboxed applet


  23  * questions.
  24  */
  25 
  26 package com.sun.webkit.network;
  27 
  28 import static com.sun.webkit.network.URLs.newURL;
  29 
  30 import java.net.MalformedURLException;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.util.Arrays;
  34 import java.util.concurrent.LinkedBlockingQueue;
  35 import java.util.concurrent.ThreadFactory;
  36 import java.util.concurrent.ThreadPoolExecutor;
  37 import java.util.concurrent.TimeUnit;
  38 import java.util.concurrent.atomic.AtomicInteger;
  39 import java.util.logging.Level;
  40 import java.util.logging.Logger;
  41 
  42 import com.sun.webkit.WebPage;

  43 
  44 final class NetworkContext {
  45 
  46     private static final Logger logger =
  47             Logger.getLogger(NetworkContext.class.getName());
  48 
  49     /**
  50      * The size of the thread pool for asynchronous loaders.
  51      */
  52     private static final int THREAD_POOL_SIZE = 20;
  53 
  54     /**
  55      * The thread pool keep alive time.
  56      */
  57     private static final long THREAD_POOL_KEEP_ALIVE_TIME = 10000L;
  58 
  59     /**
  60      * The default value of the "http.maxConnections" system property.
  61      */
  62     private static final int DEFAULT_HTTP_MAX_CONNECTIONS = 5;


 176 
 177     /**
 178      * Returns the maximum allowed number of connections per host.
 179      */
 180     private static int fwkGetMaximumHTTPConnectionCountPerHost() {
 181         // Our implementation employs HttpURLConnection for all
 182         // HTTP exchanges, so return the value of the "http.maxConnections"
 183         // system property.
 184         int propValue = AccessController.doPrivileged(
 185                 (PrivilegedAction<Integer>) () -> Integer.getInteger("http.maxConnections", -1));
 186         return propValue >= 0 ? propValue : DEFAULT_HTTP_MAX_CONNECTIONS;
 187     }
 188     
 189     /**
 190      * Thread factory for URL loader threads.
 191      */
 192     private static final class URLLoaderThreadFactory implements ThreadFactory {
 193         private final ThreadGroup group;
 194         private final AtomicInteger index = new AtomicInteger(1);
 195 







 196         private URLLoaderThreadFactory() {
 197             SecurityManager sm = System.getSecurityManager();
 198             group = (sm != null) ? sm.getThreadGroup()
 199                     : Thread.currentThread().getThreadGroup();
 200         }
 201 
 202         @Override
 203         public Thread newThread(Runnable r) {



 204             Thread t = new Thread(group, r,
 205                     "URL-Loader-" + index.getAndIncrement());
 206             t.setDaemon(true);
 207             if (t.getPriority() != Thread.NORM_PRIORITY) {
 208                 t.setPriority(Thread.NORM_PRIORITY);
 209             }
 210             return t;



 211         }
 212     }
 213 }


  23  * questions.
  24  */
  25 
  26 package com.sun.webkit.network;
  27 
  28 import static com.sun.webkit.network.URLs.newURL;
  29 
  30 import java.net.MalformedURLException;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.util.Arrays;
  34 import java.util.concurrent.LinkedBlockingQueue;
  35 import java.util.concurrent.ThreadFactory;
  36 import java.util.concurrent.ThreadPoolExecutor;
  37 import java.util.concurrent.TimeUnit;
  38 import java.util.concurrent.atomic.AtomicInteger;
  39 import java.util.logging.Level;
  40 import java.util.logging.Logger;
  41 
  42 import com.sun.webkit.WebPage;
  43 import java.security.Permission;
  44 
  45 final class NetworkContext {
  46 
  47     private static final Logger logger =
  48             Logger.getLogger(NetworkContext.class.getName());
  49 
  50     /**
  51      * The size of the thread pool for asynchronous loaders.
  52      */
  53     private static final int THREAD_POOL_SIZE = 20;
  54 
  55     /**
  56      * The thread pool keep alive time.
  57      */
  58     private static final long THREAD_POOL_KEEP_ALIVE_TIME = 10000L;
  59 
  60     /**
  61      * The default value of the "http.maxConnections" system property.
  62      */
  63     private static final int DEFAULT_HTTP_MAX_CONNECTIONS = 5;


 177 
 178     /**
 179      * Returns the maximum allowed number of connections per host.
 180      */
 181     private static int fwkGetMaximumHTTPConnectionCountPerHost() {
 182         // Our implementation employs HttpURLConnection for all
 183         // HTTP exchanges, so return the value of the "http.maxConnections"
 184         // system property.
 185         int propValue = AccessController.doPrivileged(
 186                 (PrivilegedAction<Integer>) () -> Integer.getInteger("http.maxConnections", -1));
 187         return propValue >= 0 ? propValue : DEFAULT_HTTP_MAX_CONNECTIONS;
 188     }
 189     
 190     /**
 191      * Thread factory for URL loader threads.
 192      */
 193     private static final class URLLoaderThreadFactory implements ThreadFactory {
 194         private final ThreadGroup group;
 195         private final AtomicInteger index = new AtomicInteger(1);
 196 
 197         // Need to assert the modifyThread and modifyThreadGroup permission when
 198         // creating the thread from the URLLoaderThreadFactory, so we can
 199         // create the thread with the desired ThreadGroup.
 200         // Note that this is needed when running as an applet or a web start app.
 201         private static final Permission modifyThreadGroupPerm = new RuntimePermission("modifyThreadGroup");
 202         private static final Permission modifyThreadPerm = new RuntimePermission("modifyThread");
 203 
 204         private URLLoaderThreadFactory() {
 205             SecurityManager sm = System.getSecurityManager();
 206             group = (sm != null) ? sm.getThreadGroup()
 207                     : Thread.currentThread().getThreadGroup();
 208         }
 209 
 210         @Override
 211         public Thread newThread(Runnable r) {
 212             // Assert the modifyThread and modifyThreadGroup permissions
 213             return
 214                 AccessController.doPrivileged((PrivilegedAction<Thread>) () -> {
 215                     Thread t = new Thread(group, r,
 216                             "URL-Loader-" + index.getAndIncrement());
 217                     t.setDaemon(true);
 218                     if (t.getPriority() != Thread.NORM_PRIORITY) {
 219                         t.setPriority(Thread.NORM_PRIORITY);
 220                     }
 221                     return t;
 222                 },
 223                 null,
 224                 modifyThreadGroupPerm, modifyThreadPerm);
 225         }
 226     }
 227 }