summaryrefslogtreecommitdiffstats
path: root/mojo/services/catalog/catalog.h
blob: 8f949c3219be4aecde56e63ca02e57f197eff21c (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
// 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/reader.h"
#include "mojo/services/catalog/store.h"
#include "mojo/shell/public/cpp/interface_factory.h"
#include "mojo/shell/public/interfaces/shell_resolver.mojom.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;

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

  // Callback for Reader, receives an Entry constructed from the manifest for
  // |name|.
  static void OnReadEntry(base::WeakPtr<Catalog> catalog,
                          const std::string& name,
                          const ResolveMojoNameCallback& callback,
                          scoped_ptr<Entry> entry);
  void OnReadEntryImpl(const std::string& name,
                       const ResolveMojoNameCallback& callback,
                       scoped_ptr<Entry> entry);

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

  scoped_ptr<Reader> reader_;
  base::FilePath package_path_;

  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_