2012-10-08 19:47:37 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2015-11-26 03:53:12 +01:00
|
|
|
#include "libcef/browser/net/internal_scheme_handler.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
|
2012-10-08 19:47:37 +02:00
|
|
|
#include <string>
|
2016-01-06 20:20:54 +01:00
|
|
|
#include <utility>
|
2015-07-16 23:40:01 +02:00
|
|
|
|
2013-04-16 00:16:01 +02:00
|
|
|
#include "libcef/common/content_client.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "base/strings/string_util.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2015-08-06 20:47:42 +02:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
#include "net/base/mime_util.h"
|
2012-10-08 19:47:37 +02:00
|
|
|
#include "ui/base/resource/resource_bundle.h"
|
|
|
|
|
|
|
|
namespace scheme {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-07-16 23:40:01 +02:00
|
|
|
base::FilePath FilePathFromASCII(const std::string& str) {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
return base::FilePath(base::ASCIIToUTF16(str));
|
|
|
|
#else
|
|
|
|
return base::FilePath(str);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-01 12:32:56 +02:00
|
|
|
std::string GetMimeType(const std::string& filename) {
|
2015-08-06 20:47:42 +02:00
|
|
|
// Requests should not block on the disk! On POSIX this goes to disk.
|
|
|
|
// http://code.google.com/p/chromium/issues/detail?id=59849
|
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
|
|
|
|
2015-07-16 23:40:01 +02:00
|
|
|
std::string mime_type;
|
2015-10-09 17:23:12 +02:00
|
|
|
const base::FilePath& file_path = FilePathFromASCII(filename);
|
|
|
|
if (net::GetMimeTypeFromFile(file_path, &mime_type))
|
2015-07-16 23:40:01 +02:00
|
|
|
return mime_type;
|
|
|
|
|
2015-10-09 17:23:12 +02:00
|
|
|
// Check for newer extensions used by internal resources but not yet
|
|
|
|
// recognized by the mime type detector.
|
|
|
|
const std::string& extension = CefString(file_path.FinalExtension());
|
|
|
|
if (extension == ".woff2")
|
|
|
|
return "application/font-woff2";
|
|
|
|
|
2012-10-08 19:47:37 +02:00
|
|
|
NOTREACHED() << "No known mime type for file: " << filename.c_str();
|
|
|
|
return "text/plain";
|
|
|
|
}
|
|
|
|
|
|
|
|
class RedirectHandler : public CefResourceHandler {
|
|
|
|
public:
|
2017-05-17 11:29:28 +02:00
|
|
|
explicit RedirectHandler(const GURL& url) : url_(url) {}
|
2012-10-08 19:47:37 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2012-10-08 19:47:37 +02:00
|
|
|
callback->Continue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void GetResponseHeaders(CefRefPtr<CefResponse> response,
|
|
|
|
int64& response_length,
|
|
|
|
CefString& redirectUrl) override {
|
2012-10-08 19:47:37 +02:00
|
|
|
response_length = 0;
|
|
|
|
redirectUrl = url_.spec();
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool ReadResponse(void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2012-10-08 19:47:37 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void Cancel() override {}
|
2012-10-08 19:47:37 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
GURL url_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(RedirectHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
class InternalHandler : public CefResourceHandler {
|
|
|
|
public:
|
|
|
|
InternalHandler(const std::string& mime_type,
|
|
|
|
CefRefPtr<CefStreamReader> reader,
|
|
|
|
int size)
|
2017-05-17 11:29:28 +02:00
|
|
|
: mime_type_(mime_type), reader_(reader), size_(size) {}
|
2012-10-08 19:47:37 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2012-10-08 19:47:37 +02:00
|
|
|
callback->Continue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void GetResponseHeaders(CefRefPtr<CefResponse> response,
|
|
|
|
int64& response_length,
|
|
|
|
CefString& redirectUrl) override {
|
2012-10-08 19:47:37 +02:00
|
|
|
response_length = size_;
|
|
|
|
|
|
|
|
response->SetMimeType(mime_type_);
|
|
|
|
response->SetStatus(200);
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool ReadResponse(void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2012-10-08 19:47:37 +02:00
|
|
|
bytes_read = reader_->Read(data_out, 1, bytes_to_read);
|
|
|
|
return (bytes_read > 0);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void Cancel() override {}
|
2012-10-08 19:47:37 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string mime_type_;
|
|
|
|
CefRefPtr<CefStreamReader> reader_;
|
|
|
|
int size_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(InternalHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
class InternalHandlerFactory : public CefSchemeHandlerFactory {
|
|
|
|
public:
|
|
|
|
explicit InternalHandlerFactory(
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<InternalHandlerDelegate> delegate)
|
2017-05-17 11:29:28 +02:00
|
|
|
: delegate_(std::move(delegate)) {}
|
2012-10-08 19:47:37 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefResourceHandler> Create(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
const CefString& scheme_name,
|
|
|
|
CefRefPtr<CefRequest> request) override {
|
2012-10-08 19:47:37 +02:00
|
|
|
GURL url = GURL(request->GetURL().ToString());
|
|
|
|
|
|
|
|
InternalHandlerDelegate::Action action;
|
2015-03-02 21:25:14 +01:00
|
|
|
if (delegate_->OnRequest(browser, request, &action)) {
|
2012-10-08 19:47:37 +02:00
|
|
|
if (!action.redirect_url.is_empty() && action.redirect_url.is_valid())
|
|
|
|
return new RedirectHandler(action.redirect_url);
|
|
|
|
|
|
|
|
if (action.mime_type.empty())
|
|
|
|
action.mime_type = GetMimeType(url.path());
|
|
|
|
|
|
|
|
if (action.resource_id >= 0) {
|
2013-04-16 00:16:01 +02:00
|
|
|
base::StringPiece piece = CefContentClient::Get()->GetDataResource(
|
2012-10-08 19:47:37 +02:00
|
|
|
action.resource_id, ui::SCALE_FACTOR_NONE);
|
|
|
|
if (!piece.empty()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
action.stream = CefStreamReader::CreateForData(
|
|
|
|
const_cast<char*>(piece.data()), piece.size());
|
2012-10-08 19:47:37 +02:00
|
|
|
action.stream_size = piece.size();
|
|
|
|
} else {
|
2017-05-17 11:29:28 +02:00
|
|
|
NOTREACHED() << "Failed to load internal resource for id: "
|
|
|
|
<< action.resource_id << " URL: " << url.spec().c_str();
|
2012-10-08 19:47:37 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.stream.get()) {
|
|
|
|
return new InternalHandler(action.mime_type, action.stream,
|
2017-05-17 11:29:28 +02:00
|
|
|
action.stream_size);
|
2012-10-08 19:47:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<InternalHandlerDelegate> delegate_;
|
2012-10-08 19:47:37 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(InternalHandlerFactory);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
InternalHandlerDelegate::Action::Action() : stream_size(-1), resource_id(-1) {}
|
2012-10-08 19:47:37 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> CreateInternalHandlerFactory(
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<InternalHandlerDelegate> delegate) {
|
2012-10-08 19:47:37 +02:00
|
|
|
DCHECK(delegate.get());
|
2016-01-06 20:20:54 +01:00
|
|
|
return new InternalHandlerFactory(std::move(delegate));
|
2012-10-08 19:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace scheme
|