-
Notifications
You must be signed in to change notification settings - Fork 68
/
resource_util.cc
57 lines (43 loc) · 1.63 KB
/
resource_util.cc
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
// Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "examples/shared/resource_util.h"
#include "include/cef_parser.h"
#include "include/wrapper/cef_stream_resource_handler.h"
namespace shared {
const char kTestOrigin[] = "https://example.com/";
namespace {
// Returns |url| without the query or fragment components, if any.
std::string GetUrlWithoutQueryOrFragment(const std::string& url) {
// Find the first instance of '?' or '#'.
const size_t pos = std::min(url.find('?'), url.find('#'));
if (pos != std::string::npos)
return url.substr(0, pos);
return url;
}
} // namespace
std::string GetResourcePath(const std::string& url) {
if (url.find(kTestOrigin) != 0U)
return std::string();
const std::string& url_no_query = GetUrlWithoutQueryOrFragment(url);
return url_no_query.substr(sizeof(kTestOrigin) - 1);
}
// Determine the mime type based on the |file_path| file extension.
std::string GetMimeType(const std::string& resource_path) {
std::string mime_type;
size_t sep = resource_path.find_last_of(".");
if (sep != std::string::npos) {
mime_type = CefGetMimeType(resource_path.substr(sep + 1));
if (!mime_type.empty())
return mime_type;
}
return "text/html";
}
CefRefPtr<CefResourceHandler> GetResourceHandler(
const std::string& resource_path) {
CefRefPtr<CefStreamReader> reader = GetResourceReader(resource_path);
if (!reader)
return nullptr;
return new CefStreamResourceHandler(GetMimeType(resource_path), reader);
}
} // namespace shared