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 MOJO_APPLICATION_MANAGER_APPLICATION_LOADER_H_
6#define MOJO_APPLICATION_MANAGER_APPLICATION_LOADER_H_
7
8#include "base/memory/ref_counted.h"
9#include "mojo/application_manager/application_manager_export.h"
10#include "mojo/public/cpp/system/core.h"
11#include "mojo/services/public/interfaces/network/url_loader.mojom.h"
12#include "url/gurl.h"
13
14namespace mojo {
15
16class ApplicationManager;
17
18// Interface to allowing loading behavior to be established for schemes,
19// specific urls or as the default.
20// A ApplicationLoader is responsible to using whatever mechanism is appropriate
21// to load the application at url.
22// The handle to the shell is passed to that application so it can bind it to
23// a Shell instance. This will give the Application a way to connect to other
24// apps and services.
25class MOJO_APPLICATION_MANAGER_EXPORT ApplicationLoader {
26 public:
27  class MOJO_APPLICATION_MANAGER_EXPORT LoadCallbacks
28      : public base::RefCounted<LoadCallbacks> {
29   public:
30    // Register the requested application with ApplicationManager. If the
31    // returned handle is valid, it should be used to implement the
32    // mojo::Application interface.
33    virtual ScopedMessagePipeHandle RegisterApplication() = 0;
34
35    // Load the requested application with a content handler.
36    virtual void LoadWithContentHandler(const GURL& content_handler_url,
37                                        URLResponsePtr url_response) = 0;
38
39   protected:
40    friend base::RefCounted<LoadCallbacks>;
41    virtual ~LoadCallbacks() {}
42  };
43
44  // Implements RegisterApplication() by returning a handle that was specified
45  // at construction time. LoadWithContentHandler() is not supported.
46  class MOJO_APPLICATION_MANAGER_EXPORT SimpleLoadCallbacks
47      : public LoadCallbacks {
48   public:
49    SimpleLoadCallbacks(ScopedMessagePipeHandle shell_handle);
50    virtual ScopedMessagePipeHandle RegisterApplication() OVERRIDE;
51    virtual void LoadWithContentHandler(const GURL& content_handler_url,
52                                        URLResponsePtr response) OVERRIDE;
53
54   private:
55    ScopedMessagePipeHandle shell_handle_;
56    virtual ~SimpleLoadCallbacks();
57  };
58
59  virtual ~ApplicationLoader() {}
60
61  // Load the application named |url|. Applications can be loaded two ways:
62  //
63  // 1. |url| can refer directly to a Mojo application. In this case, call
64  //    callbacks->RegisterApplication(). The returned handle should be used to
65  //    implement the mojo.Application interface. Note that the returned handle
66  //    can be invalid in the case where the application has already been
67  //    loaded.
68  //
69  // 2. |url| can refer to some content that can be handled by some other Mojo
70  //    application. In this case, call callbacks->LoadWithContentHandler() and
71  //    specify the URL of the application that should handle the content.
72  //    The specified application must implement the mojo.ContentHandler
73  //    interface.
74  virtual void Load(ApplicationManager* application_manager,
75                    const GURL& url,
76                    scoped_refptr<LoadCallbacks> callbacks) = 0;
77
78  // Called when the Application exits.
79  virtual void OnApplicationError(ApplicationManager* manager,
80                                  const GURL& url) = 0;
81
82 protected:
83  ApplicationLoader() {}
84};
85
86}  // namespace mojo
87
88#endif  // MOJO_APPLICATION_MANAGER_APPLICATION_LOADER_H_
89