Skip to content

Commit

Permalink
Implement chrome://blob-internals with network service.
Browse files Browse the repository at this point in the history
BUG=717714

Review-Url: https://codereview.chromium.org/2865243002
Cr-Commit-Position: refs/heads/master@{#470423}
  • Loading branch information
jam authored and Commit bot committed May 9, 2017
1 parent 069cf2c commit d654ee5
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 30 deletions.
2 changes: 2 additions & 0 deletions content/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ source_set("browser") {
"bad_message.h",
"blob_storage/blob_dispatcher_host.cc",
"blob_storage/blob_dispatcher_host.h",
"blob_storage/blob_internals_url_loader.cc",
"blob_storage/blob_internals_url_loader.h",
"blob_storage/chrome_blob_storage_context.cc",
"blob_storage/chrome_blob_storage_context.h",
"bluetooth/bluetooth_allowed_devices.cc",
Expand Down
63 changes: 63 additions & 0 deletions content/browser/blob_storage/blob_internals_url_loader.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2017 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.

#include "content/browser/blob_storage/blob_internals_url_loader.h"

#include "content/browser/blob_storage/blob_internals_url_loader.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "storage/browser/blob/view_blob_internals_job.h"

namespace content {

void StartBlobInternalsURLLoader(
const ResourceRequest& request,
mojom::URLLoaderClientPtrInfo client_info,
ChromeBlobStorageContext* blob_storage_context) {
scoped_refptr<net::HttpResponseHeaders> headers(
new net::HttpResponseHeaders("HTTP/1.1 200 OK"));
ResourceResponseHead resource_response;
resource_response.headers = headers;
resource_response.mime_type = "text/html";

mojom::URLLoaderClientPtr client;
client.Bind(std::move(client_info));
client->OnReceiveResponse(resource_response, base::nullopt, nullptr);

std::string output = storage::ViewBlobInternalsJob::GenerateHTML(
blob_storage_context->context());

MojoCreateDataPipeOptions options;
options.struct_size = sizeof(MojoCreateDataPipeOptions);
options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
options.element_num_bytes = 1;
options.capacity_num_bytes = output.size();
mojo::DataPipe data_pipe(options);

DCHECK(data_pipe.producer_handle.is_valid());
DCHECK(data_pipe.consumer_handle.is_valid());

void* buffer = nullptr;
uint32_t num_bytes = output.size();
MojoResult result =
BeginWriteDataRaw(data_pipe.producer_handle.get(), &buffer, &num_bytes,
MOJO_WRITE_DATA_FLAG_NONE);
CHECK_EQ(result, MOJO_RESULT_OK);
CHECK_EQ(num_bytes, output.size());

memcpy(buffer, output.c_str(), output.size());
result = EndWriteDataRaw(data_pipe.producer_handle.get(), num_bytes);
CHECK_EQ(result, MOJO_RESULT_OK);

client->OnStartLoadingResponseBody(std::move(data_pipe.consumer_handle));

ResourceRequestCompletionStatus request_complete_data;
request_complete_data.error_code = net::OK;
request_complete_data.exists_in_cache = false;
request_complete_data.completion_time = base::TimeTicks::Now();
request_complete_data.encoded_data_length = output.size();
request_complete_data.encoded_body_length = output.size();
client->OnComplete(request_complete_data);
}

} // namespace content
19 changes: 19 additions & 0 deletions content/browser/blob_storage/blob_internals_url_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2017 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.

#ifndef CONTENT_BROWSER_BLOB_STORAGE_BLOB_INTERNALS_URL_LOADER_H_
#define CONTENT_BROWSER_BLOB_STORAGE_BLOB_INTERNALS_URL_LOADER_H_

#include "content/common/url_loader.mojom.h"

namespace content {
class ChromeBlobStorageContext;

void StartBlobInternalsURLLoader(
const ResourceRequest& request,
mojom::URLLoaderClientPtrInfo client_info,
ChromeBlobStorageContext* blob_storage_context);
} // namespace content

#endif // CONTENT_BROWSER_BLOB_STORAGE_VIEW_BLOB_INTERNALS_URL_LOADER_H_
24 changes: 18 additions & 6 deletions content/browser/webui/web_ui_url_loader_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_piece.h"
#include "content/browser/blob_storage/blob_internals_url_loader.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/frame_host/frame_tree_node.h"
#include "content/browser/frame_host/render_frame_host_impl.h"
#include "content/browser/resource_context_impl.h"
Expand All @@ -20,6 +22,7 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "third_party/zlib/google/compression_utils.h"
#include "ui/base/template_expressions.h"
Expand Down Expand Up @@ -217,10 +220,8 @@ class WebUIURLLoaderFactory : public mojom::URLLoaderFactory,
public:
WebUIURLLoaderFactory(FrameTreeNode* ftn)
: frame_tree_node_id_(ftn->frame_tree_node_id()),
resource_context_(ftn->current_frame_host()
->GetProcess()
->GetBrowserContext()
->GetResourceContext()) {
browser_context_(
ftn->current_frame_host()->GetProcess()->GetBrowserContext()) {
ftn->AddObserver(this);
}

Expand All @@ -238,10 +239,21 @@ class WebUIURLLoaderFactory : public mojom::URLLoaderFactory,
const ResourceRequest& request,
mojom::URLLoaderClientPtr client) override {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (request.url.host_piece() == kChromeUIBlobInternalsHost) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(
&StartBlobInternalsURLLoader, request, client.PassInterface(),
base::Unretained(
ChromeBlobStorageContext::GetFor(browser_context_))));
return;
}

BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&StartURLLoader, request, frame_tree_node_id_,
client.PassInterface(), resource_context_));
client.PassInterface(),
browser_context_->GetResourceContext()));
}

void SyncLoad(int32_t routing_id,
Expand All @@ -258,7 +270,7 @@ class WebUIURLLoaderFactory : public mojom::URLLoaderFactory,

private:
int frame_tree_node_id_;
ResourceContext* resource_context_;
BrowserContext* browser_context_;
mojo::BindingSet<mojom::URLLoaderFactory> loader_factory_bindings_;

DISALLOW_COPY_AND_ASSIGN(WebUIURLLoaderFactory);
Expand Down
50 changes: 27 additions & 23 deletions storage/browser/blob/view_blob_internals_job.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,34 +180,38 @@ int ViewBlobInternalsJob::GetData(
mime_type->assign("text/html");
charset->assign("UTF-8");

data->clear();
StartHTML(data);
if (blob_storage_context_->registry().blob_map_.empty())
data->append(kEmptyBlobStorageMessage);
else
GenerateHTML(data);
EndHTML(data);
*data = GenerateHTML(blob_storage_context_);
return net::OK;
}

void ViewBlobInternalsJob::GenerateHTML(std::string* out) const {
for (auto iter = blob_storage_context_->registry().blob_map_.begin();
iter != blob_storage_context_->registry().blob_map_.end(); ++iter) {
AddHTMLBoldText(iter->first, out);
GenerateHTMLForBlobData(*iter->second, iter->second->content_type(),
iter->second->content_disposition(),
iter->second->refcount(), out);
}
if (!blob_storage_context_->registry().url_to_uuid_.empty()) {
AddHorizontalRule(out);
for (auto iter = blob_storage_context_->registry().url_to_uuid_.begin();
iter != blob_storage_context_->registry().url_to_uuid_.end(); ++iter) {
AddHTMLBoldText(iter->first.spec(), out);
StartHTMLList(out);
AddHTMLListItem(kUUID, iter->second, out);
EndHTMLList(out);
std::string ViewBlobInternalsJob::GenerateHTML(
BlobStorageContext* blob_storage_context) {
std::string out;
StartHTML(&out);
if (blob_storage_context->registry().blob_map_.empty()) {
out.append(kEmptyBlobStorageMessage);
} else {
for (auto iter = blob_storage_context->registry().blob_map_.begin();
iter != blob_storage_context->registry().blob_map_.end(); ++iter) {
AddHTMLBoldText(iter->first, &out);
GenerateHTMLForBlobData(*iter->second, iter->second->content_type(),
iter->second->content_disposition(),
iter->second->refcount(), &out);
}
if (!blob_storage_context->registry().url_to_uuid_.empty()) {
AddHorizontalRule(&out);
for (auto iter = blob_storage_context->registry().url_to_uuid_.begin();
iter != blob_storage_context->registry().url_to_uuid_.end();
++iter) {
AddHTMLBoldText(iter->first.spec(), &out);
StartHTMLList(&out);
AddHTMLListItem(kUUID, iter->second, &out);
EndHTMLList(&out);
}
}
}
EndHTML(&out);
return out;
}

void ViewBlobInternalsJob::GenerateHTMLForBlobData(
Expand Down
3 changes: 2 additions & 1 deletion storage/browser/blob/view_blob_internals_job.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ class STORAGE_EXPORT ViewBlobInternalsJob
bool IsRedirectResponse(GURL* location, int* http_status_code) override;
void Kill() override;

static std::string GenerateHTML(BlobStorageContext* blob_storage_context);

private:
~ViewBlobInternalsJob() override;

void GenerateHTML(std::string* out) const;
static void GenerateHTMLForBlobData(const BlobEntry& blob_data,
const std::string& content_type,
const std::string& content_disposition,
Expand Down

0 comments on commit d654ee5

Please sign in to comment.