OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 using mojo::ServiceProviderPtr; | 33 using mojo::ServiceProviderPtr; |
34 using mojo::ShellPtr; | 34 using mojo::ShellPtr; |
35 using mojo::String; | 35 using mojo::String; |
36 using mojo::URLLoaderPtr; | 36 using mojo::URLLoaderPtr; |
37 using mojo::URLResponsePtr; | 37 using mojo::URLResponsePtr; |
38 | 38 |
39 namespace html_viewer { | 39 namespace html_viewer { |
40 | 40 |
41 class HTMLViewer; | 41 class HTMLViewer; |
42 | 42 |
43 class HTMLViewerApplication : public mojo::Application { | 43 // ApplicationDelegate created by the content handler for a specific url. |
| 44 class HTMLDocumentApplicationDelegate : public mojo::ApplicationDelegate { |
44 public: | 45 public: |
45 HTMLViewerApplication(InterfaceRequest<Application> request, | 46 HTMLDocumentApplicationDelegate( |
46 URLResponsePtr response, | 47 mojo::InterfaceRequest<mojo::Application> request, |
47 Setup* setup) | 48 mojo::URLResponsePtr response, |
48 : app_refcount_(setup->app()->app_lifetime_helper()->CreateAppRefCount()), | 49 Setup* setup, |
| 50 scoped_ptr<mojo::AppRefCount> parent_app_refcount) |
| 51 : app_(this, |
| 52 request.Pass(), |
| 53 base::Bind(&HTMLDocumentApplicationDelegate::OnTerminate, |
| 54 base::Unretained(this))), |
| 55 parent_app_refcount_(parent_app_refcount.Pass()), |
49 url_(response->url), | 56 url_(response->url), |
50 binding_(this, request.Pass()), | |
51 initial_response_(response.Pass()), | 57 initial_response_(response.Pass()), |
52 setup_(setup) { | 58 setup_(setup) {} |
| 59 |
| 60 private: |
| 61 ~HTMLDocumentApplicationDelegate() override {} |
| 62 |
| 63 // Callback from the quit closure. We key off this rather than |
| 64 // ApplicationDelegate::Quit() as we don't want to shut down the messageloop |
| 65 // when we quit (the messageloop is shared among multiple |
| 66 // HTMLDocumentApplicationDelegates). |
| 67 void OnTerminate() { |
| 68 delete this; |
53 } | 69 } |
54 | 70 |
55 ~HTMLViewerApplication() override { | 71 // ApplicationDelegate; |
56 } | 72 void Initialize(mojo::ApplicationImpl* app) override { |
57 | |
58 void Initialize(ShellPtr shell, const String& url) override { | |
59 shell_ = shell.Pass(); | |
60 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 73 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
61 request->url = mojo::String::From("mojo:network_service"); | 74 request->url = mojo::String::From("mojo:network_service"); |
62 setup_->app()->ConnectToService(request.Pass(), &network_service_); | 75 app_.ConnectToService(request.Pass(), (&network_service_)); |
63 } | 76 } |
64 | 77 bool ConfigureIncomingConnection( |
65 void AcceptConnection(const String& requestor_url, | 78 mojo::ApplicationConnection* connection) override { |
66 InterfaceRequest<ServiceProvider> services, | |
67 ServiceProviderPtr exposed_services, | |
68 const String& url) override { | |
69 if (initial_response_) { | 79 if (initial_response_) { |
70 OnResponseReceived(URLLoaderPtr(), services.Pass(), | 80 OnResponseReceived(URLLoaderPtr(), connection, initial_response_.Pass()); |
71 initial_response_.Pass()); | |
72 } else { | 81 } else { |
73 URLLoaderPtr loader; | 82 URLLoaderPtr loader; |
74 network_service_->CreateURLLoader(GetProxy(&loader)); | 83 network_service_->CreateURLLoader(GetProxy(&loader)); |
75 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 84 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
76 request->url = url_; | 85 request->url = url_; |
77 request->auto_follow_redirects = true; | 86 request->auto_follow_redirects = true; |
78 | 87 |
79 // |loader| will be pass to the OnResponseReceived method through a | 88 // |loader| will be passed to the OnResponseReceived method through a |
80 // callback. Because order of evaluation is undefined, a reference to the | 89 // callback. Because order of evaluation is undefined, a reference to the |
81 // raw pointer is needed. | 90 // raw pointer is needed. |
82 mojo::URLLoader* raw_loader = loader.get(); | 91 mojo::URLLoader* raw_loader = loader.get(); |
83 raw_loader->Start( | 92 raw_loader->Start( |
84 request.Pass(), | 93 request.Pass(), |
85 base::Bind(&HTMLViewerApplication::OnResponseReceived, | 94 base::Bind(&HTMLDocumentApplicationDelegate::OnResponseReceived, |
86 base::Unretained(this), base::Passed(&loader), | 95 base::Unretained(this), base::Passed(&loader), |
87 base::Passed(&services))); | 96 connection)); |
88 } | 97 } |
| 98 return true; |
89 } | 99 } |
90 | 100 |
91 void OnQuitRequested(const mojo::Callback<void(bool)>& callback) override { | |
92 callback.Run(true); | |
93 delete this; | |
94 } | |
95 | |
96 private: | |
97 void OnResponseReceived(URLLoaderPtr loader, | 101 void OnResponseReceived(URLLoaderPtr loader, |
98 InterfaceRequest<ServiceProvider> services, | 102 mojo::ApplicationConnection* connection, |
99 URLResponsePtr response) { | 103 URLResponsePtr response) { |
100 // HTMLDocument is destroyed when the hosting view is destroyed. | 104 // HTMLDocument is destroyed when the hosting view is destroyed. |
101 // TODO(sky): when headless, this leaks. | 105 // TODO(sky): when headless, this leaks. |
102 new HTMLDocument(services.Pass(), response.Pass(), shell_.Pass(), setup_); | 106 // TODO(sky): this needs to delete if serviceprovider goes away too. |
| 107 new HTMLDocument(&app_, connection, response.Pass(), setup_); |
103 } | 108 } |
104 | 109 |
105 scoped_ptr<mojo::AppRefCount> app_refcount_; | 110 mojo::ApplicationImpl app_; |
106 String url_; | 111 // AppRefCount of the parent (HTMLViewer). |
107 mojo::StrongBinding<mojo::Application> binding_; | 112 scoped_ptr<mojo::AppRefCount> parent_app_refcount_; |
108 ShellPtr shell_; | 113 const String url_; |
109 mojo::NetworkServicePtr network_service_; | 114 mojo::NetworkServicePtr network_service_; |
110 URLResponsePtr initial_response_; | 115 URLResponsePtr initial_response_; |
111 Setup* setup_; | 116 Setup* setup_; |
112 | 117 |
113 DISALLOW_COPY_AND_ASSIGN(HTMLViewerApplication); | 118 DISALLOW_COPY_AND_ASSIGN(HTMLDocumentApplicationDelegate); |
114 }; | 119 }; |
115 | 120 |
116 class ContentHandlerImpl : public mojo::ContentHandler { | 121 class ContentHandlerImpl : public mojo::ContentHandler { |
117 public: | 122 public: |
118 ContentHandlerImpl(Setup* setup, | 123 ContentHandlerImpl(Setup* setup, |
| 124 mojo::ApplicationImpl* app, |
119 mojo::InterfaceRequest<ContentHandler> request) | 125 mojo::InterfaceRequest<ContentHandler> request) |
120 : setup_(setup), | 126 : setup_(setup), app_(app), binding_(this, request.Pass()) {} |
121 binding_(this, request.Pass()) {} | |
122 ~ContentHandlerImpl() override {} | 127 ~ContentHandlerImpl() override {} |
123 | 128 |
124 private: | 129 private: |
125 // Overridden from ContentHandler: | 130 // Overridden from ContentHandler: |
126 void StartApplication(InterfaceRequest<mojo::Application> request, | 131 void StartApplication(InterfaceRequest<mojo::Application> request, |
127 URLResponsePtr response) override { | 132 URLResponsePtr response) override { |
128 // HTMLViewerApplication is owned by the binding. | 133 // HTMLDocumentApplicationDelegate deletes itself. |
129 new HTMLViewerApplication(request.Pass(), response.Pass(), setup_); | 134 new HTMLDocumentApplicationDelegate( |
| 135 request.Pass(), response.Pass(), setup_, |
| 136 app_->app_lifetime_helper()->CreateAppRefCount()); |
130 } | 137 } |
131 | 138 |
132 Setup* setup_; | 139 Setup* setup_; |
| 140 mojo::ApplicationImpl* app_; |
133 mojo::StrongBinding<mojo::ContentHandler> binding_; | 141 mojo::StrongBinding<mojo::ContentHandler> binding_; |
134 | 142 |
135 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); | 143 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); |
136 }; | 144 }; |
137 | 145 |
138 class HTMLViewer : public mojo::ApplicationDelegate, | 146 class HTMLViewer : public mojo::ApplicationDelegate, |
139 public mojo::InterfaceFactory<ContentHandler> { | 147 public mojo::InterfaceFactory<ContentHandler> { |
140 public: | 148 public: |
141 HTMLViewer() {} | 149 HTMLViewer() : app_(nullptr) {} |
142 ~HTMLViewer() override {} | 150 ~HTMLViewer() override {} |
143 | 151 |
144 private: | 152 private: |
145 // Overridden from ApplicationDelegate: | 153 // Overridden from ApplicationDelegate: |
146 void Initialize(mojo::ApplicationImpl* app) override { | 154 void Initialize(mojo::ApplicationImpl* app) override { |
| 155 app_ = app; |
147 setup_.reset(new Setup(app)); | 156 setup_.reset(new Setup(app)); |
148 } | 157 } |
149 | 158 |
150 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | 159 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
151 // If we're not being connected to from the view manager assume we're being | 160 // If we're not being connected to from the view manager assume we're being |
152 // run in tests, or a headless environment, in which case we'll never get a | 161 // run in tests, or a headless environment, in which case we'll never get a |
153 // ui and there is no point in waiting for it. | 162 // ui and there is no point in waiting for it. |
154 if (connection->GetRemoteApplicationURL() != "mojo://view_manager/" && | 163 if (connection->GetRemoteApplicationURL() != "mojo://view_manager/" && |
155 !setup_->did_init()) { | 164 !setup_->did_init()) { |
156 setup_->InitHeadless(); | 165 setup_->InitHeadless(); |
157 } | 166 } |
158 connection->AddService(this); | 167 connection->AddService(this); |
159 return true; | 168 return true; |
160 } | 169 } |
161 | 170 |
162 // Overridden from InterfaceFactory<ContentHandler> | 171 // Overridden from InterfaceFactory<ContentHandler> |
163 void Create(ApplicationConnection* connection, | 172 void Create(ApplicationConnection* connection, |
164 mojo::InterfaceRequest<ContentHandler> request) override { | 173 mojo::InterfaceRequest<ContentHandler> request) override { |
165 new ContentHandlerImpl(setup_.get(), request.Pass()); | 174 new ContentHandlerImpl(setup_.get(), app_, request.Pass()); |
166 } | 175 } |
167 | 176 |
168 scoped_ptr<Setup> setup_; | 177 scoped_ptr<Setup> setup_; |
| 178 mojo::ApplicationImpl* app_; |
169 | 179 |
170 DISALLOW_COPY_AND_ASSIGN(HTMLViewer); | 180 DISALLOW_COPY_AND_ASSIGN(HTMLViewer); |
171 }; | 181 }; |
172 | 182 |
173 } // namespace html_viewer | 183 } // namespace html_viewer |
174 | 184 |
175 MojoResult MojoMain(MojoHandle shell_handle) { | 185 MojoResult MojoMain(MojoHandle shell_handle) { |
176 mojo::ApplicationRunner runner(new html_viewer::HTMLViewer); | 186 mojo::ApplicationRunner runner(new html_viewer::HTMLViewer); |
177 return runner.Run(shell_handle); | 187 return runner.Run(shell_handle); |
178 } | 188 } |
OLD | NEW |