summaryrefslogtreecommitdiffstats
path: root/mojo
diff options
context:
space:
mode:
authorabarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-10-18 22:07:45 +0000
committerabarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-10-18 22:07:45 +0000
commit2410e4c01a215a487490e7bf87c4369cb8c13e53 (patch)
tree8b4c6460eca9616a9bdb5032f8253ddb7590e114 /mojo
parent980969549b2d830274e99d8919d6bb2fa0d82b5e (diff)
downloadchromium_src-2410e4c01a215a487490e7bf87c4369cb8c13e53.zip
chromium_src-2410e4c01a215a487490e7bf87c4369cb8c13e53.tar.gz
chromium_src-2410e4c01a215a487490e7bf87c4369cb8c13e53.tar.bz2
Teach mojo_shell how to load http URLs
After this CL, we can load content over HTTP. In order to make that work, I needed to introduce a number of basic concepts into the shell, including an IO thread and a directory to store data (such as the HTTP cache). I've tried to keep the net dependencies separated from the bulk of the mojo_shell code because I expect we'll want to move the net dependencies to an app that runs on top of the Mojo platform rather than linked into the mojo_shell itself. To that end, the mojo::loader::Loader class doesn't leak any net headers to its clients. The version of the loader in this CL is extremely basic. We'll undoubtedly make it fancier as time goes on. The next step after this CL is to store the result in a file and then load that file as a shared object. R=aa@chromium.org, darin@chromium.org Review URL: https://codereview.chromium.org/27943002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@229481 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo')
-rw-r--r--mojo/loader/DEPS4
-rw-r--r--mojo/loader/job.cc20
-rw-r--r--mojo/loader/job.h37
-rw-r--r--mojo/loader/loader.cc81
-rw-r--r--mojo/loader/loader.h36
-rw-r--r--mojo/loader/url_request_context_getter.cc94
-rw-r--r--mojo/loader/url_request_context_getter.h45
-rw-r--r--mojo/mojo.gyp11
-rw-r--r--mojo/shell/app_container.cc3
-rw-r--r--mojo/shell/app_container.h1
-rw-r--r--mojo/shell/shell.cc6
-rw-r--r--mojo/shell/storage.cc52
-rw-r--r--mojo/shell/storage.h32
-rw-r--r--mojo/shell/task_runners.cc25
-rw-r--r--mojo/shell/task_runners.h39
15 files changed, 485 insertions, 1 deletions
diff --git a/mojo/loader/DEPS b/mojo/loader/DEPS
new file mode 100644
index 0000000..d97699d
--- /dev/null
+++ b/mojo/loader/DEPS
@@ -0,0 +1,4 @@
+include_rules = [
+ "+net",
+ "+url",
+]
diff --git a/mojo/loader/job.cc b/mojo/loader/job.cc
new file mode 100644
index 0000000..a89030e
--- /dev/null
+++ b/mojo/loader/job.cc
@@ -0,0 +1,20 @@
+// Copyright (c) 2013 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/loader/job.h"
+
+namespace mojo {
+namespace loader {
+
+Job::Delegate::~Delegate() {
+}
+
+Job::Job() {
+}
+
+Job::~Job() {
+}
+
+} // namespace loader
+} // namespace mojo
diff --git a/mojo/loader/job.h b/mojo/loader/job.h
new file mode 100644
index 0000000..f216331
--- /dev/null
+++ b/mojo/loader/job.h
@@ -0,0 +1,37 @@
+// Copyright (c) 2013 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_LOADER_JOB_H_
+#define MOJO_LOADER_JOB_H_
+
+#include "url/gurl.h"
+
+namespace mojo {
+namespace loader {
+
+// A job represents an individual network load operation.
+class Job {
+ public:
+ class Delegate {
+ public:
+ virtual void DidCompleteLoad(const GURL& app_url) = 0;
+
+ protected:
+ virtual ~Delegate();
+ };
+
+ // You can cancel a job by deleting it.
+ virtual ~Job();
+
+ protected:
+ // You can create a job using Loader.
+ Job();
+
+ DISALLOW_COPY_AND_ASSIGN(Job);
+};
+
+} // namespace loader
+} // namespace mojo
+
+#endif // MOJO_LOADER_JOB_H_
diff --git a/mojo/loader/loader.cc b/mojo/loader/loader.cc
new file mode 100644
index 0000000..001dfee
--- /dev/null
+++ b/mojo/loader/loader.cc
@@ -0,0 +1,81 @@
+// Copyright (c) 2013 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/loader/loader.h"
+
+#include "base/message_loop/message_loop.h"
+#include "base/threading/thread.h"
+#include "mojo/loader/url_request_context_getter.h"
+#include "net/url_request/url_fetcher.h"
+#include "net/url_request/url_fetcher_delegate.h"
+
+namespace mojo {
+namespace loader {
+
+namespace {
+
+class JobImpl : public net::URLFetcherDelegate, public Job {
+ public:
+ JobImpl(const GURL& app_url, Job::Delegate* delegate);
+ virtual ~JobImpl();
+
+ virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
+
+ net::URLFetcher* fetcher() const { return fetcher_.get(); }
+
+ private:
+ scoped_ptr<net::URLFetcher> fetcher_;
+ Job::Delegate* delegate_;
+
+ DISALLOW_COPY_AND_ASSIGN(JobImpl);
+};
+
+JobImpl::JobImpl(const GURL& app_url, Job::Delegate* delegate)
+ : delegate_(delegate) {
+ fetcher_.reset(net::URLFetcher::Create(app_url, net::URLFetcher::GET, this));
+}
+
+JobImpl::~JobImpl() {
+}
+
+void JobImpl::OnURLFetchComplete(const net::URLFetcher* source) {
+ delegate_->DidCompleteLoad(source->GetURL());
+}
+
+scoped_ptr<base::Thread> CreateIOThread(const char* name) {
+ scoped_ptr<base::Thread> thread(new base::Thread(name));
+ base::Thread::Options options;
+ options.message_loop_type = base::MessageLoop::TYPE_IO;
+ thread->StartWithOptions(options);
+ return thread.Pass();
+}
+
+} // namespace
+
+class Loader::Data {
+ public:
+ scoped_ptr<base::Thread> cache_thread;
+ scoped_refptr<URLRequestContextGetter> url_request_context_getter;
+};
+
+Loader::Loader(base::SingleThreadTaskRunner* network_runner,
+ base::FilePath base_path)
+ : data_(new Data()) {
+ data_->cache_thread = CreateIOThread("cache_thread");
+ data_->url_request_context_getter = new URLRequestContextGetter(
+ base_path, network_runner, data_->cache_thread->message_loop_proxy());
+}
+
+Loader::~Loader() {
+}
+
+scoped_ptr<Job> Loader::Load(const GURL& app_url, Job::Delegate* delegate) {
+ JobImpl* job = new JobImpl(app_url, delegate);
+ job->fetcher()->SetRequestContext(data_->url_request_context_getter.get());
+ job->fetcher()->Start();
+ return make_scoped_ptr(static_cast<Job*>(job));
+}
+
+} // namespace loader
+} // namespace mojo
diff --git a/mojo/loader/loader.h b/mojo/loader/loader.h
new file mode 100644
index 0000000..3f7b9cb
--- /dev/null
+++ b/mojo/loader/loader.h
@@ -0,0 +1,36 @@
+// Copyright (c) 2013 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_LOADER_LOADER_H_
+#define MOJO_LOADER_LOADER_H_
+
+#include "base/files/file_path.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/single_thread_task_runner.h"
+#include "mojo/loader/job.h"
+#include "url/gurl.h"
+
+namespace mojo {
+namespace loader {
+
+class Loader {
+ public:
+ Loader(base::SingleThreadTaskRunner* network_runner,
+ base::FilePath base_path);
+ ~Loader();
+
+ scoped_ptr<Job> Load(const GURL& app_url, Job::Delegate* delegate);
+
+ private:
+ class Data;
+
+ scoped_ptr<Data> data_;
+
+ DISALLOW_COPY_AND_ASSIGN(Loader);
+};
+
+} // namespace loader
+} // namespace mojo
+
+#endif // MOJO_LOADER_LOADER_H_
diff --git a/mojo/loader/url_request_context_getter.cc b/mojo/loader/url_request_context_getter.cc
new file mode 100644
index 0000000..992b36f
--- /dev/null
+++ b/mojo/loader/url_request_context_getter.cc
@@ -0,0 +1,94 @@
+// Copyright (c) 2013 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/loader/url_request_context_getter.h"
+
+#include "net/cookies/cookie_monster.h"
+#include "net/http/http_cache.h"
+#include "net/http/http_network_session.h"
+#include "net/http/http_server_properties_impl.h"
+#include "net/proxy/proxy_service.h"
+#include "net/ssl/ssl_config_service_defaults.h"
+#include "net/url_request/static_http_user_agent_settings.h"
+#include "net/url_request/url_request_context.h"
+#include "net/url_request/url_request_job_factory_impl.h"
+
+namespace mojo {
+namespace loader {
+
+URLRequestContextGetter::URLRequestContextGetter(
+ base::FilePath base_path,
+ base::SingleThreadTaskRunner* network_task_runner,
+ base::MessageLoopProxy* cache_task_runner)
+ : base_path_(base_path),
+ network_task_runner_(network_task_runner),
+ cache_task_runner_(cache_task_runner),
+ net_log_(new net::NetLog()) {
+}
+
+URLRequestContextGetter::~URLRequestContextGetter() {
+}
+
+net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
+ if (!url_request_context_) {
+ url_request_context_.reset(new net::URLRequestContext());
+ url_request_context_->set_net_log(net_log_.get());
+
+ storage_.reset(
+ new net::URLRequestContextStorage(url_request_context_.get()));
+
+ storage_->set_cookie_store(new net::CookieMonster(NULL, NULL));
+ storage_->set_http_user_agent_settings(
+ new net::StaticHttpUserAgentSettings("en-us,en", "Mojo/0.1"));
+
+ storage_->set_proxy_service(net::ProxyService::CreateDirect());
+ storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults);
+ storage_->set_http_server_properties(
+ scoped_ptr<net::HttpServerProperties>(
+ new net::HttpServerPropertiesImpl()));
+ storage_->set_host_resolver(net::HostResolver::CreateDefaultResolver(
+ url_request_context_->net_log()));
+
+ net::HttpNetworkSession::Params network_session_params;
+ network_session_params.net_log =
+ url_request_context_->net_log();
+ network_session_params.proxy_service =
+ url_request_context_->proxy_service();
+ network_session_params.ssl_config_service =
+ url_request_context_->ssl_config_service();
+ network_session_params.http_server_properties =
+ url_request_context_->http_server_properties();
+ network_session_params.host_resolver =
+ url_request_context_->host_resolver();
+
+ base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache"));
+
+ net::HttpCache::DefaultBackend* main_backend =
+ new net::HttpCache::DefaultBackend(
+ net::DISK_CACHE,
+ net::CACHE_BACKEND_DEFAULT,
+ cache_path,
+ 0,
+ cache_task_runner_.get());
+
+ net::HttpCache* main_cache = new net::HttpCache(
+ network_session_params, main_backend);
+ storage_->set_http_transaction_factory(main_cache);
+
+ scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
+ new net::URLRequestJobFactoryImpl());
+
+ storage_->set_job_factory(job_factory.release());
+ }
+
+ return url_request_context_.get();
+}
+
+scoped_refptr<base::SingleThreadTaskRunner>
+URLRequestContextGetter::GetNetworkTaskRunner() const {
+ return network_task_runner_;
+}
+
+} // namespace loader
+} // namespace mojo
diff --git a/mojo/loader/url_request_context_getter.h b/mojo/loader/url_request_context_getter.h
new file mode 100644
index 0000000..146420fc
--- /dev/null
+++ b/mojo/loader/url_request_context_getter.h
@@ -0,0 +1,45 @@
+// Copyright (c) 2013 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_URL_REQUEST_CONTEXT_GETTER_H_
+#define MOJO_URL_REQUEST_CONTEXT_GETTER_H_
+
+#include "base/files/file_path.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop/message_loop_proxy.h"
+#include "net/url_request/url_request_context_getter.h"
+#include "net/url_request/url_request_context_storage.h"
+
+namespace mojo {
+namespace loader {
+
+class URLRequestContextGetter : public net::URLRequestContextGetter {
+ public:
+ URLRequestContextGetter(
+ base::FilePath base_path,
+ base::SingleThreadTaskRunner* network_task_runner,
+ base::MessageLoopProxy* cache_task_runner);
+
+ virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE;
+ virtual scoped_refptr<base::SingleThreadTaskRunner>
+ GetNetworkTaskRunner() const OVERRIDE;
+
+ protected:
+ virtual ~URLRequestContextGetter();
+
+ private:
+ base::FilePath base_path_;
+ scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
+ scoped_refptr<base::MessageLoopProxy> cache_task_runner_;
+ scoped_ptr<net::NetLog> net_log_;
+ scoped_ptr<net::URLRequestContextStorage> storage_;
+ scoped_ptr<net::URLRequestContext> url_request_context_;
+
+ DISALLOW_COPY_AND_ASSIGN(URLRequestContextGetter);
+};
+
+} // namespace loader
+} // namespace mojo
+
+#endif // MOJO_URL_REQUEST_CONTEXT_GETTER_H_
diff --git a/mojo/mojo.gyp b/mojo/mojo.gyp
index c69fc1a..b81d538 100644
--- a/mojo/mojo.gyp
+++ b/mojo/mojo.gyp
@@ -135,14 +135,25 @@
'type': 'executable',
'dependencies': [
'../base/base.gyp:base',
+ '../net/net.gyp:net',
'mojo_system',
],
'sources': [
+ 'loader/job.cc',
+ 'loader/job.h',
+ 'loader/loader.cc',
+ 'loader/loader.h',
+ 'loader/url_request_context_getter.cc',
+ 'loader/url_request_context_getter.h',
'shell/app_container.cc',
'shell/app_container.h',
'shell/shell.cc',
+ 'shell/storage.cc',
+ 'shell/storage.h',
'shell/switches.cc',
'shell/switches.h',
+ 'shell/task_runners.cc',
+ 'shell/task_runners.h',
],
'conditions': [
['OS == "win"', {
diff --git a/mojo/shell/app_container.cc b/mojo/shell/app_container.cc
index 7a1d500..ea47ecd 100644
--- a/mojo/shell/app_container.cc
+++ b/mojo/shell/app_container.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "mojo/shell/app_container.h"
+
#include "base/bind.h"
#include "base/callback_forward.h"
#include "base/files/file_path.h"
@@ -9,7 +11,6 @@
#include "base/thread_task_runner_handle.h"
#include "base/threading/thread.h"
#include "mojo/public/system/core.h"
-#include "mojo/shell/app_container.h"
typedef MojoResult (*MojoMainFunction)(mojo::Handle pipe);
diff --git a/mojo/shell/app_container.h b/mojo/shell/app_container.h
index cc3ab00..7553913 100644
--- a/mojo/shell/app_container.h
+++ b/mojo/shell/app_container.h
@@ -10,6 +10,7 @@
#include "mojo/public/system/core.h"
namespace base {
+class FilePath;
class Thread;
}
diff --git a/mojo/shell/shell.cc b/mojo/shell/shell.cc
index 72f697f..74d6a13 100644
--- a/mojo/shell/shell.cc
+++ b/mojo/shell/shell.cc
@@ -8,7 +8,9 @@
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "mojo/shell/app_container.h"
+#include "mojo/shell/storage.h"
#include "mojo/shell/switches.h"
+#include "mojo/shell/task_runners.h"
#include "mojo/system/core_impl.h"
int main(int argc, char** argv) {
@@ -19,6 +21,10 @@ int main(int argc, char** argv) {
base::MessageLoop message_loop(base::MessageLoop::TYPE_UI);
+ // TODO(abarth): Group these objects into a "context" object.
+ mojo::shell::TaskRunners task_runners(message_loop.message_loop_proxy());
+ mojo::shell::Storage storage;
+
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (!command_line.HasSwitch(switches::kApp)) {
LOG(ERROR) << "No app path specified.";
diff --git a/mojo/shell/storage.cc b/mojo/shell/storage.cc
new file mode 100644
index 0000000..c753ef2
--- /dev/null
+++ b/mojo/shell/storage.cc
@@ -0,0 +1,52 @@
+// Copyright (c) 2013 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/shell/storage.h"
+
+#include "base/environment.h"
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/path_service.h"
+
+#if defined(OS_WIN)
+#include "base/base_paths_win.h"
+#elif defined(OS_LINUX)
+#include "base/nix/xdg_util.h"
+#elif defined(OS_MACOSX)
+#include "base/base_paths_mac.h"
+#endif
+
+namespace mojo {
+namespace shell {
+
+Storage::Storage() {
+#if defined(OS_WIN)
+ CHECK(PathService::Get(base::DIR_LOCAL_APP_DATA, &profile_path_));
+ profile_path_ = profile_path_.Append(std::wstring(L"mojo_shell"));
+#elif defined(OS_LINUX)
+ scoped_ptr<base::Environment> env(base::Environment::Create());
+ base::FilePath config_dir(
+ base::nix::GetXDGDirectory(env.get(),
+ base::nix::kXdgConfigHomeEnvVar,
+ base::nix::kDotConfigDir));
+ profile_path_ = config_dir.Append("mojo_shell");
+#elif defined(OS_MACOSX)
+ CHECK(PathService::Get(base::DIR_APP_DATA, &profile_path_));
+ profile_path_ = profile_path_.Append("Chromium Mojo Shell");
+#elif defined(OS_ANDROID)
+ CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &profile_path_));
+ profile_path_ = profile_path_.Append(FILE_PATH_LITERAL("mojo_shell"));
+#else
+ NOTIMPLEMENTED();
+#endif
+
+ if (!base::PathExists(profile_path_))
+ file_util::CreateDirectory(profile_path_);
+}
+
+Storage::~Storage() {
+}
+
+} // namespace shell
+} // namespace mojo
diff --git a/mojo/shell/storage.h b/mojo/shell/storage.h
new file mode 100644
index 0000000..62cb0a2
--- /dev/null
+++ b/mojo/shell/storage.h
@@ -0,0 +1,32 @@
+// Copyright (c) 2013 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_SHELL_STORAGE_H_
+#define MOJO_SHELL_STORAGE_H_
+
+#include "base/files/file_path.h"
+
+namespace mojo {
+namespace shell {
+
+// A object that represents the persistent storage used by the shell.
+class Storage {
+ public:
+ Storage();
+ ~Storage();
+
+ base::FilePath profile_path() const {
+ return profile_path_;
+ }
+
+ private:
+ base::FilePath profile_path_;
+
+ DISALLOW_COPY_AND_ASSIGN(Storage);
+};
+
+} // namespace shell
+} // namespace mojo
+
+#endif // MOJO_SHELL_STORAGE_H_
diff --git a/mojo/shell/task_runners.cc b/mojo/shell/task_runners.cc
new file mode 100644
index 0000000..d1c4053
--- /dev/null
+++ b/mojo/shell/task_runners.cc
@@ -0,0 +1,25 @@
+// Copyright (c) 2013 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/shell/task_runners.h"
+
+#include "base/message_loop/message_loop_proxy.h"
+#include "base/threading/thread.h"
+
+namespace mojo {
+namespace shell {
+
+TaskRunners::TaskRunners(base::SingleThreadTaskRunner* ui_runner)
+ : ui_runner_(ui_runner),
+ io_thread_(new base::Thread("io_thread")) {
+ base::Thread::Options options;
+ options.message_loop_type = base::MessageLoop::TYPE_IO;
+ io_thread_->StartWithOptions(options);
+}
+
+TaskRunners::~TaskRunners() {
+}
+
+} // namespace shell
+} // namespace mojo
diff --git a/mojo/shell/task_runners.h b/mojo/shell/task_runners.h
new file mode 100644
index 0000000..2b0c4db
--- /dev/null
+++ b/mojo/shell/task_runners.h
@@ -0,0 +1,39 @@
+// Copyright (c) 2013 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_SHELL_TASK_RUNNERS_H_
+#define MOJO_SHELL_TASK_RUNNERS_H_
+
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop/message_loop_proxy.h"
+#include "base/threading/thread.h"
+
+namespace mojo {
+namespace shell {
+
+// A context object that contains the common task runners for the shell
+class TaskRunners {
+ public:
+ explicit TaskRunners(base::SingleThreadTaskRunner* ui_runner);
+ ~TaskRunners();
+
+ base::SingleThreadTaskRunner* ui_runner() const {
+ return ui_runner_.get();
+ }
+
+ base::SingleThreadTaskRunner* io_runner() const {
+ return io_thread_->message_loop_proxy();
+ }
+
+ private:
+ scoped_refptr<base::SingleThreadTaskRunner> ui_runner_;
+ scoped_ptr<base::Thread> io_thread_;
+
+ DISALLOW_COPY_AND_ASSIGN(TaskRunners);
+};
+
+} // namespace shell
+} // namespace mojo
+
+#endif // MOJO_SHELL_TASK_RUNNERS_H_