aboutsummaryrefslogtreecommitdiff
path: root/server/atoms_handler.go
blob: f42cbdb957e772f82d4a4a081749b2ad39f613fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
	"fmt"
	"net/http"
	"strconv"

	"android.googlesource.com/platform/tools/gpu/atom"
	"android.googlesource.com/platform/tools/gpu/database"
	"android.googlesource.com/platform/tools/gpu/log"
	"android.googlesource.com/platform/tools/gpu/service"
)

const (
	captureParamName = "capture"
	contextParamName = "ctx"
)

// AtomsHandler is an HTTP request handler that returns a human-readable description
// of the atoms for a given capture and context.
type atomsHandler struct {
	database.Database
}

// ServeHTTP writes to res a human-readable plain text description for each of the
// atoms of the capture/context identified by their respective parameters, parsed
// from the given req query string.
func (h atomsHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
	captures, err := h.Captures()
	if err != nil {
		panic(err)
	}

	captureName := req.URL.Query().Get(captureParamName)
	captureID, found := captures[captureName]
	if !found {
		http.NotFound(res, req)
		return
	}

	var c service.Capture
	if err := h.Load(captureID, log.Nop{}, &c); err != nil {
		panic(err)
	}

	var stream service.AtomStream
	if err := h.Load(c.Atoms.ID, log.Nop{}, &stream); err != nil {
		panic(err)
	}

	atoms, err := stream.List()
	if err != nil {
		panic(err)
	}

	var contextID atom.ContextID
	if ctx, err := strconv.ParseInt(req.URL.Query().Get(contextParamName), 10, 32); err == nil {
		contextID = atom.ContextID(int(ctx))
	}

	res.Header().Add("Content-Type", "text/plain;charset=UTF-8")

	for i, a := range atoms {
		if a.ContextID() == contextID {
			fmt.Fprintf(res, "%.6d %s\n", i, a)
		}
	}
}