1/*
2 * Copyright (C) 2007 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
17package android.ddm;
18
19import org.apache.harmony.dalvik.ddmc.Chunk;
20import org.apache.harmony.dalvik.ddmc.ChunkHandler;
21import org.apache.harmony.dalvik.ddmc.DdmServer;
22import android.util.Log;
23import java.nio.ByteBuffer;
24
25/**
26 * Handle thread-related traffic.
27 */
28public class DdmHandleNativeHeap extends ChunkHandler {
29
30    public static final int CHUNK_NHGT = type("NHGT");
31
32    private static DdmHandleNativeHeap mInstance = new DdmHandleNativeHeap();
33
34
35    /* singleton, do not instantiate */
36    private DdmHandleNativeHeap() {}
37
38    /**
39     * Register for the messages we're interested in.
40     */
41    public static void register() {
42        DdmServer.registerHandler(CHUNK_NHGT, mInstance);
43    }
44
45    /**
46     * Called when the DDM server connects.  The handler is allowed to
47     * send messages to the server.
48     */
49    public void connected() {}
50
51    /**
52     * Called when the DDM server disconnects.  Can be used to disable
53     * periodic transmissions or clean up saved state.
54     */
55    public void disconnected() {}
56
57    /**
58     * Handle a chunk of data.
59     */
60    public Chunk handleChunk(Chunk request) {
61        Log.i("ddm-nativeheap", "Handling " + name(request.type) + " chunk");
62        int type = request.type;
63
64        if (type == CHUNK_NHGT) {
65            return handleNHGT(request);
66        } else {
67            throw new RuntimeException("Unknown packet "
68                + ChunkHandler.name(type));
69        }
70    }
71
72    /*
73     * Handle a "Native Heap GeT" request.
74     */
75    private Chunk handleNHGT(Chunk request) {
76        //ByteBuffer in = wrapChunk(request);
77
78        byte[] data = getLeakInfo();
79
80        if (data != null) {
81            // wrap & return
82            Log.i("ddm-nativeheap", "Sending " + data.length + " bytes");
83            return new Chunk(ChunkHandler.type("NHGT"), data, 0, data.length);
84        } else {
85            // failed, return a failure error code and message
86            return createFailChunk(1, "Something went wrong");
87        }
88    }
89
90    private native byte[] getLeakInfo();
91}
92
93