1/*
2 * Copyright 2008, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef Command_h
27#define Command_h
28
29#include "wtf/MainThread.h"
30#include "wtf/Vector.h"
31
32namespace WebCore {
33class Frame;
34}
35
36using namespace WTF;
37using namespace WebCore;
38
39namespace android {
40
41// WebCore Debug Server
42namespace WDS {
43
44class Connection;
45
46// Command identifier length
47#define COMMAND_LENGTH 4
48
49// The dispatcher function called with a Frame for context and the established
50// connection to the client. The connection can be used to read and write to the
51// client application. Return true on successful completion of the command,
52// return false to indicate failure.
53typedef bool (*DispatchFunction)(const Frame*, const Connection*);
54
55// Note: Although the type is named MainThreadFunction, it may not always be
56// the main thread. The type is generic enough to reuse here but named
57// something more appropriate.
58typedef MainThreadFunction TargetThreadFunction;
59
60// Helper class to dipatch functions on a particular thread.
61class Handler {
62public:
63    virtual ~Handler() {}
64    virtual void post(TargetThreadFunction, void*) const = 0;
65};
66
67// Class for containing information about particular commands.
68class Command {
69public:
70    Command(const char* name, const char* desc, const DispatchFunction func,
71            const Handler& handler)
72        : m_name(name)
73        , m_description(desc)
74        , m_dispatch(func)
75        , m_handler(handler) {}
76    Command(const Command& comm)
77        : m_name(comm.m_name)
78        , m_description(comm.m_description)
79        , m_dispatch(comm.m_dispatch)
80        , m_handler(comm.m_handler) {}
81    virtual ~Command() {}
82
83    // Initialize the debug server commands
84    static void Init();
85
86    // Find the command specified by the client request.
87    static Command* Find(const Connection* conn);
88
89    // Dispatch this command
90    void dispatch();
91
92    const char* name() const { return m_name; }
93
94protected:
95    const char* m_name;
96    const char* m_description;
97    const DispatchFunction m_dispatch;
98
99private:
100    const Handler& m_handler;
101    static Vector<const Command*>* s_commands;
102};
103
104} // end namespace WDS
105
106} // end namespace android
107
108#endif
109