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