1 /* 2 * Copyright (C) 2013 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 android.media; 18 19 import android.annotation.Nullable; 20 import android.compat.annotation.UnsupportedAppUsage; 21 import android.os.IBinder; 22 import android.util.Log; 23 24 import com.android.internal.annotations.GuardedBy; 25 26 import java.net.CookieHandler; 27 import java.net.CookieManager; 28 import java.net.CookieStore; 29 import java.net.HttpCookie; 30 import java.util.List; 31 32 /** @hide */ 33 public class MediaHTTPService extends IMediaHTTPService.Stub { 34 private static final String TAG = "MediaHTTPService"; 35 @Nullable private List<HttpCookie> mCookies; 36 private final Object mCookieStoreInitializedLock = new Object(); 37 @GuardedBy("mCookieStoreInitializedLock") 38 private boolean mCookieStoreInitialized = false; 39 MediaHTTPService(@ullable List<HttpCookie> cookies)40 public MediaHTTPService(@Nullable List<HttpCookie> cookies) { 41 mCookies = cookies; 42 Log.v(TAG, "MediaHTTPService(" + this + "): Cookies: " + cookies); 43 } 44 makeHTTPConnection()45 public IMediaHTTPConnection makeHTTPConnection() { 46 47 synchronized (mCookieStoreInitializedLock) { 48 // Only need to do it once for all connections 49 if ( !mCookieStoreInitialized ) { 50 CookieHandler cookieHandler = CookieHandler.getDefault(); 51 if (cookieHandler == null) { 52 cookieHandler = new CookieManager(); 53 CookieHandler.setDefault(cookieHandler); 54 Log.v(TAG, "makeHTTPConnection: CookieManager created: " + cookieHandler); 55 } else { 56 Log.v(TAG, "makeHTTPConnection: CookieHandler (" + cookieHandler + ") exists."); 57 } 58 59 // Applying the bootstrapping cookies 60 if ( mCookies != null ) { 61 if ( cookieHandler instanceof CookieManager ) { 62 CookieManager cookieManager = (CookieManager)cookieHandler; 63 CookieStore store = cookieManager.getCookieStore(); 64 for ( HttpCookie cookie : mCookies ) { 65 try { 66 store.add(null, cookie); 67 } catch ( Exception e ) { 68 Log.v(TAG, "makeHTTPConnection: CookieStore.add" + e); 69 } 70 //for extended debugging when needed 71 //Log.v(TAG, "MediaHTTPConnection adding Cookie[" + cookie.getName() + 72 // "]: " + cookie); 73 } 74 } else { 75 Log.w(TAG, "makeHTTPConnection: The installed CookieHandler is not a " 76 + "CookieManager. Can’t add the provided cookies to the cookie " 77 + "store."); 78 } 79 } // mCookies 80 81 mCookieStoreInitialized = true; 82 83 Log.v(TAG, "makeHTTPConnection(" + this + "): cookieHandler: " + cookieHandler + 84 " Cookies: " + mCookies); 85 } 86 } 87 88 return new MediaHTTPConnection(); 89 } 90 91 @UnsupportedAppUsage createHttpServiceBinderIfNecessary( String path)92 /* package private */static IBinder createHttpServiceBinderIfNecessary( 93 String path) { 94 return createHttpServiceBinderIfNecessary(path, null); 95 } 96 97 // when cookies are provided createHttpServiceBinderIfNecessary( String path, List<HttpCookie> cookies)98 static IBinder createHttpServiceBinderIfNecessary( 99 String path, List<HttpCookie> cookies) { 100 if (path.startsWith("http://") || path.startsWith("https://")) { 101 return (new MediaHTTPService(cookies)).asBinder(); 102 } else if (path.startsWith("widevine://")) { 103 Log.d(TAG, "Widevine classic is no longer supported"); 104 } 105 106 return null; 107 } 108 } 109