OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/html_viewer/html_document_application_delegate.h" | 5 #include "components/html_viewer/html_document_application_delegate.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "components/html_viewer/global_state.h" | 11 #include "components/html_viewer/global_state.h" |
12 #include "components/html_viewer/html_document.h" | 12 #include "components/html_viewer/html_document.h" |
13 #include "mojo/shell/public/cpp/application_connection.h" | 13 #include "mojo/shell/public/cpp/shell_client.h" |
14 #include "mojo/shell/public/cpp/application_delegate.h" | |
15 #include "mojo/shell/public/cpp/connect.h" | |
16 | 14 |
17 namespace html_viewer { | 15 namespace html_viewer { |
18 | 16 |
19 // ServiceConnectorQueue records all incoming service requests and processes | 17 // ServiceConnectorQueue records all incoming service requests and processes |
20 // them once PushRequestsTo() is called. This is useful if you need to delay | 18 // them once PushRequestsTo() is called. This is useful if you need to delay |
21 // processing incoming service requests. | 19 // processing incoming service requests. |
22 class HTMLDocumentApplicationDelegate::ServiceConnectorQueue | 20 class HTMLDocumentApplicationDelegate::ServiceConnectorQueue |
23 : public mojo::ServiceConnector { | 21 : public mojo::ServiceConnector { |
24 public: | 22 public: |
25 ServiceConnectorQueue() {} | 23 ServiceConnectorQueue() {} |
26 ~ServiceConnectorQueue() override {} | 24 ~ServiceConnectorQueue() override {} |
27 | 25 |
28 void PushRequestsTo(mojo::ApplicationConnection* connection) { | 26 void PushRequestsTo(mojo::Connection* connection) { |
29 ScopedVector<Request> requests; | 27 ScopedVector<Request> requests; |
30 requests_.swap(requests); | 28 requests_.swap(requests); |
31 for (Request* request : requests) { | 29 for (Request* request : requests) { |
32 connection->GetLocalServiceProvider()->ConnectToService( | 30 connection->GetLocalServiceProvider()->ConnectToService( |
33 request->interface_name, std::move(request->handle)); | 31 request->interface_name, std::move(request->handle)); |
34 } | 32 } |
35 } | 33 } |
36 | 34 |
37 private: | 35 private: |
38 struct Request { | 36 struct Request { |
39 std::string interface_name; | 37 std::string interface_name; |
40 mojo::ScopedMessagePipeHandle handle; | 38 mojo::ScopedMessagePipeHandle handle; |
41 }; | 39 }; |
42 | 40 |
43 // mojo::ServiceConnector: | 41 // mojo::ServiceConnector: |
44 void ConnectToService(mojo::ApplicationConnection* application_connection, | 42 void ConnectToService(mojo::Connection* connection, |
45 const std::string& interface_name, | 43 const std::string& interface_name, |
46 mojo::ScopedMessagePipeHandle handle) override { | 44 mojo::ScopedMessagePipeHandle handle) override { |
47 scoped_ptr<Request> request(new Request); | 45 scoped_ptr<Request> request(new Request); |
48 request->interface_name = interface_name; | 46 request->interface_name = interface_name; |
49 request->handle = std::move(handle); | 47 request->handle = std::move(handle); |
50 requests_.push_back(std::move(request)); | 48 requests_.push_back(std::move(request)); |
51 } | 49 } |
52 | 50 |
53 ScopedVector<Request> requests_; | 51 ScopedVector<Request> requests_; |
54 | 52 |
(...skipping 23 matching lines...) Expand all Loading... |
78 // OnHTMLDocumentDeleted() and remove from |documents_|. Copy the set so we | 76 // OnHTMLDocumentDeleted() and remove from |documents_|. Copy the set so we |
79 // don't have to worry about the set being modified out from under us. | 77 // don't have to worry about the set being modified out from under us. |
80 std::set<HTMLDocument*> documents2(documents2_); | 78 std::set<HTMLDocument*> documents2(documents2_); |
81 for (HTMLDocument* doc : documents2) | 79 for (HTMLDocument* doc : documents2) |
82 doc->Destroy(); | 80 doc->Destroy(); |
83 DCHECK(documents2_.empty()); | 81 DCHECK(documents2_.empty()); |
84 destruct_callback_.Run(); | 82 destruct_callback_.Run(); |
85 } | 83 } |
86 | 84 |
87 // Callback from the quit closure. We key off this rather than | 85 // Callback from the quit closure. We key off this rather than |
88 // ApplicationDelegate::Quit() as we don't want to shut down the messageloop | 86 // mojo::ShellClient::Quit() as we don't want to shut down the messageloop |
89 // when we quit (the messageloop is shared among multiple | 87 // when we quit (the messageloop is shared among multiple |
90 // HTMLDocumentApplicationDelegates). | 88 // HTMLDocumentApplicationDelegates). |
91 void HTMLDocumentApplicationDelegate::OnTerminate() { | 89 void HTMLDocumentApplicationDelegate::OnTerminate() { |
92 delete this; | 90 delete this; |
93 } | 91 } |
94 | 92 |
95 // ApplicationDelegate; | 93 // mojo::ShellClient; |
96 void HTMLDocumentApplicationDelegate::Initialize(mojo::Shell* shell, | 94 void HTMLDocumentApplicationDelegate::Initialize(mojo::Shell* shell, |
97 const std::string& url, | 95 const std::string& url, |
98 uint32_t id) { | 96 uint32_t id) { |
99 app_.ConnectToService("mojo:network_service", &url_loader_factory_); | 97 app_.ConnectToService("mojo:network_service", &url_loader_factory_); |
100 } | 98 } |
101 | 99 |
102 bool HTMLDocumentApplicationDelegate::AcceptConnection( | 100 bool HTMLDocumentApplicationDelegate::AcceptConnection( |
103 mojo::ApplicationConnection* connection) { | 101 mojo::Connection* connection) { |
104 if (initial_response_) { | 102 if (initial_response_) { |
105 OnResponseReceived(nullptr, mojo::URLLoaderPtr(), connection, nullptr, | 103 OnResponseReceived(nullptr, mojo::URLLoaderPtr(), connection, nullptr, |
106 std::move(initial_response_)); | 104 std::move(initial_response_)); |
107 } else if (url_ == "about:blank") { | 105 } else if (url_ == "about:blank") { |
108 // This is a little unfortunate. At the browser side, when starting a new | 106 // This is a little unfortunate. At the browser side, when starting a new |
109 // app for "about:blank", the application manager uses | 107 // app for "about:blank", the application manager uses |
110 // mojo::shell::AboutFetcher to construct a response for "about:blank". | 108 // mojo::shell::AboutFetcher to construct a response for "about:blank". |
111 // However, when an app for "about:blank" already exists, it is reused and | 109 // However, when an app for "about:blank" already exists, it is reused and |
112 // we end up here. We cannot fetch the URL using mojo::URLLoader because it | 110 // we end up here. We cannot fetch the URL using mojo::URLLoader because it |
113 // is not an actual Web resource. | 111 // is not an actual Web resource. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 148 |
151 void HTMLDocumentApplicationDelegate::OnHTMLDocumentDeleted2( | 149 void HTMLDocumentApplicationDelegate::OnHTMLDocumentDeleted2( |
152 HTMLDocument* document) { | 150 HTMLDocument* document) { |
153 DCHECK(documents2_.count(document) > 0); | 151 DCHECK(documents2_.count(document) > 0); |
154 documents2_.erase(document); | 152 documents2_.erase(document); |
155 } | 153 } |
156 | 154 |
157 void HTMLDocumentApplicationDelegate::OnResponseReceived( | 155 void HTMLDocumentApplicationDelegate::OnResponseReceived( |
158 scoped_ptr<mojo::AppRefCount> app_refcount, | 156 scoped_ptr<mojo::AppRefCount> app_refcount, |
159 mojo::URLLoaderPtr loader, | 157 mojo::URLLoaderPtr loader, |
160 mojo::ApplicationConnection* connection, | 158 mojo::Connection* connection, |
161 scoped_ptr<ServiceConnectorQueue> connector_queue, | 159 scoped_ptr<ServiceConnectorQueue> connector_queue, |
162 mojo::URLResponsePtr response) { | 160 mojo::URLResponsePtr response) { |
163 // HTMLDocument is destroyed when the hosting view is destroyed, or | 161 // HTMLDocument is destroyed when the hosting view is destroyed, or |
164 // explicitly from our destructor. | 162 // explicitly from our destructor. |
165 HTMLDocument* document = new HTMLDocument( | 163 HTMLDocument* document = new HTMLDocument( |
166 &app_, connection, std::move(response), global_state_, | 164 &app_, connection, std::move(response), global_state_, |
167 base::Bind(&HTMLDocumentApplicationDelegate::OnHTMLDocumentDeleted2, | 165 base::Bind(&HTMLDocumentApplicationDelegate::OnHTMLDocumentDeleted2, |
168 base::Unretained(this)), | 166 base::Unretained(this)), |
169 html_factory_); | 167 html_factory_); |
170 documents2_.insert(document); | 168 documents2_.insert(document); |
171 | 169 |
172 if (connector_queue) { | 170 if (connector_queue) { |
173 connector_queue->PushRequestsTo(connection); | 171 connector_queue->PushRequestsTo(connection); |
174 connection->SetServiceConnector(nullptr); | 172 connection->SetServiceConnector(nullptr); |
175 } | 173 } |
176 } | 174 } |
177 | 175 |
178 HTMLFrame* HTMLDocumentApplicationDelegate::CreateHTMLFrame( | 176 HTMLFrame* HTMLDocumentApplicationDelegate::CreateHTMLFrame( |
179 HTMLFrame::CreateParams* params) { | 177 HTMLFrame::CreateParams* params) { |
180 return new HTMLFrame(params); | 178 return new HTMLFrame(params); |
181 } | 179 } |
182 | 180 |
183 HTMLWidgetRootLocal* HTMLDocumentApplicationDelegate::CreateHTMLWidgetRootLocal( | 181 HTMLWidgetRootLocal* HTMLDocumentApplicationDelegate::CreateHTMLWidgetRootLocal( |
184 HTMLWidgetRootLocal::CreateParams* params) { | 182 HTMLWidgetRootLocal::CreateParams* params) { |
185 return new HTMLWidgetRootLocal(params); | 183 return new HTMLWidgetRootLocal(params); |
186 } | 184 } |
187 | 185 |
188 } // namespace html_viewer | 186 } // namespace html_viewer |
OLD | NEW |