1// Copyright 2014 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.
4
5#ifndef CONTENT_RENDERER_MANIFEST_MANIFEST_MANAGER_H_
6#define CONTENT_RENDERER_MANIFEST_MANIFEST_MANAGER_H_
7
8#include <list>
9
10#include "base/callback_forward.h"
11#include "base/memory/scoped_ptr.h"
12#include "content/public/common/manifest.h"
13#include "content/public/renderer/render_frame_observer.h"
14
15class GURL;
16
17namespace blink {
18class WebURLResponse;
19}
20
21namespace content {
22
23class ManifestFetcher;
24
25// The ManifestManager is a helper class that takes care of fetching and parsing
26// the Manifest of the associated RenderFrame. It uses the ManifestFetcher and
27// the ManifestParser in order to do so.
28// There are two expected consumers of this helper: ManifestManagerHost, via IPC
29// messages and callers inside the renderer process. The latter should use
30// GetManifest().
31class ManifestManager : public RenderFrameObserver {
32 public:
33  typedef base::Callback<void(const Manifest&)> GetManifestCallback;
34
35  explicit ManifestManager(RenderFrame* render_frame);
36  virtual ~ManifestManager();
37
38  // Will call the given |callback| with the Manifest associated with the
39  // RenderFrame if any. Will pass an empty Manifest in case of error.
40  void GetManifest(const GetManifestCallback& callback);
41
42  // RenderFrameObserver implementation.
43  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
44  virtual void DidChangeManifest() OVERRIDE;
45
46 private:
47  enum ResolveState {
48    ResolveStateSuccess,
49    ResolveStateFailure
50  };
51
52  // Called when receiving a ManifestManagerMsg_RequestManifest from the browser
53  // process.
54  void OnRequestManifest(int request_id);
55  void OnRequestManifestComplete(int request_id, const Manifest&);
56
57  void FetchManifest();
58  void OnManifestFetchComplete(const GURL& document_url,
59                               const blink::WebURLResponse& response,
60                               const std::string& data);
61  void ResolveCallbacks(ResolveState state);
62
63  scoped_ptr<ManifestFetcher> fetcher_;
64
65  // Whether the RenderFrame may have an associated Manifest. If true, the frame
66  // may have a manifest, if false, it can't have one. This boolean is true when
67  // DidChangeManifest() is called, if it is never called, it means that the
68  // associated document has no <link rel='manifest'>.
69  bool may_have_manifest_;
70
71  // Whether the current Manifest is dirty.
72  bool manifest_dirty_;
73
74  // Current Manifest. Might be outdated if manifest_dirty_ is true.
75  Manifest manifest_;
76
77  std::list<GetManifestCallback> pending_callbacks_;
78
79  DISALLOW_COPY_AND_ASSIGN(ManifestManager);
80};
81
82} // namespace content
83
84#endif // CONTENT_RENDERER_MANIFEST_MANIFEST_MANAGER_H_
85