summaryrefslogtreecommitdiffstats
path: root/components/html_viewer/html_document_oopif.h
blob: 593e8dc371fdc4d6f9c895be8268b897d9c2ec9c (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// 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 COMPONENTS_HTML_VIEWER_HTML_DOCUMENT_OOPIF_H_
#define COMPONENTS_HTML_VIEWER_HTML_DOCUMENT_OOPIF_H_

#include <set>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "components/devtools_service/public/interfaces/devtools_service.mojom.h"
#include "components/html_viewer/ax_provider_impl.h"
#include "components/html_viewer/html_frame_delegate.h"
#include "components/html_viewer/public/interfaces/test_html_viewer.mojom.h"
#include "components/view_manager/public/cpp/view_manager_client_factory.h"
#include "components/view_manager/public/cpp/view_manager_delegate.h"
#include "components/view_manager/public/cpp/view_observer.h"
#include "mandoline/tab/public/interfaces/frame_tree.mojom.h"
#include "mojo/application/public/cpp/app_lifetime_helper.h"
#include "mojo/application/public/cpp/interface_factory.h"
#include "mojo/application/public/cpp/service_provider_impl.h"
#include "mojo/application/public/interfaces/application.mojom.h"
#include "mojo/services/network/public/interfaces/url_loader.mojom.h"

namespace base {
class SingleThreadTaskRunner;
}

namespace mojo {
class ViewManager;
class View;
}

namespace html_viewer {

class AxProviderImpl;
class DocumentResourceWaiter;
class GlobalState;
class HTMLFrameTreeManager;
class TestHTMLViewerImpl;
class WebLayerTreeViewImpl;

// A view for a single HTML document.
//
// HTMLDocument is deleted in one of two ways:
// . When the View the HTMLDocument is embedded in is destroyed.
// . Explicitly by way of Destroy().
class HTMLDocumentOOPIF
    : public mojo::ViewManagerDelegate,
      public mojo::ViewObserver,
      public HTMLFrameDelegate,
      public mojo::InterfaceFactory<mojo::AxProvider>,
      public mojo::InterfaceFactory<mandoline::FrameTreeClient>,
      public mojo::InterfaceFactory<TestHTMLViewer>,
      public mojo::InterfaceFactory<devtools_service::DevToolsAgent> {
 public:
  using DeleteCallback = base::Callback<void(HTMLDocumentOOPIF*)>;
  using HTMLFrameCreationCallback =
      base::Callback<HTMLFrame*(HTMLFrame::CreateParams*)>;

  // Load a new HTMLDocument with |response|.
  // |html_document_app| is the application this app was created in, and
  // |connection| the specific connection triggering this new instance.
  // |setup| is used to obtain init type state (such as resources).
  HTMLDocumentOOPIF(mojo::ApplicationImpl* html_document_app,
                    mojo::ApplicationConnection* connection,
                    mojo::URLResponsePtr response,
                    GlobalState* setup,
                    const DeleteCallback& delete_callback,
                    const HTMLFrameCreationCallback& frame_creation_callback);

  // Deletes this object.
  void Destroy();

 private:
  friend class DocumentResourceWaiter;  // So it can call LoadIfNecessary().

  // Requests for interfaces before the document is loaded go here. Once
  // loaded the requests are bound and BeforeLoadCache is deleted.
  struct BeforeLoadCache {
    BeforeLoadCache();
    ~BeforeLoadCache();

    std::set<mojo::InterfaceRequest<mojo::AxProvider>*> ax_provider_requests;
    std::set<mojo::InterfaceRequest<TestHTMLViewer>*> test_interface_requests;
  };

  ~HTMLDocumentOOPIF() override;

  void LoadIfNecessary();
  void Load();

  BeforeLoadCache* GetBeforeLoadCache();

  // ViewManagerDelegate:
  void OnEmbed(mojo::View* root) override;
  void OnViewManagerDestroyed(mojo::ViewManager* view_manager) override;

  // ViewObserver:
  void OnViewViewportMetricsChanged(
      mojo::View* view,
      const mojo::ViewportMetrics& old_metrics,
      const mojo::ViewportMetrics& new_metrics) override;
  void OnViewDestroyed(mojo::View* view) override;

  // HTMLFrameDelegate:
  void OnFrameDidFinishLoad() override;
  mojo::ApplicationImpl* GetApp() override;
  HTMLFrame* CreateHTMLFrame(HTMLFrame::CreateParams* params) override;
  void OnFrameSwappedToRemote() override;

  // mojo::InterfaceFactory<mojo::AxProvider>:
  void Create(mojo::ApplicationConnection* connection,
              mojo::InterfaceRequest<mojo::AxProvider> request) override;

  // mojo::InterfaceFactory<mandoline::FrameTreeClient>:
  void Create(
      mojo::ApplicationConnection* connection,
      mojo::InterfaceRequest<mandoline::FrameTreeClient> request) override;

  // mojo::InterfaceFactory<TestHTMLViewer>:
  void Create(mojo::ApplicationConnection* connection,
              mojo::InterfaceRequest<TestHTMLViewer> request) override;

  // mojo::InterfaceFactory<devtools_service::DevToolsAgent>:
  void Create(
      mojo::ApplicationConnection* connection,
      mojo::InterfaceRequest<devtools_service::DevToolsAgent> request) override;

  scoped_ptr<mojo::AppRefCount> app_refcount_;
  mojo::ApplicationImpl* html_document_app_;
  mojo::ApplicationConnection* connection_;
  mojo::ViewManagerClientFactory view_manager_client_factory_;

  // HTMLDocument owns these pointers; binding requests after document load.
  std::set<AxProviderImpl*> ax_providers_;

  ScopedVector<TestHTMLViewerImpl> test_html_viewers_;

  // Set to true when the local frame has finished loading.
  bool did_finish_local_frame_load_ = false;

  GlobalState* global_state_;

  HTMLFrame* frame_;

  scoped_ptr<DocumentResourceWaiter> resource_waiter_;

  scoped_ptr<BeforeLoadCache> before_load_cache_;

  DeleteCallback delete_callback_;

  HTMLFrameCreationCallback frame_creation_callback_;

  mojo::View* root_;

  // Cache interface request of DevToolsAgent if |frame_| hasn't been
  // initialized.
  mojo::InterfaceRequest<devtools_service::DevToolsAgent>
      devtools_agent_request_;

  DISALLOW_COPY_AND_ASSIGN(HTMLDocumentOOPIF);
};

}  // namespace html_viewer

#endif  // COMPONENTS_HTML_VIEWER_HTML_DOCUMENT_OOPIF_H_