summaryrefslogtreecommitdiffstats
path: root/mojo/services/catalog/catalog.h
blob: e4bc6a547e54a160f018882ab27a2f81edea3ede (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2016 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_SERVICES_CATALOG_CATALOG_H_
#define MOJO_SERVICES_CATALOG_CATALOG_H_

#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "base/path_service.h"
#include "base/values.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "mojo/services/catalog/entry.h"
#include "mojo/services/catalog/public/interfaces/catalog.mojom.h"
#include "mojo/services/catalog/public/interfaces/resolver.mojom.h"
#include "mojo/services/catalog/store.h"
#include "mojo/shell/public/cpp/interface_factory.h"
#include "mojo/shell/public/interfaces/shell_resolver.mojom.h"
#include "url/gurl.h"

namespace catalog {

class Store;

class Catalog : public mojom::Resolver,
                public mojo::shell::mojom::ShellResolver,
                public mojom::Catalog {
 public:
  Catalog(base::TaskRunner* blocking_pool, scoped_ptr<Store> store);
  ~Catalog() override;

  void BindResolver(mojom::ResolverRequest request);
  void BindShellResolver(mojo::shell::mojom::ShellResolverRequest request);
  void BindCatalog(mojom::CatalogRequest request);

 private:
  using MojoNameAliasMap =
      std::map<std::string, std::pair<std::string, std::string>>;

  // mojom::Resolver:
  void ResolveResponse(
      mojo::URLResponsePtr response,
      const ResolveResponseCallback& callback) override;
  void ResolveInterfaces(mojo::Array<mojo::String> interfaces,
                         const ResolveInterfacesCallback& callback) override;
  void ResolveMIMEType(const mojo::String& mime_type,
                       const ResolveMIMETypeCallback& callback) override;
  void ResolveProtocolScheme(
      const mojo::String& scheme,
      const ResolveProtocolSchemeCallback& callback) override;

  // mojo::shell::mojom::ShellResolver:
  void ResolveMojoName(const mojo::String& mojo_name,
                       const ResolveMojoNameCallback& callback) override;

  // mojom::Catalog:
  void GetEntries(mojo::Array<mojo::String> names,
                  const GetEntriesCallback& callback) override;

  // Completes resolving a Mojo name from the Shell after the resolved name has
  // been added to the catalog and the manifest read.
  void CompleteResolveMojoName(const std::string& resolved_name,
                               const std::string& qualifier,
                               const ResolveMojoNameCallback& callback);

  bool IsNameInCatalog(const std::string& name) const;

  // Called from ResolveMojoName().
  // Attempts to load a manifest for |name|, reads it and adds its metadata to
  // the catalog.
  void AddNameToCatalog(const std::string& name,
                        const ResolveMojoNameCallback& callback);

  // Populate/serialize the catalog from/to the supplied store.
  void DeserializeCatalog();
  void SerializeCatalog();

  // Construct a catalog entry from |dictionary|.
  scoped_ptr<Entry> DeserializeApplication(
      const base::DictionaryValue* dictionary);

  GURL GetManifestURL(const std::string& name);

  // Called once the manifest has been read. |pm| may be null at this point,
  // but |callback| must be run.
  static void OnReadManifest(base::WeakPtr<Catalog> catalog,
                             const std::string& name,
                             const ResolveMojoNameCallback& callback,
                             scoped_ptr<base::Value> manifest);

  // Called once the manifest is read and |this| hasn't been deleted.
  void OnReadManifestImpl(const std::string& name,
                          const ResolveMojoNameCallback& callback,
                          scoped_ptr<base::Value> manifest);

  base::TaskRunner* blocking_pool_;
  GURL system_package_dir_;

  mojo::BindingSet<mojom::Resolver> resolver_bindings_;
  mojo::BindingSet<mojo::shell::mojom::ShellResolver> shell_resolver_bindings_;
  mojo::BindingSet<mojom::Catalog> catalog_bindings_;

  scoped_ptr<Store> store_;
  std::map<std::string, Entry> catalog_;

  // Used when an app handles multiple names. Maps from app (as name) to name of
  // app that is responsible for handling it. The value is a pair of the name of
  // the handler along with a qualifier.
  MojoNameAliasMap mojo_name_aliases_;

  std::map<std::string, std::string> qualifiers_;

  base::WeakPtrFactory<Catalog> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(Catalog);
};

}  // namespace catalog

#endif  // MOJO_SERVICES_CATALOG_CATALOG_H_