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/interface_binder.h" |
13 #include "mojo/shell/public/cpp/shell_client.h" | 14 #include "mojo/shell/public/cpp/shell_client.h" |
14 | 15 |
15 namespace html_viewer { | 16 namespace html_viewer { |
16 | 17 |
17 // ServiceConnectorQueue records all incoming service requests and processes | 18 // InterfaceBinderQueue records all incoming service requests and processes |
18 // them once PushRequestsTo() is called. This is useful if you need to delay | 19 // them once PushRequestsTo() is called. This is useful if you need to delay |
19 // processing incoming service requests. | 20 // processing incoming service requests. |
20 class HTMLDocumentApplicationDelegate::ServiceConnectorQueue | 21 class HTMLDocumentApplicationDelegate::InterfaceBinderQueue |
21 : public mojo::ServiceConnector { | 22 : public mojo::InterfaceBinder { |
22 public: | 23 public: |
23 ServiceConnectorQueue() {} | 24 InterfaceBinderQueue() {} |
24 ~ServiceConnectorQueue() override {} | 25 ~InterfaceBinderQueue() override {} |
25 | 26 |
26 void PushRequestsTo(mojo::Connection* connection) { | 27 void PushRequestsTo(mojo::Connection* connection) { |
27 ScopedVector<Request> requests; | 28 ScopedVector<Request> requests; |
28 requests_.swap(requests); | 29 requests_.swap(requests); |
29 for (Request* request : requests) { | 30 for (Request* request : requests) { |
30 connection->GetLocalServiceProvider()->ConnectToService( | 31 connection->GetLocalServiceProvider()->ConnectToService( |
31 request->interface_name, std::move(request->handle)); | 32 request->interface_name, std::move(request->handle)); |
32 } | 33 } |
33 } | 34 } |
34 | 35 |
35 private: | 36 private: |
36 struct Request { | 37 struct Request { |
37 std::string interface_name; | 38 std::string interface_name; |
38 mojo::ScopedMessagePipeHandle handle; | 39 mojo::ScopedMessagePipeHandle handle; |
39 }; | 40 }; |
40 | 41 |
41 // mojo::ServiceConnector: | 42 // mojo::InterfaceBinder: |
42 void ConnectToService(mojo::Connection* connection, | 43 void BindInterface(mojo::Connection* connection, |
43 const std::string& interface_name, | 44 const std::string& interface_name, |
44 mojo::ScopedMessagePipeHandle handle) override { | 45 mojo::ScopedMessagePipeHandle handle) override { |
45 scoped_ptr<Request> request(new Request); | 46 scoped_ptr<Request> request(new Request); |
46 request->interface_name = interface_name; | 47 request->interface_name = interface_name; |
47 request->handle = std::move(handle); | 48 request->handle = std::move(handle); |
48 requests_.push_back(std::move(request)); | 49 requests_.push_back(std::move(request)); |
49 } | 50 } |
50 | 51 |
51 ScopedVector<Request> requests_; | 52 ScopedVector<Request> requests_; |
52 | 53 |
53 DISALLOW_COPY_AND_ASSIGN(ServiceConnectorQueue); | 54 DISALLOW_COPY_AND_ASSIGN(InterfaceBinderQueue); |
54 }; | 55 }; |
55 | 56 |
56 HTMLDocumentApplicationDelegate::HTMLDocumentApplicationDelegate( | 57 HTMLDocumentApplicationDelegate::HTMLDocumentApplicationDelegate( |
57 mojo::ShellClientRequest request, | 58 mojo::ShellClientRequest request, |
58 mojo::URLResponsePtr response, | 59 mojo::URLResponsePtr response, |
59 GlobalState* global_state, | 60 GlobalState* global_state, |
60 scoped_ptr<mojo::AppRefCount> parent_app_refcount, | 61 scoped_ptr<mojo::AppRefCount> parent_app_refcount, |
61 const mojo::Callback<void()>& destruct_callback) | 62 const mojo::Callback<void()>& destruct_callback) |
62 : app_(this, | 63 : app_(this, |
63 std::move(request), | 64 std::move(request), |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // TODO(yzshen): find out a better approach. | 113 // TODO(yzshen): find out a better approach. |
113 mojo::URLResponsePtr response(mojo::URLResponse::New()); | 114 mojo::URLResponsePtr response(mojo::URLResponse::New()); |
114 response->url = url_; | 115 response->url = url_; |
115 response->status_code = 200; | 116 response->status_code = 200; |
116 response->mime_type = "text/html"; | 117 response->mime_type = "text/html"; |
117 OnResponseReceived(nullptr, mojo::URLLoaderPtr(), connection, nullptr, | 118 OnResponseReceived(nullptr, mojo::URLLoaderPtr(), connection, nullptr, |
118 std::move(response)); | 119 std::move(response)); |
119 } else { | 120 } else { |
120 // HTMLDocument provides services, but is created asynchronously. Queue up | 121 // HTMLDocument provides services, but is created asynchronously. Queue up |
121 // requests until the HTMLDocument is created. | 122 // requests until the HTMLDocument is created. |
122 scoped_ptr<ServiceConnectorQueue> service_connector_queue( | 123 scoped_ptr<InterfaceBinderQueue> interface_binder_queue( |
123 new ServiceConnectorQueue); | 124 new InterfaceBinderQueue); |
124 connection->SetServiceConnector(service_connector_queue.get()); | 125 connection->SetDefaultInterfaceBinder(interface_binder_queue.get()); |
125 | 126 |
126 mojo::URLLoaderPtr loader; | 127 mojo::URLLoaderPtr loader; |
127 url_loader_factory_->CreateURLLoader(GetProxy(&loader)); | 128 url_loader_factory_->CreateURLLoader(GetProxy(&loader)); |
128 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 129 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
129 request->url = url_; | 130 request->url = url_; |
130 request->auto_follow_redirects = true; | 131 request->auto_follow_redirects = true; |
131 | 132 |
132 // |loader| will be passed to the OnResponseReceived method through a | 133 // |loader| will be passed to the OnResponseReceived method through a |
133 // callback. Because order of evaluation is undefined, a reference to the | 134 // callback. Because order of evaluation is undefined, a reference to the |
134 // raw pointer is needed. | 135 // raw pointer is needed. |
135 mojo::URLLoader* raw_loader = loader.get(); | 136 mojo::URLLoader* raw_loader = loader.get(); |
136 // The app needs to stay alive while waiting for the response to be | 137 // The app needs to stay alive while waiting for the response to be |
137 // available. | 138 // available. |
138 scoped_ptr<mojo::AppRefCount> app_retainer(app_.CreateAppRefCount()); | 139 scoped_ptr<mojo::AppRefCount> app_retainer(app_.CreateAppRefCount()); |
139 raw_loader->Start( | 140 raw_loader->Start( |
140 std::move(request), | 141 std::move(request), |
141 base::Bind(&HTMLDocumentApplicationDelegate::OnResponseReceived, | 142 base::Bind(&HTMLDocumentApplicationDelegate::OnResponseReceived, |
142 weak_factory_.GetWeakPtr(), base::Passed(&app_retainer), | 143 weak_factory_.GetWeakPtr(), base::Passed(&app_retainer), |
143 base::Passed(&loader), connection, | 144 base::Passed(&loader), connection, |
144 base::Passed(&service_connector_queue))); | 145 base::Passed(&interface_binder_queue))); |
145 } | 146 } |
146 return true; | 147 return true; |
147 } | 148 } |
148 | 149 |
149 void HTMLDocumentApplicationDelegate::OnHTMLDocumentDeleted2( | 150 void HTMLDocumentApplicationDelegate::OnHTMLDocumentDeleted2( |
150 HTMLDocument* document) { | 151 HTMLDocument* document) { |
151 DCHECK(documents2_.count(document) > 0); | 152 DCHECK(documents2_.count(document) > 0); |
152 documents2_.erase(document); | 153 documents2_.erase(document); |
153 } | 154 } |
154 | 155 |
155 void HTMLDocumentApplicationDelegate::OnResponseReceived( | 156 void HTMLDocumentApplicationDelegate::OnResponseReceived( |
156 scoped_ptr<mojo::AppRefCount> app_refcount, | 157 scoped_ptr<mojo::AppRefCount> app_refcount, |
157 mojo::URLLoaderPtr loader, | 158 mojo::URLLoaderPtr loader, |
158 mojo::Connection* connection, | 159 mojo::Connection* connection, |
159 scoped_ptr<ServiceConnectorQueue> connector_queue, | 160 scoped_ptr<InterfaceBinderQueue> binder_queue, |
160 mojo::URLResponsePtr response) { | 161 mojo::URLResponsePtr response) { |
161 // HTMLDocument is destroyed when the hosting view is destroyed, or | 162 // HTMLDocument is destroyed when the hosting view is destroyed, or |
162 // explicitly from our destructor. | 163 // explicitly from our destructor. |
163 HTMLDocument* document = new HTMLDocument( | 164 HTMLDocument* document = new HTMLDocument( |
164 &app_, connection, std::move(response), global_state_, | 165 &app_, connection, std::move(response), global_state_, |
165 base::Bind(&HTMLDocumentApplicationDelegate::OnHTMLDocumentDeleted2, | 166 base::Bind(&HTMLDocumentApplicationDelegate::OnHTMLDocumentDeleted2, |
166 base::Unretained(this)), | 167 base::Unretained(this)), |
167 html_factory_); | 168 html_factory_); |
168 documents2_.insert(document); | 169 documents2_.insert(document); |
169 | 170 |
170 if (connector_queue) { | 171 if (binder_queue) { |
171 connector_queue->PushRequestsTo(connection); | 172 binder_queue->PushRequestsTo(connection); |
172 connection->SetServiceConnector(nullptr); | 173 connection->SetDefaultInterfaceBinder(nullptr); |
173 } | 174 } |
174 } | 175 } |
175 | 176 |
176 HTMLFrame* HTMLDocumentApplicationDelegate::CreateHTMLFrame( | 177 HTMLFrame* HTMLDocumentApplicationDelegate::CreateHTMLFrame( |
177 HTMLFrame::CreateParams* params) { | 178 HTMLFrame::CreateParams* params) { |
178 return new HTMLFrame(params); | 179 return new HTMLFrame(params); |
179 } | 180 } |
180 | 181 |
181 HTMLWidgetRootLocal* HTMLDocumentApplicationDelegate::CreateHTMLWidgetRootLocal( | 182 HTMLWidgetRootLocal* HTMLDocumentApplicationDelegate::CreateHTMLWidgetRootLocal( |
182 HTMLWidgetRootLocal::CreateParams* params) { | 183 HTMLWidgetRootLocal::CreateParams* params) { |
183 return new HTMLWidgetRootLocal(params); | 184 return new HTMLWidgetRootLocal(params); |
184 } | 185 } |
185 | 186 |
186 } // namespace html_viewer | 187 } // namespace html_viewer |
OLD | NEW |