| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrCaps.h" | 8 #include "GrCaps.h" |
| 9 #include "GrContextFactory.h" | 9 #include "GrContextFactory.h" |
| 10 | 10 |
| 11 #include "Request.h" | 11 #include "Request.h" |
| 12 #include "Response.h" |
| 12 | 13 |
| 13 #include "SkCanvas.h" | 14 #include "SkCanvas.h" |
| 14 #include "SkCommandLineFlags.h" | 15 #include "SkCommandLineFlags.h" |
| 15 #include "SkJSONCanvas.h" | 16 #include "SkJSONCanvas.h" |
| 16 #include "SkPictureRecorder.h" | 17 #include "SkPictureRecorder.h" |
| 17 #include "SkPixelSerializer.h" | 18 #include "SkPixelSerializer.h" |
| 18 | 19 |
| 19 #include <sys/socket.h> | 20 #include <sys/socket.h> |
| 20 #include <microhttpd.h> | 21 #include <microhttpd.h> |
| 21 | 22 |
| 23 using namespace Response; |
| 24 |
| 22 // To get image decoders linked in we have to do the below magic | 25 // To get image decoders linked in we have to do the below magic |
| 23 #include "SkForceLinking.h" | 26 #include "SkForceLinking.h" |
| 24 #include "SkImageDecoder.h" | 27 #include "SkImageDecoder.h" |
| 25 __SK_FORCE_IMAGE_DECODER_LINKING; | 28 __SK_FORCE_IMAGE_DECODER_LINKING; |
| 26 | 29 |
| 27 DEFINE_string(source, "https://debugger.skia.org", "Where to load the web UI fro
m."); | |
| 28 DEFINE_int32(port, 8888, "The port to listen on."); | 30 DEFINE_int32(port, 8888, "The port to listen on."); |
| 29 | 31 |
| 30 SkString generateTemplate(SkString source) { | |
| 31 SkString debuggerTemplate; | |
| 32 debuggerTemplate.appendf( | |
| 33 "<!DOCTYPE html>\n" | |
| 34 "<html>\n" | |
| 35 "<head>\n" | |
| 36 " <title>SkDebugger</title>\n" | |
| 37 " <meta charset=\"utf-8\" />\n" | |
| 38 " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=egde,chrome=1\">\
n" | |
| 39 " <meta name=\"viewport\" content=\"width=device-width, initial-scale
=1.0\">\n" | |
| 40 " <script src=\"%s/res/js/core.js\" type=\"text/javascript\" charset=
\"utf-8\"></script>\n" | |
| 41 " <link href=\"%s/res/vul/elements.html\" rel=\"import\" />\n" | |
| 42 " <link rel='shortcut icon' href='https://debugger.skia.org/res/img/f
avicon.ico' type='image/x-icon'/ >" | |
| 43 "</head>\n" | |
| 44 "<body class=\"fullbleed layout vertical\">\n" | |
| 45 " <debugger-app-sk>This is the app." | |
| 46 " </debugger-app-sk>\n" | |
| 47 "</body>\n" | |
| 48 "</html>", source.c_str(), source.c_str()); | |
| 49 return debuggerTemplate; | |
| 50 | |
| 51 } | |
| 52 | |
| 53 static const size_t kBufferSize = 1024; | 32 static const size_t kBufferSize = 1024; |
| 54 | 33 |
| 55 static int process_upload_data(void* cls, enum MHD_ValueKind kind, | 34 static int process_upload_data(void* cls, enum MHD_ValueKind kind, |
| 56 const char* key, const char* filename, | 35 const char* key, const char* filename, |
| 57 const char* content_type, const char* transfer_en
coding, | 36 const char* content_type, const char* transfer_en
coding, |
| 58 const char* data, uint64_t off, size_t size) { | 37 const char* data, uint64_t off, size_t size) { |
| 59 struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls); | 38 struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls); |
| 60 | 39 |
| 61 if (0 != size) { | 40 if (0 != size) { |
| 62 uc->fStream.write(data, size); | 41 uc->fStream.write(data, size); |
| 63 } | 42 } |
| 64 return MHD_YES; | 43 return MHD_YES; |
| 65 } | 44 } |
| 66 | 45 |
| 67 // SendOK just sends an empty response with a 200 OK status code. | |
| 68 static int SendOK(MHD_Connection* connection) { | |
| 69 const char* data = ""; | |
| 70 | |
| 71 MHD_Response* response = MHD_create_response_from_buffer(strlen(data), | |
| 72 (void*)data, | |
| 73 MHD_RESPMEM_PERSIST
ENT); | |
| 74 int ret = MHD_queue_response(connection, 200, response); | |
| 75 MHD_destroy_response(response); | |
| 76 return ret; | |
| 77 } | |
| 78 | |
| 79 static int SendError(MHD_Connection* connection, const char* msg) { | |
| 80 MHD_Response* response = MHD_create_response_from_buffer(strlen(msg), | |
| 81 (void*) msg, | |
| 82 MHD_RESPMEM_PERSIST
ENT); | |
| 83 int ret = MHD_queue_response(connection, 500, response); | |
| 84 MHD_destroy_response(response); | |
| 85 return ret; | |
| 86 } | |
| 87 | |
| 88 static int SendData(MHD_Connection* connection, const SkData* data, const char*
type, | |
| 89 bool setContentDisposition = false, const char* dispositionS
tring = nullptr) { | |
| 90 MHD_Response* response = MHD_create_response_from_buffer(data->size(), | |
| 91 const_cast<void*>(d
ata->data()), | |
| 92 MHD_RESPMEM_MUST_CO
PY); | |
| 93 MHD_add_response_header(response, "Content-Type", type); | |
| 94 | |
| 95 if (setContentDisposition) { | |
| 96 MHD_add_response_header(response, "Content-Disposition", dispositionStri
ng); | |
| 97 } | |
| 98 | |
| 99 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response); | |
| 100 MHD_destroy_response(response); | |
| 101 return ret; | |
| 102 } | |
| 103 | |
| 104 static int SendJSON(MHD_Connection* connection, Request* request, int n) { | |
| 105 SkCanvas* canvas = request->getCanvas(); | |
| 106 SkDebugCanvas* debugCanvas = request->fDebugCanvas; | |
| 107 UrlDataManager* urlDataManager = &request->fUrlDataManager; | |
| 108 Json::Value root = debugCanvas->toJSON(*urlDataManager, n, canvas); | |
| 109 root["mode"] = Json::Value(request->fGPUEnabled ? "gpu" : "cpu"); | |
| 110 SkDynamicMemoryWStream stream; | |
| 111 stream.writeText(Json::FastWriter().write(root).c_str()); | |
| 112 | |
| 113 SkAutoTUnref<SkData> data(stream.copyToData()); | |
| 114 return SendData(connection, data, "application/json"); | |
| 115 } | |
| 116 | |
| 117 static int SendTemplate(MHD_Connection* connection, bool redirect = false, | |
| 118 const char* redirectUrl = nullptr) { | |
| 119 SkString debuggerTemplate = generateTemplate(SkString(FLAGS_source[0])); | |
| 120 | |
| 121 MHD_Response* response = MHD_create_response_from_buffer( | |
| 122 debuggerTemplate.size(), | |
| 123 (void*) const_cast<char*>(debuggerTemplate.c_str()), | |
| 124 MHD_RESPMEM_MUST_COPY); | |
| 125 MHD_add_response_header (response, "Access-Control-Allow-Origin", "*"); | |
| 126 | |
| 127 int status = MHD_HTTP_OK; | |
| 128 | |
| 129 if (redirect) { | |
| 130 MHD_add_response_header (response, "Location", redirectUrl); | |
| 131 status = MHD_HTTP_SEE_OTHER; | |
| 132 } | |
| 133 | |
| 134 int ret = MHD_queue_response(connection, status, response); | |
| 135 MHD_destroy_response(response); | |
| 136 return ret; | |
| 137 } | |
| 138 | |
| 139 class UrlHandler { | 46 class UrlHandler { |
| 140 public: | 47 public: |
| 141 virtual ~UrlHandler() {} | 48 virtual ~UrlHandler() {} |
| 142 virtual bool canHandle(const char* method, const char* url) = 0; | 49 virtual bool canHandle(const char* method, const char* url) = 0; |
| 143 virtual int handle(Request* request, MHD_Connection* connection, | 50 virtual int handle(Request* request, MHD_Connection* connection, |
| 144 const char* url, const char* method, | 51 const char* url, const char* method, |
| 145 const char* upload_data, size_t* upload_data_size) = 0; | 52 const char* upload_data, size_t* upload_data_size) = 0; |
| 146 }; | 53 }; |
| 147 | 54 |
| 148 class CmdHandler : public UrlHandler { | 55 class CmdHandler : public UrlHandler { |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 MHD_stop_daemon(daemon); | 547 MHD_stop_daemon(daemon); |
| 641 return 0; | 548 return 0; |
| 642 } | 549 } |
| 643 | 550 |
| 644 #if !defined SK_BUILD_FOR_IOS | 551 #if !defined SK_BUILD_FOR_IOS |
| 645 int main(int argc, char** argv) { | 552 int main(int argc, char** argv) { |
| 646 SkCommandLineFlags::Parse(argc, argv); | 553 SkCommandLineFlags::Parse(argc, argv); |
| 647 return skiaserve_main(); | 554 return skiaserve_main(); |
| 648 } | 555 } |
| 649 #endif | 556 #endif |
| OLD | NEW |