Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / html_viewer / html_document.h
blob501d6887808ca65ce86735410b65c7f8c0efba43
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.
5 #ifndef COMPONENTS_HTML_VIEWER_HTML_DOCUMENT_H_
6 #define COMPONENTS_HTML_VIEWER_HTML_DOCUMENT_H_
8 #include <set>
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "components/devtools_service/public/interfaces/devtools_service.mojom.h"
15 #include "components/html_viewer/ax_provider_impl.h"
16 #include "components/html_viewer/html_frame_delegate.h"
17 #include "components/html_viewer/public/interfaces/test_html_viewer.mojom.h"
18 #include "components/mus/public/cpp/view_tree_delegate.h"
19 #include "components/web_view/public/interfaces/frame.mojom.h"
20 #include "mojo/application/public/cpp/app_lifetime_helper.h"
21 #include "mojo/application/public/cpp/interface_factory.h"
22 #include "mojo/application/public/cpp/service_provider_impl.h"
23 #include "mojo/application/public/interfaces/application.mojom.h"
24 #include "mojo/services/network/public/interfaces/url_loader.mojom.h"
26 namespace base {
27 class SingleThreadTaskRunner;
30 namespace mus {
31 class View;
32 class ViewTreeConnection;
35 namespace html_viewer {
37 class AxProviderImpl;
38 class DocumentResourceWaiter;
39 class GlobalState;
40 class HTMLFactory;
41 class HTMLFrame;
42 class TestHTMLViewerImpl;
43 class ViewTreeDelegateImpl;
44 class WebLayerTreeViewImpl;
46 // A view for a single HTML document.
48 // HTMLDocument is deleted in one of two ways:
49 // . When the View the HTMLDocument is embedded in is destroyed.
50 // . Explicitly by way of Destroy().
51 class HTMLDocument
52 : public mus::ViewTreeDelegate,
53 public HTMLFrameDelegate,
54 public mojo::InterfaceFactory<mojo::AxProvider>,
55 public mojo::InterfaceFactory<web_view::mojom::FrameClient>,
56 public mojo::InterfaceFactory<TestHTMLViewer>,
57 public mojo::InterfaceFactory<devtools_service::DevToolsAgent>,
58 public mojo::InterfaceFactory<mojo::ViewTreeClient> {
59 public:
60 using DeleteCallback = base::Callback<void(HTMLDocument*)>;
62 // Load a new HTMLDocument with |response|.
63 // |html_document_app| is the application this app was created in, and
64 // |connection| the specific connection triggering this new instance.
65 // |setup| is used to obtain init type state (such as resources).
66 HTMLDocument(mojo::ApplicationImpl* html_document_app,
67 mojo::ApplicationConnection* connection,
68 mojo::URLResponsePtr response,
69 GlobalState* setup,
70 const DeleteCallback& delete_callback,
71 HTMLFactory* factory);
73 // Deletes this object.
74 void Destroy();
76 private:
77 friend class DocumentResourceWaiter; // So it can call Load().
79 // Requests for interfaces before the document is loaded go here. Once
80 // loaded the requests are bound and BeforeLoadCache is deleted.
81 struct BeforeLoadCache {
82 BeforeLoadCache();
83 ~BeforeLoadCache();
85 std::set<mojo::InterfaceRequest<mojo::AxProvider>*> ax_provider_requests;
86 std::set<mojo::InterfaceRequest<TestHTMLViewer>*> test_interface_requests;
89 // Any state that needs to be moved when rendering transfers from one frame
90 // to another is stored here.
91 struct TransferableState {
92 TransferableState();
93 ~TransferableState();
95 // Takes the state from |other|.
96 void Move(TransferableState* other);
98 bool owns_view_tree_connection;
99 mus::View* root;
100 scoped_ptr<ViewTreeDelegateImpl> view_tree_delegate_impl;
103 ~HTMLDocument() override;
105 void Load();
107 BeforeLoadCache* GetBeforeLoadCache();
109 // ViewTreeDelegate:
110 void OnEmbed(mus::View* root) override;
111 void OnConnectionLost(mus::ViewTreeConnection* connection) override;
113 // HTMLFrameDelegate:
114 mojo::ApplicationImpl* GetApp() override;
115 HTMLFactory* GetHTMLFactory() override;
116 void OnFrameDidFinishLoad() override;
117 void OnFrameSwappedToRemote() override;
118 void OnSwap(HTMLFrame* frame, HTMLFrameDelegate* old_delegate) override;
119 void OnFrameDestroyed() override;
121 // mojo::InterfaceFactory<mojo::AxProvider>:
122 void Create(mojo::ApplicationConnection* connection,
123 mojo::InterfaceRequest<mojo::AxProvider> request) override;
125 // mojo::InterfaceFactory<web_view::mojom::FrameClient>:
126 void Create(
127 mojo::ApplicationConnection* connection,
128 mojo::InterfaceRequest<web_view::mojom::FrameClient> request) override;
130 // mojo::InterfaceFactory<TestHTMLViewer>:
131 void Create(mojo::ApplicationConnection* connection,
132 mojo::InterfaceRequest<TestHTMLViewer> request) override;
134 // mojo::InterfaceFactory<devtools_service::DevToolsAgent>:
135 void Create(
136 mojo::ApplicationConnection* connection,
137 mojo::InterfaceRequest<devtools_service::DevToolsAgent> request) override;
139 // mojo::InterfaceFactory<mus::ViewTreeClient>:
140 void Create(mojo::ApplicationConnection* connection,
141 mojo::InterfaceRequest<mojo::ViewTreeClient> request) override;
143 scoped_ptr<mojo::AppRefCount> app_refcount_;
144 mojo::ApplicationImpl* html_document_app_;
145 mojo::ApplicationConnection* connection_;
147 // HTMLDocument owns these pointers; binding requests after document load.
148 std::set<AxProviderImpl*> ax_providers_;
150 ScopedVector<TestHTMLViewerImpl> test_html_viewers_;
152 // Set to true when the local frame has finished loading.
153 bool did_finish_local_frame_load_ = false;
155 GlobalState* global_state_;
157 HTMLFrame* frame_;
159 scoped_ptr<DocumentResourceWaiter> resource_waiter_;
161 scoped_ptr<BeforeLoadCache> before_load_cache_;
163 DeleteCallback delete_callback_;
165 HTMLFactory* factory_;
167 TransferableState transferable_state_;
169 // Cache interface request of DevToolsAgent if |frame_| hasn't been
170 // initialized.
171 mojo::InterfaceRequest<devtools_service::DevToolsAgent>
172 devtools_agent_request_;
174 DISALLOW_COPY_AND_ASSIGN(HTMLDocument);
177 } // namespace html_viewer
179 #endif // COMPONENTS_HTML_VIEWER_HTML_DOCUMENT_H_