Skip to content

Commit

Permalink
Support setting default headers on HttpDataSource.Factory's
Browse files Browse the repository at this point in the history
Issue: #2166

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=143703258
  • Loading branch information
ojw28 committed Jan 5, 2017
1 parent a77bc8d commit 9d48d4e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.android.exoplayer2.ext.cronet;

import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.HttpDataSource.BaseFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource.Factory;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Predicate;
Expand All @@ -25,7 +26,7 @@
/**
* A {@link Factory} that produces {@link CronetDataSource}.
*/
public final class CronetDataSourceFactory implements Factory {
public final class CronetDataSourceFactory extends BaseFactory {

/**
* The default connection timeout, in milliseconds.
Expand Down Expand Up @@ -67,7 +68,7 @@ public CronetDataSourceFactory(CronetEngine cronetEngine,
}

@Override
public CronetDataSource createDataSource() {
protected CronetDataSource createDataSourceInternal() {
return new CronetDataSource(cronetEngine, executor, contentTypePredicate, transferListener,
connectTimeoutMs, readTimeoutMs, resetTimeoutOnRedirects);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.android.exoplayer2.ext.okhttp;

import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.HttpDataSource.BaseFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource.Factory;
import com.google.android.exoplayer2.upstream.TransferListener;
import okhttp3.CacheControl;
Expand All @@ -24,7 +25,7 @@
/**
* A {@link Factory} that produces {@link OkHttpDataSource}.
*/
public final class OkHttpDataSourceFactory implements Factory {
public final class OkHttpDataSourceFactory extends BaseFactory {

private final Call.Factory callFactory;
private final String userAgent;
Expand Down Expand Up @@ -58,7 +59,7 @@ public OkHttpDataSourceFactory(Call.Factory callFactory, String userAgent,
}

@Override
public OkHttpDataSource createDataSource() {
protected OkHttpDataSource createDataSourceInternal() {
return new OkHttpDataSource(callFactory, userAgent, null, listener, cacheControl);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
package com.google.android.exoplayer2.upstream;

import com.google.android.exoplayer2.upstream.HttpDataSource.BaseFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource.Factory;

/** A {@link Factory} that produces {@link DefaultHttpDataSource} instances. */
public final class DefaultHttpDataSourceFactory implements Factory {
public final class DefaultHttpDataSourceFactory extends BaseFactory {

private final String userAgent;
private final TransferListener<? super DataSource> listener;
Expand Down Expand Up @@ -75,8 +76,9 @@ public DefaultHttpDataSourceFactory(String userAgent,
}

@Override
public DefaultHttpDataSource createDataSource() {
protected DefaultHttpDataSource createDataSourceInternal() {
return new DefaultHttpDataSource(userAgent, null, listener, connectTimeoutMillis,
readTimeoutMillis, allowCrossProtocolRedirects);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

import android.support.annotation.IntDef;
import android.text.TextUtils;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Predicate;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -38,6 +40,86 @@ interface Factory extends DataSource.Factory {
@Override
HttpDataSource createDataSource();

/**
* Sets a default request header field for {@link HttpDataSource} instances subsequently
* created by the factory. Previously created instances are not affected.
*
* @param name The name of the header field.
* @param value The value of the field.
*/
void setDefaultRequestProperty(String name, String value);

/**
* Clears a default request header field for {@link HttpDataSource} instances subsequently
* created by the factory. Previously created instances are not affected.
*
* @param name The name of the header field.
*/
void clearDefaultRequestProperty(String name);

/**
* Clears all default request header fields for all {@link HttpDataSource} instances
* subsequently created by the factory. Previously created instances are not affected.
*/
void clearAllDefaultRequestProperties();

}

/**
* Base implementation of {@link Factory} that sets default request properties.
*/
abstract class BaseFactory implements Factory {

private final HashMap<String, String> requestProperties;

public BaseFactory() {
requestProperties = new HashMap<>();
}

@Override
public final HttpDataSource createDataSource() {
HttpDataSource dataSource = createDataSourceInternal();
synchronized (requestProperties) {
for (Map.Entry<String, String> property : requestProperties.entrySet()) {
dataSource.setRequestProperty(property.getKey(), property.getValue());
}
}
return dataSource;
}

@Override
public final void setDefaultRequestProperty(String name, String value) {
Assertions.checkNotNull(name);
Assertions.checkNotNull(value);
synchronized (requestProperties) {
requestProperties.put(name, value);
}
}

@Override
public final void clearDefaultRequestProperty(String name) {
Assertions.checkNotNull(name);
synchronized (requestProperties) {
requestProperties.remove(name);
}
}

@Override
public final void clearAllDefaultRequestProperties() {
synchronized (requestProperties) {
requestProperties.clear();
}
}

/**
* Called by {@link #createDataSource()} to create a {@link HttpDataSource} instance without
* default request properties set. Default request properties will be set by
* {@link #createDataSource()} before the instance is returned.
*
* @return A {@link HttpDataSource} instance without default request properties set.
*/
protected abstract HttpDataSource createDataSourceInternal();

}

/**
Expand Down

0 comments on commit 9d48d4e

Please sign in to comment.