Flush after LTHI::ReclaimResources when hidden.
[chromium-blink-merge.git] / mojo / shell / loader.cc
blob71d6616130b714ae4e8f03e56b75f5fa1b15e414
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/shell/loader.h"
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/message_loop/message_loop.h"
10 #include "mojo/shell/switches.h"
11 #include "net/base/load_flags.h"
12 #include "net/base/network_delegate.h"
13 #include "net/http/http_response_headers.h"
14 #include "net/url_request/url_fetcher.h"
15 #include "net/url_request/url_request_status.h"
17 namespace mojo {
18 namespace shell {
20 Loader::Delegate::~Delegate() {
23 Loader::Job::Job(const GURL& app_url, Delegate* delegate)
24 : delegate_(delegate) {
25 fetcher_.reset(net::URLFetcher::Create(app_url, net::URLFetcher::GET, this));
28 Loader::Job::~Job() {
31 void Loader::Job::OnURLFetchComplete(const net::URLFetcher* source) {
32 net::URLRequestStatus status = source->GetStatus();
33 if (!status.is_success()) {
34 LOG(ERROR) << "URL fetch didn't succeed: status = " << status.status()
35 << ", error = " << status.error();
36 } else if (source->GetResponseCode() != 200) {
37 LOG(ERROR) << "HTTP response not OK: code = " << source->GetResponseCode();
39 // TODO: Do something else in the error cases?
41 base::FilePath app_path;
42 source->GetResponseAsFilePath(true, &app_path);
43 std::string mime_type;
44 std::string* passed_mime_type =
45 source->GetResponseHeaders()->GetMimeType(&mime_type) ? &mime_type : NULL;
46 delegate_->DidCompleteLoad(source->GetURL(), app_path, passed_mime_type);
49 Loader::Loader(base::SingleThreadTaskRunner* network_runner,
50 base::SingleThreadTaskRunner* file_runner,
51 base::MessageLoopProxy* cache_runner,
52 scoped_ptr<net::NetworkDelegate> network_delegate,
53 base::FilePath base_path)
54 : file_runner_(file_runner),
55 url_request_context_getter_(new URLRequestContextGetter(
56 base_path,
57 network_runner,
58 file_runner,
59 cache_runner,
60 network_delegate.Pass())) {
63 Loader::~Loader() {
66 scoped_ptr<Loader::Job> Loader::Load(const GURL& app_url, Delegate* delegate) {
67 scoped_ptr<Job> job(new Job(app_url, delegate));
68 job->fetcher_->SetRequestContext(url_request_context_getter_.get());
69 #if defined(MOJO_SHELL_DEBUG)
70 base::FilePath tmp_dir;
71 base::GetTempDir(&tmp_dir);
72 // If MOJO_SHELL_DEBUG is set we want to dowload to a well known location.
73 // This makes it easier to do the necessary links so that symbols are found.
74 job->fetcher_->SaveResponseToFileAtPath(
75 tmp_dir.Append("link-me"),
76 file_runner_.get());
77 #else
78 job->fetcher_->SaveResponseToTemporaryFile(file_runner_.get());
79 #endif
80 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableCache))
81 job->fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE);
82 job->fetcher_->Start();
83 return job.Pass();
86 } // namespace shell
87 } // namespace mojo