Skip to content

Commit

Permalink
Move more of ContentHandler handling into PackageManager.
Browse files Browse the repository at this point in the history
Dependency/#include rules required that I restructure the unit tests as part of this, moving all the content handler tests into package_manager also.

R=yzshen@chromium.org
http://crbug.com/533085

Review URL: https://codereview.chromium.org/1358533004

Cr-Commit-Position: refs/heads/master@{#350572}
  • Loading branch information
ben authored and Commit bot committed Sep 24, 2015
1 parent 277f231 commit 885517f
Show file tree
Hide file tree
Showing 24 changed files with 1,252 additions and 1,055 deletions.
2 changes: 1 addition & 1 deletion content/browser/mojo/mojo_shell_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ MojoShellContext::MojoShellContext() {
// Construct with an empty filepath since mojo: urls can't be registered now
// the url scheme registry is locked.
scoped_ptr<mojo::package_manager::PackageManagerImpl> package_manager(
new mojo::package_manager::PackageManagerImpl(base::FilePath()));
new mojo::package_manager::PackageManagerImpl(base::FilePath(), nullptr));
application_manager_.reset(
new mojo::shell::ApplicationManager(package_manager.Pass()));

Expand Down
1 change: 1 addition & 0 deletions mojo/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ group("tests") {

if (!is_component_build) {
deps += [
"//mojo/package_manager:unittests",
"//mojo/runner:apptests",
"//mojo/runner:mojo_runner_unittests",
"//mojo/services/network:apptests",
Expand Down
2 changes: 1 addition & 1 deletion mojo/fetcher/about_fetcher_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class AboutFetcherTest : public testing::Test {
base::FilePath shell_dir;
PathService::Get(base::DIR_MODULE, &shell_dir);
scoped_ptr<package_manager::PackageManagerImpl> package_manager(
new package_manager::PackageManagerImpl(shell_dir));
new package_manager::PackageManagerImpl(shell_dir, nullptr));
package_manager->RegisterContentHandler(
"text/html", GURL("test:html_content_handler"));
application_manager_.reset(
Expand Down
4 changes: 2 additions & 2 deletions mojo/mojo_shell.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
'shell/connect_to_application_params.h',
'shell/connect_util.cc',
'shell/connect_util.h',
'shell/content_handler_connection.cc',
'shell/content_handler_connection.h',
'shell/data_pipe_peek.cc',
'shell/data_pipe_peek.h',
'shell/fetcher.cc',
Expand Down Expand Up @@ -65,6 +63,8 @@
'fetcher/update_fetcher.h',
'fetcher/url_resolver.cc',
'fetcher/url_resolver.h',
'package_manager/content_handler_connection.cc',
'package_manager/content_handler_connection.h',
'package_manager/package_manager_impl.cc',
'package_manager/package_manager_impl.h',
],
Expand Down
24 changes: 24 additions & 0 deletions mojo/package_manager/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//testing/test.gni")

source_set("package_manager") {
sources = [
"content_handler_connection.cc",
"content_handler_connection.h",
"package_manager_impl.cc",
"package_manager_impl.h",
]
Expand All @@ -14,3 +18,23 @@ source_set("package_manager") {
"//mojo/util:filename_util",
]
}

test("unittests") {
output_name = "mojo_package_manager_unittests"

sources = [
"capability_filter_content_handler_unittest.cc",
"content_handler_unittest.cc",
]

deps = [
":package_manager",
"//base",
"//mojo/application/public/cpp",
"//mojo/shell",
"//mojo/shell:test_support",
"//third_party/mojo/src/mojo/edk/test:run_all_unittests",
"//testing/gtest",
"//url",
]
}
158 changes: 158 additions & 0 deletions mojo/package_manager/capability_filter_content_handler_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright 2015 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 "base/path_service.h"
#include "mojo/application/public/cpp/application_connection.h"
#include "mojo/application/public/cpp/application_delegate.h"
#include "mojo/application/public/cpp/application_impl.h"
#include "mojo/application/public/cpp/interface_factory.h"
#include "mojo/application/public/interfaces/content_handler.mojom.h"
#include "mojo/common/weak_binding_set.h"
#include "mojo/package_manager/package_manager_impl.h"
#include "mojo/shell/capability_filter_test.h"
#include "mojo/shell/fetcher.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace package_manager {
namespace test {
namespace {

const char kTestMimeType[] = "test/mime-type";

// A custom Fetcher used to trigger a content handler for kTestMimeType for a
// specific test.
class TestFetcher : public shell::Fetcher {
public:
TestFetcher(const GURL& url, const FetchCallback& callback)
: shell::Fetcher(callback),
url_(url) {
loader_callback_.Run(make_scoped_ptr(this));
}
~TestFetcher() override {}

private:
// Overridden from Fetcher:
const GURL& GetURL() const override { return url_; }
GURL GetRedirectURL() const override { return GURL(); }
GURL GetRedirectReferer() const override { return GURL(); }
URLResponsePtr AsURLResponse(base::TaskRunner* task_runner,
uint32_t skip) override {
URLResponsePtr response(URLResponse::New());
response->url = url_.spec();
return response.Pass();
}
void AsPath(
base::TaskRunner* task_runner,
base::Callback<void(const base::FilePath&, bool)> callback) override {}
std::string MimeType() override { return kTestMimeType; }
bool HasMojoMagic() override { return false; }
bool PeekFirstLine(std::string* line) override { return false; }

const GURL url_;

DISALLOW_COPY_AND_ASSIGN(TestFetcher);
};

class TestPackageManager : public PackageManagerImpl {
public:
TestPackageManager(const base::FilePath& package_path)
: PackageManagerImpl(package_path, nullptr) {}
~TestPackageManager() override {}

private:
// Overridden from PackageManagerImpl:
void FetchRequest(
URLRequestPtr request,
const shell::Fetcher::FetchCallback& loader_callback) override {
new TestFetcher(GURL(request->url), loader_callback);
}

DISALLOW_COPY_AND_ASSIGN(TestPackageManager);
};

class TestContentHandler : public ApplicationDelegate,
public InterfaceFactory<ContentHandler>,
public ContentHandler {
public:
TestContentHandler() : app_(nullptr) {}
~TestContentHandler() override {}

private:
// Overridden from ApplicationDelegate:
void Initialize(ApplicationImpl* app) override {
app_ = app;
}
bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
connection->AddService<ContentHandler>(this);
return true;
}

// Overridden from InterfaceFactory<ContentHandler>:
void Create(ApplicationConnection* connection,
InterfaceRequest<ContentHandler> request) override {
bindings_.AddBinding(this, request.Pass());
}

// Overridden from ContentHandler:
void StartApplication(InterfaceRequest<Application> application,
URLResponsePtr response) override {
scoped_ptr<ApplicationDelegate> delegate(new shell::test::TestApplication);
embedded_apps_.push_back(
new ApplicationImpl(delegate.get(), application.Pass()));
embedded_app_delegates_.push_back(delegate.Pass());
}

ApplicationImpl* app_;
WeakBindingSet<ContentHandler> bindings_;
ScopedVector<ApplicationDelegate> embedded_app_delegates_;
ScopedVector<ApplicationImpl> embedded_apps_;

DISALLOW_COPY_AND_ASSIGN(TestContentHandler);
};

} // namespace

class CapabilityFilterContentHandlerTest
: public shell::test::CapabilityFilterTest {
public:
CapabilityFilterContentHandlerTest()
: package_manager_(nullptr) {
base::FilePath shell_dir;
PathService::Get(base::DIR_MODULE, &shell_dir);
package_manager_ = new TestPackageManager(shell_dir);
}
~CapabilityFilterContentHandlerTest() override {}

private:
// Overridden from CapabilityFilterTest:
shell::PackageManager* CreatePackageManager() override {
return package_manager_;
}
void SetUp() override {
shell::test::CapabilityFilterTest::SetUp();

GURL content_handler_url("test:content_handler");
package_manager_->RegisterContentHandler(kTestMimeType,
content_handler_url);
CreateLoader<TestContentHandler>(content_handler_url.spec());
}

// Owned by ApplicationManager in base class.
PackageManagerImpl* package_manager_;

DISALLOW_COPY_AND_ASSIGN(CapabilityFilterContentHandlerTest);
};

TEST_F(CapabilityFilterContentHandlerTest, Blocking) {
RunBlockingTest();
}

TEST_F(CapabilityFilterContentHandlerTest, Wildcards) {
RunWildcardTest();
}

} // namespace test
} // namespace package_manager
} // namespace mojo
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/shell/content_handler_connection.h"
#include "mojo/package_manager/content_handler_connection.h"

#include "base/memory/scoped_ptr.h"
#include "mojo/shell/application_manager.h"
#include "mojo/shell/connect_to_application_params.h"
#include "mojo/shell/identity.h"

namespace mojo {
namespace shell {
namespace package_manager {

ContentHandlerConnection::ContentHandlerConnection(
ApplicationManager* manager,
const Identity& source,
const Identity& content_handler,
uint32_t id)
: manager_(manager),
shell::ApplicationManager* manager,
const shell::Identity& source,
const shell::Identity& content_handler,
uint32_t id,
const ClosedCallback& connection_closed_callback)
: connection_closed_callback_(connection_closed_callback),
identity_(content_handler),
connection_closed_(false),
id_(id) {
ServiceProviderPtr services;

scoped_ptr<ConnectToApplicationParams> params(new ConnectToApplicationParams);
scoped_ptr<shell::ConnectToApplicationParams> params(
new shell::ConnectToApplicationParams);
params->set_source(source);
params->SetTarget(identity_);
params->set_services(GetProxy(&services));
Expand All @@ -41,7 +43,7 @@ void ContentHandlerConnection::CloseConnection() {
if (connection_closed_)
return;
connection_closed_ = true;
manager_->OnContentHandlerConnectionClosed(this);
connection_closed_callback_.Run(this);
delete this;
}

Expand All @@ -51,5 +53,5 @@ ContentHandlerConnection::~ContentHandlerConnection() {
DCHECK(connection_closed_);
}

} // namespace shell
} // namespace package_manager
} // namespace mojo
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_SHELL_CONTENT_HANDLER_CONNECTION_H_
#define MOJO_SHELL_CONTENT_HANDLER_CONNECTION_H_
#ifndef MOJO_PACKAGE_MANAGER_CONTENT_HANDLER_CONNECTION_H_
#define MOJO_PACKAGE_MANAGER_CONTENT_HANDLER_CONNECTION_H_

#include <string>

#include "base/callback.h"
#include "mojo/application/public/interfaces/content_handler.mojom.h"
#include "mojo/shell/identity.h"
#include "url/gurl.h"

namespace mojo {
namespace shell {

class ApplicationManager;
class Identity;
}
namespace package_manager {

// A ContentHandlerConnection is responsible for creating and maintaining a
// connection to an app which provides the ContentHandler service.
Expand All @@ -25,24 +26,27 @@ class Identity;
// destruction.
class ContentHandlerConnection {
public:
using ClosedCallback = base::Callback<void(ContentHandlerConnection*)>;
// |id| is a unique identifier for this content handler.
ContentHandlerConnection(ApplicationManager* manager,
const Identity& source,
const Identity& content_handler,
uint32_t id);
ContentHandlerConnection(shell::ApplicationManager* manager,
const shell::Identity& source,
const shell::Identity& content_handler,
uint32_t id,
const ClosedCallback& connection_closed_callback);

// Closes the connection and destroys |this| object.
void CloseConnection();

ContentHandler* content_handler() { return content_handler_.get(); }
const Identity& identity() const { return identity_; }
const shell::Identity& identity() const { return identity_; }
uint32_t id() const { return id_; }

private:
~ContentHandlerConnection();

ApplicationManager* manager_;
Identity identity_;
ClosedCallback connection_closed_callback_;
shell::Identity identity_;

ContentHandlerPtr content_handler_;
bool connection_closed_;
// The id for this content handler.
Expand All @@ -51,7 +55,7 @@ class ContentHandlerConnection {
DISALLOW_COPY_AND_ASSIGN(ContentHandlerConnection);
};

} // namespace shell
} // namespace package_manager
} // namespace mojo

#endif // MOJO_SHELL_CONTENT_HANDLER_CONNECTION_H_
#endif // MOJO_PACKAGE_MANAGER_CONTENT_HANDLER_CONNECTION_H_
Loading

0 comments on commit 885517f

Please sign in to comment.