// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.domokit.oknet;

import android.content.Context;

import com.squareup.okhttp.OkHttpClient;

import org.chromium.mojo.bindings.InterfaceRequest;
import org.chromium.mojo.system.Core;
import org.chromium.mojo.system.DataPipe;
import org.chromium.mojo.system.MessagePipeHandle;
import org.chromium.mojo.system.MojoException;
import org.chromium.mojom.mojo.CookieStore;
import org.chromium.mojom.mojo.NetAddress;
import org.chromium.mojom.mojo.NetworkService;
import org.chromium.mojom.mojo.TcpBoundSocket;
import org.chromium.mojom.mojo.TcpConnectedSocket;
import org.chromium.mojom.mojo.UdpSocket;
import org.chromium.mojom.mojo.UrlLoader;
import org.chromium.mojom.mojo.WebSocket;

/**
 * OkHttp implementation of NetworkService.
 */
public class NetworkServiceImpl implements NetworkService {
    private OkHttpClient mClient;
    private Core mCore;

    public NetworkServiceImpl(Context context, Core core, MessagePipeHandle pipe) {
        assert core != null;
        mCore = core;
        mClient = new OkHttpClient();

        NetworkService.MANAGER.bind(this, pipe);
    }

    @Override
    public void close() {}

    @Override
    public void onConnectionError(MojoException e) {}

    @Override
    public void createUrlLoader(InterfaceRequest<UrlLoader> loader) {
        UrlLoader.MANAGER.bind(new UrlLoaderImpl(mCore, mClient), loader);
    }

    @Override
    public void getCookieStore(InterfaceRequest<CookieStore> cookieStore) {
        cookieStore.close();
    }

    @Override
    public void createWebSocket(InterfaceRequest<WebSocket> socket) {
        socket.close();
    }

    @Override
    public void createTcpBoundSocket(NetAddress localAddress,
            InterfaceRequest<TcpBoundSocket> boundSocket, CreateTcpBoundSocketResponse callback) {
        boundSocket.close();
    }

    @Override
    public void createTcpConnectedSocket(NetAddress remoteAddress,
            DataPipe.ConsumerHandle sendStream, DataPipe.ProducerHandle receiveStream,
            InterfaceRequest<TcpConnectedSocket> clientSocket,
            CreateTcpConnectedSocketResponse callback) {
        sendStream.close();
        receiveStream.close();
        clientSocket.close();
    }

    @Override
    public void createUdpSocket(InterfaceRequest<UdpSocket> socket) {
        socket.close();
    }
}

@@ -219,16 +219,75 @@
 ontext;%0A
+import android.util.Log;%0A%0Aimport com.squareup.okhttp.Cache;
 %0Aimport 
@@ -910,16 +910,66 @@
 ocket;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A%0A
 /**%0A * O
@@ -1084,16 +1084,83 @@
 private 
+static final String TAG = %22NetworkServiceImpl%22;%0A    private static 
 OkHttpCl
@@ -1164,17 +1164,17 @@
 pClient 
-m
+s
 Client;%0A
@@ -1333,24 +1333,25 @@
 = core;%0A
+%0A
         
 mClient 
@@ -1346,37 +1346,472 @@
     
-mClient = new OkHttpClient();
+if (sClient == null) %7B%0A            sClient = new OkHttpClient();%0A%0A            try %7B%0A                int cacheSize = 10 * 1024 * 1024; // 10 MiB%0A                File cacheDirectory = new File(context.getCacheDir(), %22ok_http_cache%22);%0A                Cache cache = new Cache(cacheDirectory, cacheSize);%0A                sClient.setCache(cache);%0A            %7D catch (IOException e) %7B%0A                Log.e(TAG, %22Unable to create HTTP cache%22, e);%0A            %7D%0A        %7D
 %0A%0A  
@@ -2115,17 +2115,17 @@
 (mCore, 
-m
+s
 Client),

Add an HTTP cache to SkyShell