Chromium Code Reviews| 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::DoQuit, | |
| 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) {} |
| 53 } | |
| 54 | 59 |
| 55 ~HTMLViewerApplication() override { | 60 private: |
| 56 } | 61 ~HTMLDocumentApplicationDelegate() override {} |
| 57 | 62 |
| 58 void Initialize(ShellPtr shell, const String& url) override { | 63 // Callback from the quit closure. We key off this rather than |
| 59 shell_ = shell.Pass(); | 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 DoQuit() { delete this; } | |
|
jam
2015/06/04 22:10:00
why isn't this app_->shell()->QuitApplication() as
sky
2015/06/04 23:16:46
As discussed it should be this. I've renamed the f
| |
| 68 | |
| 69 // ApplicationDelegate; | |
| 70 void Initialize(mojo::ApplicationImpl* app) override { | |
| 60 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 71 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 61 request->url = mojo::String::From("mojo:network_service"); | 72 request->url = mojo::String::From("mojo:network_service"); |
| 62 setup_->app()->ConnectToService(request.Pass(), &network_service_); | 73 app_.ConnectToService(request.Pass(), (&network_service_)); |
| 63 } | 74 } |
| 64 | 75 bool ConfigureIncomingConnection( |
| 65 void AcceptConnection(const String& requestor_url, | 76 mojo::ApplicationConnection* connection) override { |
| 66 InterfaceRequest<ServiceProvider> services, | |
| 67 ServiceProviderPtr exposed_services, | |
| 68 const String& url) override { | |
| 69 if (initial_response_) { | 77 if (initial_response_) { |
| 70 OnResponseReceived(URLLoaderPtr(), services.Pass(), | 78 OnResponseReceived(URLLoaderPtr(), connection, initial_response_.Pass()); |
| 71 initial_response_.Pass()); | |
| 72 } else { | 79 } else { |
| 73 URLLoaderPtr loader; | 80 URLLoaderPtr loader; |
| 74 network_service_->CreateURLLoader(GetProxy(&loader)); | 81 network_service_->CreateURLLoader(GetProxy(&loader)); |
| 75 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 82 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 76 request->url = url_; | 83 request->url = url_; |
| 77 request->auto_follow_redirects = true; | 84 request->auto_follow_redirects = true; |
| 78 | 85 |
| 79 // |loader| will be pass to the OnResponseReceived method through a | 86 // |loader| will be passed to the OnResponseReceived method through a |
| 80 // callback. Because order of evaluation is undefined, a reference to the | 87 // callback. Because order of evaluation is undefined, a reference to the |
| 81 // raw pointer is needed. | 88 // raw pointer is needed. |
| 82 mojo::URLLoader* raw_loader = loader.get(); | 89 mojo::URLLoader* raw_loader = loader.get(); |
| 83 raw_loader->Start( | 90 raw_loader->Start( |
| 84 request.Pass(), | 91 request.Pass(), |
| 85 base::Bind(&HTMLViewerApplication::OnResponseReceived, | 92 base::Bind(&HTMLDocumentApplicationDelegate::OnResponseReceived, |
| 86 base::Unretained(this), base::Passed(&loader), | 93 base::Unretained(this), base::Passed(&loader), |
| 87 base::Passed(&services))); | 94 connection)); |
| 88 } | 95 } |
| 96 return true; | |
| 89 } | 97 } |
| 90 | 98 |
| 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, | 99 void OnResponseReceived(URLLoaderPtr loader, |
| 98 InterfaceRequest<ServiceProvider> services, | 100 mojo::ApplicationConnection* connection, |
| 99 URLResponsePtr response) { | 101 URLResponsePtr response) { |
| 100 // HTMLDocument is destroyed when the hosting view is destroyed. | 102 // HTMLDocument is destroyed when the hosting view is destroyed. |
| 101 // TODO(sky): when headless, this leaks. | 103 // TODO(sky): when headless, this leaks. |
| 102 new HTMLDocument(services.Pass(), response.Pass(), shell_.Pass(), setup_); | 104 // TODO(sky): this needs to delete if serviceprovider goes away too. |
| 105 new HTMLDocument(&app_, connection, response.Pass(), setup_); | |
| 103 } | 106 } |
| 104 | 107 |
| 105 scoped_ptr<mojo::AppRefCount> app_refcount_; | 108 mojo::ApplicationImpl app_; |
| 106 String url_; | 109 // AppRefCount of the parent (HTMLViewer). |
| 107 mojo::StrongBinding<mojo::Application> binding_; | 110 scoped_ptr<mojo::AppRefCount> parent_app_refcount_; |
| 108 ShellPtr shell_; | 111 const String url_; |
| 109 mojo::NetworkServicePtr network_service_; | 112 mojo::NetworkServicePtr network_service_; |
| 110 URLResponsePtr initial_response_; | 113 URLResponsePtr initial_response_; |
| 111 Setup* setup_; | 114 Setup* setup_; |
| 112 | 115 |
| 113 DISALLOW_COPY_AND_ASSIGN(HTMLViewerApplication); | 116 DISALLOW_COPY_AND_ASSIGN(HTMLDocumentApplicationDelegate); |
| 114 }; | 117 }; |
| 115 | 118 |
| 116 class ContentHandlerImpl : public mojo::ContentHandler { | 119 class ContentHandlerImpl : public mojo::ContentHandler { |
| 117 public: | 120 public: |
| 118 ContentHandlerImpl(Setup* setup, | 121 ContentHandlerImpl(Setup* setup, |
| 122 mojo::ApplicationImpl* app, | |
| 119 mojo::InterfaceRequest<ContentHandler> request) | 123 mojo::InterfaceRequest<ContentHandler> request) |
| 120 : setup_(setup), | 124 : setup_(setup), app_(app), binding_(this, request.Pass()) {} |
| 121 binding_(this, request.Pass()) {} | |
| 122 ~ContentHandlerImpl() override {} | 125 ~ContentHandlerImpl() override {} |
| 123 | 126 |
| 124 private: | 127 private: |
| 125 // Overridden from ContentHandler: | 128 // Overridden from ContentHandler: |
| 126 void StartApplication(InterfaceRequest<mojo::Application> request, | 129 void StartApplication(InterfaceRequest<mojo::Application> request, |
| 127 URLResponsePtr response) override { | 130 URLResponsePtr response) override { |
| 128 // HTMLViewerApplication is owned by the binding. | 131 // HTMLDocumentApplicationDelegate deletes itself. |
| 129 new HTMLViewerApplication(request.Pass(), response.Pass(), setup_); | 132 new HTMLDocumentApplicationDelegate( |
| 133 request.Pass(), response.Pass(), setup_, | |
| 134 app_->app_lifetime_helper()->CreateAppRefCount()); | |
| 130 } | 135 } |
| 131 | 136 |
| 132 Setup* setup_; | 137 Setup* setup_; |
| 138 mojo::ApplicationImpl* app_; | |
| 133 mojo::StrongBinding<mojo::ContentHandler> binding_; | 139 mojo::StrongBinding<mojo::ContentHandler> binding_; |
| 134 | 140 |
| 135 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); | 141 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); |
| 136 }; | 142 }; |
| 137 | 143 |
| 138 class HTMLViewer : public mojo::ApplicationDelegate, | 144 class HTMLViewer : public mojo::ApplicationDelegate, |
| 139 public mojo::InterfaceFactory<ContentHandler> { | 145 public mojo::InterfaceFactory<ContentHandler> { |
| 140 public: | 146 public: |
| 141 HTMLViewer() {} | 147 HTMLViewer() : app_(nullptr) {} |
| 142 ~HTMLViewer() override {} | 148 ~HTMLViewer() override {} |
| 143 | 149 |
| 144 private: | 150 private: |
| 145 // Overridden from ApplicationDelegate: | 151 // Overridden from ApplicationDelegate: |
| 146 void Initialize(mojo::ApplicationImpl* app) override { | 152 void Initialize(mojo::ApplicationImpl* app) override { |
| 153 app_ = app; | |
| 147 setup_.reset(new Setup(app)); | 154 setup_.reset(new Setup(app)); |
| 148 } | 155 } |
| 149 | 156 |
| 150 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | 157 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
| 151 // If we're not being connected to from the view manager assume we're being | 158 // 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 | 159 // 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. | 160 // ui and there is no point in waiting for it. |
| 154 if (connection->GetRemoteApplicationURL() != "mojo://view_manager/" && | 161 if (connection->GetRemoteApplicationURL() != "mojo://view_manager/" && |
| 155 !setup_->did_init()) { | 162 !setup_->did_init()) { |
| 156 setup_->InitHeadless(); | 163 setup_->InitHeadless(); |
| 157 } | 164 } |
| 158 connection->AddService(this); | 165 connection->AddService(this); |
| 159 return true; | 166 return true; |
| 160 } | 167 } |
| 161 | 168 |
| 162 // Overridden from InterfaceFactory<ContentHandler> | 169 // Overridden from InterfaceFactory<ContentHandler> |
| 163 void Create(ApplicationConnection* connection, | 170 void Create(ApplicationConnection* connection, |
| 164 mojo::InterfaceRequest<ContentHandler> request) override { | 171 mojo::InterfaceRequest<ContentHandler> request) override { |
| 165 new ContentHandlerImpl(setup_.get(), request.Pass()); | 172 new ContentHandlerImpl(setup_.get(), app_, request.Pass()); |
| 166 } | 173 } |
| 167 | 174 |
| 168 scoped_ptr<Setup> setup_; | 175 scoped_ptr<Setup> setup_; |
| 176 mojo::ApplicationImpl* app_; | |
| 169 | 177 |
| 170 DISALLOW_COPY_AND_ASSIGN(HTMLViewer); | 178 DISALLOW_COPY_AND_ASSIGN(HTMLViewer); |
| 171 }; | 179 }; |
| 172 | 180 |
| 173 } // namespace html_viewer | 181 } // namespace html_viewer |
| 174 | 182 |
| 175 MojoResult MojoMain(MojoHandle shell_handle) { | 183 MojoResult MojoMain(MojoHandle shell_handle) { |
| 176 mojo::ApplicationRunner runner(new html_viewer::HTMLViewer); | 184 mojo::ApplicationRunner runner(new html_viewer::HTMLViewer); |
| 177 return runner.Run(shell_handle); | 185 return runner.Run(shell_handle); |
| 178 } | 186 } |
| OLD | NEW |