Commit 10f9b627 authored by Brett Wilson's avatar Brett Wilson

Add mojo package manager skeleton.

This application downloads a URL to a temp file, unzips it, and then cleans everything up.

IN a future pass, we'll extract a manifest and contents of the module.

BUG=
R=ben@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#296294}
parent c2680c95
...@@ -15,6 +15,7 @@ group("mojo") { ...@@ -15,6 +15,7 @@ group("mojo") {
"//mojo/public", "//mojo/public",
"//mojo/services", "//mojo/services",
"//mojo/shell:mojo_shell", "//mojo/shell:mojo_shell",
"//mojo/tools/package_manager",
] ]
if (is_linux) { if (is_linux) {
......
# Copyright 2014 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.
shared_library("package_manager") {
output_name = "mojo_package_manager"
sources = [
"package_fetcher.cc",
"package_fetcher.h",
"package_manager.cc",
"package_manager_application.cc",
"package_manager_application.h",
"unpacker.cc",
"unpacker.h",
]
deps = [
"//base",
"//mojo/application",
"//mojo/public/c/system:for_shared_library",
"//mojo/public/cpp/utility",
"//mojo/services/public/interfaces/network",
"//third_party/zlib:zip",
"//url",
]
}
include_rules = [
'+third_party/zlib',
]
// Copyright 2014 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 "mojo/tools/package_manager/package_fetcher.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "mojo/services/public/interfaces/network/url_loader.mojom.h"
namespace mojo {
PackageFetcher::PackageFetcher(NetworkService* network_service,
PackageFetcherDelegate* delegate,
const GURL& url)
: delegate_(delegate),
url_(url) {
network_service->CreateURLLoader(Get(&loader_));
URLRequestPtr request(URLRequest::New());
request->url = url.spec();
request->auto_follow_redirects = true;
loader_->Start(request.Pass(),
base::Bind(&PackageFetcher::OnReceivedResponse,
base::Unretained(this)));
}
PackageFetcher::~PackageFetcher() {
}
void PackageFetcher::OnReceivedResponse(URLResponsePtr response) {
if (response->error) {
printf("Got error %d (%s) for %s\n",
response->error->code,
response->error->description.get().c_str(),
url_.spec().c_str());
delegate_->FetchFailed(url_);
return;
}
if (!base::CreateTemporaryFile(&output_file_name_)) {
delegate_->FetchFailed(url_);
return;
}
output_file_.Initialize(output_file_name_,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
if (!output_file_.IsValid()) {
base::DeleteFile(output_file_name_, false);
delegate_->FetchFailed(url_);
// Danger: may be deleted now.
return;
}
body_ = response->body.Pass();
ReadData(MOJO_RESULT_OK);
// Danger: may be deleted now.
}
void PackageFetcher::ReadData(MojoResult) {
char buf[4096];
uint32_t num_bytes = sizeof(buf);
MojoResult result = ReadDataRaw(body_.get(), buf, &num_bytes,
MOJO_READ_DATA_FLAG_NONE);
if (result == MOJO_RESULT_SHOULD_WAIT) {
WaitToReadMore();
} else if (result == MOJO_RESULT_OK) {
if (output_file_.WriteAtCurrentPos(buf, num_bytes) !=
static_cast<int>(num_bytes)) {
// Clean up the output file.
output_file_.Close();
base::DeleteFile(output_file_name_, false);
delegate_->FetchFailed(url_);
// Danger: may be deleted now.
return;
}
WaitToReadMore();
} else if (result == MOJO_RESULT_FAILED_PRECONDITION) {
// Done.
output_file_.Close();
delegate_->FetchSucceeded(url_, output_file_name_);
// Danger: may be deleted now.
}
}
void PackageFetcher::WaitToReadMore() {
handle_watcher_.Start(
body_.get(),
MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE,
base::Bind(&PackageFetcher::ReadData, base::Unretained(this)));
}
} // namespace mojo
// Copyright 2014 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 MOJO_TOOLS_PACKAGE_MANAGER_FETCHER_H_
#define MOJO_TOOLS_PACKAGE_MANAGER_FETCHER_H_
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "mojo/common/handle_watcher.h"
#include "mojo/public/cpp/system/macros.h"
#include "mojo/services/public/interfaces/network/network_service.mojom.h"
#include "mojo/services/public/interfaces/network/url_loader.mojom.h"
#include "url/gurl.h"
namespace base {
class FilePath;
} // namespace base
namespace mojo {
class PackageFetcherDelegate;
class PackageFetcher {
public:
PackageFetcher(NetworkService* network_service,
PackageFetcherDelegate* delegate,
const GURL& url);
virtual ~PackageFetcher();
private:
void OnReceivedResponse(URLResponsePtr response);
void ReadData(MojoResult);
void WaitToReadMore();
PackageFetcherDelegate* delegate_;
// URL of the package.
GURL url_;
URLLoaderPtr loader_;
// Valid once file has started streaming.
ScopedDataPipeConsumerHandle body_;
common::HandleWatcher handle_watcher_;
base::FilePath output_file_name_;
base::File output_file_;
MOJO_DISALLOW_COPY_AND_ASSIGN(PackageFetcher);
};
class PackageFetcherDelegate {
public:
virtual void FetchSucceeded(const GURL& url, const base::FilePath& name) = 0;
virtual void FetchFailed(const GURL& url) = 0;
};
} // namespace mojo
#endif // MOJO_TOOLS_PACKAGE_MANAGER_FETCHER_H_
// Copyright 2014 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 "mojo/application/application_runner_chromium.h"
#include "mojo/public/c/system/main.h"
#include "mojo/tools/package_manager/package_manager_application.h"
MojoResult MojoMain(MojoHandle shell_handle) {
mojo::ApplicationRunnerChromium runner(new mojo::PackageManagerApplication);
return runner.Run(shell_handle);
}
// Copyright 2014 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 "mojo/tools/package_manager/package_manager_application.h"
#include "base/files/file_util.h"
#include "base/message_loop/message_loop.h"
#include "base/stl_util.h"
#include "mojo/public/cpp/application/application_impl.h"
namespace mojo {
PackageManagerApplication::PendingLoad::PendingLoad() {
}
PackageManagerApplication::PendingLoad::~PendingLoad() {
}
PackageManagerApplication::PackageManagerApplication() {
}
PackageManagerApplication::~PackageManagerApplication() {
printf("APPLICATION EXITING\n");
STLDeleteContainerPairSecondPointers(pending_.begin(), pending_.end());
}
void PackageManagerApplication::Initialize(ApplicationImpl* app) {
app->ConnectToService("mojo:mojo_network_service", &network_service_);
printf("Enter URL> ");
char buf[1024];
if (scanf("%1023s", buf) != 1) {
printf("No input, exiting\n");
base::MessageLoop::current()->Quit();
return;
}
GURL url(buf);
if (!url.is_valid()) {
printf("Invalid URL\n");
base::MessageLoop::current()->Quit();
return;
}
PendingLoad* load = new PendingLoad;
load->fetcher.reset(new mojo::PackageFetcher(
network_service_.get(), this, url));
pending_[url] = load;
}
void PackageManagerApplication::PendingLoadComplete(const GURL& url) {
pending_.erase(pending_.find(url));
if (pending_.empty())
base::MessageLoop::current()->Quit();
}
void PackageManagerApplication::FetchSucceeded(
const GURL& url,
const base::FilePath& name) {
PendingLoad* load = pending_.find(url)->second;
if (!load->unpacker.Unpack(name)) {
base::DeleteFile(name, false);
printf("Failed to unpack\n");
PendingLoadComplete(url);
return;
}
base::DeleteFile(name, false);
PendingLoadComplete(url);
}
void PackageManagerApplication::FetchFailed(const GURL& url) {
PendingLoadComplete(url);
}
} // namespace mojo
// Copyright 2014 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 MOJO_PACKAGE_MANAGER_PACKAGE_MANAGER_APPLICATION_H_
#define MOJO_PACKAGE_MANAGER_PACKAGE_MANAGER_APPLICATION_H_
#include <map>
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/interface_factory.h"
#include "mojo/public/cpp/system/macros.h"
#include "mojo/services/public/interfaces/network/network_service.mojom.h"
#include "mojo/tools/package_manager/package_fetcher.h"
#include "mojo/tools/package_manager/unpacker.h"
namespace mojo {
class PackageManagerApplication
: public ApplicationDelegate,
public PackageFetcherDelegate {
public:
PackageManagerApplication();
virtual ~PackageManagerApplication();
private:
struct PendingLoad {
PendingLoad();
~PendingLoad();
scoped_ptr<PackageFetcher> fetcher;
Unpacker unpacker;
};
typedef std::map<GURL, PendingLoad*> PendingLoadMap;
// Deletes the pending load entry for the given URL and possibly exits the
// message loop if all loads are done.
void PendingLoadComplete(const GURL& url);
// ApplicationDelegate implementation.
virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE;
// PackageFetcher.
virtual void FetchSucceeded(const GURL& url,
const base::FilePath& name) override;
virtual void FetchFailed(const GURL& url) override;
mojo::NetworkServicePtr network_service_;
PendingLoadMap pending_; // Owning pointers.
MOJO_DISALLOW_COPY_AND_ASSIGN(PackageManagerApplication);
};
} // namespace mojo
#endif // MOJO_PACKAGE_MANAGER_PACKAGE_MANAGER_APPLICATION_H
// Copyright 2014 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 "mojo/tools/package_manager/unpacker.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "third_party/zlib/google/zip.h"
namespace mojo {
Unpacker::Unpacker() {
}
Unpacker::~Unpacker() {
if (!dir_.empty()) {
printf("Deleting %s\n", dir_.value().c_str());
//DeleteFile(dir_, true);
}
}
bool Unpacker::Unpack(const base::FilePath& zip_file) {
DCHECK(zip_file_.empty());
zip_file_ = zip_file;
DCHECK(dir_.empty());
if (!CreateNewTempDirectory(base::FilePath::StringType(), &dir_))
return false;
if (!zip::Unzip(zip_file, dir_)) {
dir_ = base::FilePath();
return false;
}
return true;
}
} // namespace mojo
// Copyright 2014 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 MOJO_TOOLS_PACKAGE_MANAGER_UNPACKER_H_
#define MOJO_TOOLS_PACKAGE_MANAGER_UNPACKER_H_
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "mojo/public/cpp/system/macros.h"
namespace mojo {
// Unzips a package into a temporary folder. The temporary folder will be
// deleted when the object is destroyed.
//
// In the future, this class would probably manage doing the unzip operation on
// a background thread.
class Unpacker {
public:
Unpacker();
~Unpacker();
// Actually does the unpacking, returns true on success.
bool Unpack(const base::FilePath& zip_file);
// The root directory where the package has been unpacked.
const base::FilePath& dir() const { return dir_; }
private:
base::FilePath zip_file_;
base::FilePath dir_;
MOJO_DISALLOW_COPY_AND_ASSIGN(Unpacker);
};
} // namespace mojo
#endif // MOJO_TOOLS_PACKAGE_MANAGER_UNPACKER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment