summaryrefslogtreecommitdiffstats
path: root/mojo/loader/loader.cc
blob: 001dfee0cecc063c0704529b7e0085ec969f0236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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