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_HOST_H_
6#define CONTENT_RENDERER_MANIFEST_MANIFEST_MANAGER_HOST_H_
7
8#include "base/callback_forward.h"
9#include "base/id_map.h"
10#include "content/public/browser/web_contents_observer.h"
11
12#if defined(COMPILER_GCC)
13namespace BASE_HASH_NAMESPACE {
14template<>
15struct hash<content::RenderFrameHost*> {
16  uint64 operator()(content::RenderFrameHost* ptr) const {
17    return hash<uint64>()(reinterpret_cast<uint64>(ptr));
18  }
19};
20}
21#endif
22
23namespace content {
24
25class RenderFrameHost;
26class WebContents;
27struct Manifest;
28
29// ManifestManagerHost is a helper class that allows callers to get the Manifest
30// associated with a frame. It handles the IPC messaging with the child process.
31// TODO(mlamouri): keep a cached version and a dirty bit here.
32class ManifestManagerHost : public WebContentsObserver {
33 public:
34  explicit ManifestManagerHost(WebContents* web_contents);
35  virtual ~ManifestManagerHost();
36
37  typedef base::Callback<void(const Manifest&)> GetManifestCallback;
38
39  // Calls the given callback with the manifest associated with the
40  // given RenderFrameHost. If the frame has no manifest or if getting it failed
41  // the callback will have an empty manifest.
42  void GetManifest(RenderFrameHost*, const GetManifestCallback&);
43
44  // WebContentsObserver
45  virtual bool OnMessageReceived(const IPC::Message&,
46                                 RenderFrameHost*) OVERRIDE;
47  virtual void RenderFrameDeleted(RenderFrameHost*) OVERRIDE;
48
49 private:
50  typedef IDMap<GetManifestCallback, IDMapOwnPointer> CallbackMap;
51  typedef base::hash_map<RenderFrameHost*, CallbackMap*> FrameCallbackMap;
52
53  void OnRequestManifestResponse(
54      RenderFrameHost*, int request_id, const Manifest&);
55
56  // Returns the CallbackMap associated with the given RenderFrameHost, or null.
57  CallbackMap* GetCallbackMapForFrame(RenderFrameHost*);
58
59  FrameCallbackMap pending_callbacks_;
60
61  DISALLOW_COPY_AND_ASSIGN(ManifestManagerHost);
62};
63
64} // namespace content
65
66#endif // CONTENT_RENDERER_MANIFEST_MANIFEST_MANAGER_HOST_H_
67