summaryrefslogtreecommitdiffstats
path: root/mojo/application/public/cpp/content_handler_factory.h
blob: f27c1752c37ddfbd6fad5184d91ec9eadee8ec4c (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
// Copyright 2014 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_APPLICATION_PUBLIC_CPP_CONTENT_HANDLER_FACTORY_H_
#define MOJO_APPLICATION_PUBLIC_CPP_CONTENT_HANDLER_FACTORY_H_

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "mojo/application/public/cpp/interface_factory.h"
#include "mojo/application/public/interfaces/content_handler.mojom.h"
#include "mojo/application/public/interfaces/shell.mojom.h"
#include "mojo/services/network/public/interfaces/url_loader.mojom.h"

namespace mojo {

class ContentHandlerFactory : public InterfaceFactory<ContentHandler> {
 public:
  class HandledApplicationHolder {
   public:
    virtual ~HandledApplicationHolder() {}
  };

  class Delegate {
   public:
    virtual ~Delegate() {}
    // Implement this method to create the Application. This method will be
    // called on a new thread. Leaving this method will quit the application.
    virtual void RunApplication(
        InterfaceRequest<Application> application_request,
        URLResponsePtr response) = 0;
  };

  class ManagedDelegate : public Delegate {
   public:
    ~ManagedDelegate() override {}
    // Implement this method to create the Application for the given content.
    // This method will be called on a new thread. The application will be run
    // on this new thread, and the returned value will be kept alive until the
    // application ends.
    virtual scoped_ptr<HandledApplicationHolder> CreateApplication(
        InterfaceRequest<Application> application_request,
        URLResponsePtr response) = 0;

   private:
    void RunApplication(InterfaceRequest<Application> application_request,
                        URLResponsePtr response) override;
  };

  explicit ContentHandlerFactory(Delegate* delegate);
  ~ContentHandlerFactory() override;

 private:
  // From InterfaceFactory:
  void Create(ApplicationConnection* connection,
              InterfaceRequest<ContentHandler> request) override;

  Delegate* delegate_;

  DISALLOW_COPY_AND_ASSIGN(ContentHandlerFactory);
};

template <class A>
class HandledApplicationHolderImpl
    : public ContentHandlerFactory::HandledApplicationHolder {
 public:
  explicit HandledApplicationHolderImpl(A* value) : value_(value) {}

 private:
  scoped_ptr<A> value_;
};

template <class A>
scoped_ptr<ContentHandlerFactory::HandledApplicationHolder>
make_handled_factory_holder(A* value) {
  return make_scoped_ptr(new HandledApplicationHolderImpl<A>(value));
}

}  // namespace mojo

#endif  // MOJO_APPLICATION_PUBLIC_CPP_CONTENT_HANDLER_FACTORY_H_