summaryrefslogtreecommitdiffstats
path: root/mojo/shell/fetcher.h
blob: 7b135d9270f361f8312f5111a98befec9fca7087 (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
// 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.

#ifndef MOJO_SHELL_FETCHER_H_
#define MOJO_SHELL_FETCHER_H_

#include "base/callback.h"
#include "base/memory/scoped_ptr.h"

#include "mojo/services/network/public/interfaces/url_loader.mojom.h"

class GURL;

namespace base {
class FilePath;
class TaskRunner;
}

namespace mojo {
namespace shell {

// Fetcher abstracts getting an application by either file or http[s] URL.
//
// Although it is possible to use the Network implementation for http[s] URLs
// (because the underlying net library knows how to handle them), it is
// extremely slow because network responses must be copied to disk in order to
// get a file handle we can use with dlopen.
//
// Until this is solved, we use two different implementations so that
// performance isn't completely absymal.
class Fetcher {
 public:
  // The param will be null in the case where the content could not be fetched.
  // Reasons include:
  // - network error
  // - 4x or 5x HTTP errors
  typedef base::Callback<void(scoped_ptr<Fetcher>)> FetchCallback;

  Fetcher(const FetchCallback& fetch_callback);
  virtual ~Fetcher();

  // Returns the original URL that was fetched.
  virtual const GURL& GetURL() const = 0;

  // If the fetch resulted in a redirect, this returns the final URL after all
  // redirects. Otherwise, it returns an empty URL.
  virtual GURL GetRedirectURL() const = 0;

  // If the fetch resulted in a redirect, this returns the referer URL to use
  // with the redirect.
  virtual GURL GetRedirectReferer() const = 0;

  virtual URLResponsePtr AsURLResponse(base::TaskRunner* task_runner,
                                       uint32_t skip) = 0;

  virtual void AsPath(
      base::TaskRunner* task_runner,
      base::Callback<void(const base::FilePath&, bool)> callback) = 0;

  virtual std::string MimeType() = 0;

  virtual bool HasMojoMagic() = 0;

  virtual bool PeekFirstLine(std::string* line) = 0;

  bool PeekContentHandler(std::string* mojo_shebang,
                          GURL* mojo_content_handler_url);

 protected:
  static const char kMojoMagic[];
  static const size_t kMaxShebangLength;

  FetchCallback loader_callback_;
};

}  // namespace shell
}  // namespace mojo

#endif  // MOJO_SHELL_FETCHER_H_