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