Commit 15df4776 authored by sky's avatar sky Committed by Commit bot

Changes html_viewer to get services from appropriate application

There are actually two changes here:
. Convert HTMLViewerApplication to use an ApplicationImpl rather than
  be an Application.
. Make HTMLDocument use services from the application it was created
  for rather than HTMLViewer's application.

BUG=none
TEST=none
R=jam@chromium.org

Review URL: https://codereview.chromium.org/1165013005

Cr-Commit-Position: refs/heads/master@{#333050}
parent 404f4d1d
...@@ -142,21 +142,20 @@ bool CanNavigateLocally(blink::WebFrame* frame, ...@@ -142,21 +142,20 @@ bool CanNavigateLocally(blink::WebFrame* frame,
} // namespace } // namespace
HTMLDocument::HTMLDocument( HTMLDocument::HTMLDocument(mojo::ApplicationImpl* html_document_app,
mojo::InterfaceRequest<mojo::ServiceProvider> services, mojo::ApplicationConnection* connection,
URLResponsePtr response, URLResponsePtr response,
mojo::ShellPtr shell, Setup* setup)
Setup* setup) : app_refcount_(
: app_refcount_(setup->app()->app_lifetime_helper()->CreateAppRefCount()), html_document_app->app_lifetime_helper()->CreateAppRefCount()),
html_document_app_(html_document_app),
response_(response.Pass()), response_(response.Pass()),
shell_(shell.Pass()),
web_view_(nullptr), web_view_(nullptr),
root_(nullptr), root_(nullptr),
view_manager_client_factory_(shell_.get(), this), view_manager_client_factory_(html_document_app->shell(), this),
setup_(setup) { setup_(setup) {
exported_services_.AddService(this); connection->AddService(this);
exported_services_.AddService(&view_manager_client_factory_); connection->AddService(&view_manager_client_factory_);
exported_services_.Bind(services.Pass());
if (setup_->did_init()) if (setup_->did_init())
Load(response_.Pass()); Load(response_.Pass());
} }
...@@ -272,13 +271,13 @@ void HTMLDocument::initializeLayerTreeView() { ...@@ -272,13 +271,13 @@ void HTMLDocument::initializeLayerTreeView() {
mojo::URLRequestPtr request(mojo::URLRequest::New()); mojo::URLRequestPtr request(mojo::URLRequest::New());
request->url = mojo::String::From("mojo:surfaces_service"); request->url = mojo::String::From("mojo:surfaces_service");
mojo::SurfacePtr surface; mojo::SurfacePtr surface;
setup_->app()->ConnectToService(request.Pass(), &surface); html_document_app_->ConnectToService(request.Pass(), &surface);
// TODO(jamesr): Should be mojo:gpu_service // TODO(jamesr): Should be mojo:gpu_service
mojo::URLRequestPtr request2(mojo::URLRequest::New()); mojo::URLRequestPtr request2(mojo::URLRequest::New());
request2->url = mojo::String::From("mojo:view_manager"); request2->url = mojo::String::From("mojo:view_manager");
mojo::GpuPtr gpu_service; mojo::GpuPtr gpu_service;
setup_->app()->ConnectToService(request2.Pass(), &gpu_service); html_document_app_->ConnectToService(request2.Pass(), &gpu_service);
web_layer_tree_view_impl_.reset(new WebLayerTreeViewImpl( web_layer_tree_view_impl_.reset(new WebLayerTreeViewImpl(
setup_->compositor_thread(), surface.Pass(), gpu_service.Pass())); setup_->compositor_thread(), surface.Pass(), gpu_service.Pass()));
} }
...@@ -292,8 +291,8 @@ blink::WebMediaPlayer* HTMLDocument::createMediaPlayer( ...@@ -292,8 +291,8 @@ blink::WebMediaPlayer* HTMLDocument::createMediaPlayer(
const blink::WebURL& url, const blink::WebURL& url,
blink::WebMediaPlayerClient* client, blink::WebMediaPlayerClient* client,
blink::WebContentDecryptionModule* initial_cdm) { blink::WebContentDecryptionModule* initial_cdm) {
return setup_->media_factory()->CreateMediaPlayer(frame, url, client, return setup_->media_factory()->CreateMediaPlayer(
initial_cdm, shell_.get()); frame, url, client, initial_cdm, html_document_app_->shell());
} }
blink::WebFrame* HTMLDocument::createChildFrame( blink::WebFrame* HTMLDocument::createChildFrame(
...@@ -409,7 +408,6 @@ void HTMLDocument::OnViewViewportMetricsChanged( ...@@ -409,7 +408,6 @@ void HTMLDocument::OnViewViewportMetricsChanged(
void HTMLDocument::OnViewDestroyed(View* view) { void HTMLDocument::OnViewDestroyed(View* view) {
DCHECK_EQ(view, root_); DCHECK_EQ(view, root_);
root_ = nullptr; root_ = nullptr;
shell_->QuitApplication();
} }
void HTMLDocument::OnViewInputEvent(View* view, const mojo::EventPtr& event) { void HTMLDocument::OnViewInputEvent(View* view, const mojo::EventPtr& event) {
......
...@@ -51,16 +51,12 @@ class HTMLDocument : public blink::WebViewClient, ...@@ -51,16 +51,12 @@ class HTMLDocument : public blink::WebViewClient,
public mojo::InterfaceFactory<mojo::AxProvider> { public mojo::InterfaceFactory<mojo::AxProvider> {
public: public:
// Load a new HTMLDocument with |response|. // Load a new HTMLDocument with |response|.
// // |html_document_app| is the application this app was created in, and
// |services| should be used to implement a ServiceProvider which exposes // |connection| the specific connection triggering this new instance.
// services to the connecting application. // |setup| is used to obtain init type state (such as resources).
// Commonly, the connecting application is the ViewManager and it will HTMLDocument(mojo::ApplicationImpl* html_document_app,
// request ViewManagerClient. mojo::ApplicationConnection* connection,
//
// |shell| is the Shell connection for this mojo::Application.
HTMLDocument(mojo::InterfaceRequest<mojo::ServiceProvider> services,
mojo::URLResponsePtr response, mojo::URLResponsePtr response,
mojo::ShellPtr shell,
Setup* setup); Setup* setup);
~HTMLDocument() override; ~HTMLDocument() override;
...@@ -140,10 +136,9 @@ class HTMLDocument : public blink::WebViewClient, ...@@ -140,10 +136,9 @@ class HTMLDocument : public blink::WebViewClient,
void ConvertLocalFrameToRemoteFrame(blink::WebLocalFrame* frame); void ConvertLocalFrameToRemoteFrame(blink::WebLocalFrame* frame);
scoped_ptr<mojo::AppRefCount> app_refcount_; scoped_ptr<mojo::AppRefCount> app_refcount_;
mojo::ApplicationImpl* html_document_app_;
mojo::URLResponsePtr response_; mojo::URLResponsePtr response_;
mojo::ServiceProviderImpl exported_services_;
mojo::ServiceProviderPtr embedder_service_provider_; mojo::ServiceProviderPtr embedder_service_provider_;
mojo::ShellPtr shell_;
mojo::LazyInterfacePtr<mojo::NavigatorHost> navigator_host_; mojo::LazyInterfacePtr<mojo::NavigatorHost> navigator_host_;
blink::WebView* web_view_; blink::WebView* web_view_;
mojo::View* root_; mojo::View* root_;
......
...@@ -40,35 +40,44 @@ namespace html_viewer { ...@@ -40,35 +40,44 @@ namespace html_viewer {
class HTMLViewer; class HTMLViewer;
class HTMLViewerApplication : public mojo::Application { // ApplicationDelegate created by the content handler for a specific url.
class HTMLDocumentApplicationDelegate : public mojo::ApplicationDelegate {
public: public:
HTMLViewerApplication(InterfaceRequest<Application> request, HTMLDocumentApplicationDelegate(
URLResponsePtr response, mojo::InterfaceRequest<mojo::Application> request,
Setup* setup) mojo::URLResponsePtr response,
: app_refcount_(setup->app()->app_lifetime_helper()->CreateAppRefCount()), Setup* setup,
scoped_ptr<mojo::AppRefCount> parent_app_refcount)
: app_(this,
request.Pass(),
base::Bind(&HTMLDocumentApplicationDelegate::OnTerminate,
base::Unretained(this))),
parent_app_refcount_(parent_app_refcount.Pass()),
url_(response->url), url_(response->url),
binding_(this, request.Pass()),
initial_response_(response.Pass()), initial_response_(response.Pass()),
setup_(setup) { setup_(setup) {}
}
private:
~HTMLDocumentApplicationDelegate() override {}
~HTMLViewerApplication() override { // Callback from the quit closure. We key off this rather than
// ApplicationDelegate::Quit() as we don't want to shut down the messageloop
// when we quit (the messageloop is shared among multiple
// HTMLDocumentApplicationDelegates).
void OnTerminate() {
delete this;
} }
void Initialize(ShellPtr shell, const String& url) override { // ApplicationDelegate;
shell_ = shell.Pass(); void Initialize(mojo::ApplicationImpl* app) override {
mojo::URLRequestPtr request(mojo::URLRequest::New()); mojo::URLRequestPtr request(mojo::URLRequest::New());
request->url = mojo::String::From("mojo:network_service"); request->url = mojo::String::From("mojo:network_service");
setup_->app()->ConnectToService(request.Pass(), &network_service_); app_.ConnectToService(request.Pass(), (&network_service_));
} }
bool ConfigureIncomingConnection(
void AcceptConnection(const String& requestor_url, mojo::ApplicationConnection* connection) override {
InterfaceRequest<ServiceProvider> services,
ServiceProviderPtr exposed_services,
const String& url) override {
if (initial_response_) { if (initial_response_) {
OnResponseReceived(URLLoaderPtr(), services.Pass(), OnResponseReceived(URLLoaderPtr(), connection, initial_response_.Pass());
initial_response_.Pass());
} else { } else {
URLLoaderPtr loader; URLLoaderPtr loader;
network_service_->CreateURLLoader(GetProxy(&loader)); network_service_->CreateURLLoader(GetProxy(&loader));
...@@ -76,60 +85,59 @@ class HTMLViewerApplication : public mojo::Application { ...@@ -76,60 +85,59 @@ class HTMLViewerApplication : public mojo::Application {
request->url = url_; request->url = url_;
request->auto_follow_redirects = true; request->auto_follow_redirects = true;
// |loader| will be pass to the OnResponseReceived method through a // |loader| will be passed to the OnResponseReceived method through a
// callback. Because order of evaluation is undefined, a reference to the // callback. Because order of evaluation is undefined, a reference to the
// raw pointer is needed. // raw pointer is needed.
mojo::URLLoader* raw_loader = loader.get(); mojo::URLLoader* raw_loader = loader.get();
raw_loader->Start( raw_loader->Start(
request.Pass(), request.Pass(),
base::Bind(&HTMLViewerApplication::OnResponseReceived, base::Bind(&HTMLDocumentApplicationDelegate::OnResponseReceived,
base::Unretained(this), base::Passed(&loader), base::Unretained(this), base::Passed(&loader),
base::Passed(&services))); connection));
} }
return true;
} }
void OnQuitRequested(const mojo::Callback<void(bool)>& callback) override {
callback.Run(true);
delete this;
}
private:
void OnResponseReceived(URLLoaderPtr loader, void OnResponseReceived(URLLoaderPtr loader,
InterfaceRequest<ServiceProvider> services, mojo::ApplicationConnection* connection,
URLResponsePtr response) { URLResponsePtr response) {
// HTMLDocument is destroyed when the hosting view is destroyed. // HTMLDocument is destroyed when the hosting view is destroyed.
// TODO(sky): when headless, this leaks. // TODO(sky): when headless, this leaks.
new HTMLDocument(services.Pass(), response.Pass(), shell_.Pass(), setup_); // TODO(sky): this needs to delete if serviceprovider goes away too.
new HTMLDocument(&app_, connection, response.Pass(), setup_);
} }
scoped_ptr<mojo::AppRefCount> app_refcount_; mojo::ApplicationImpl app_;
String url_; // AppRefCount of the parent (HTMLViewer).
mojo::StrongBinding<mojo::Application> binding_; scoped_ptr<mojo::AppRefCount> parent_app_refcount_;
ShellPtr shell_; const String url_;
mojo::NetworkServicePtr network_service_; mojo::NetworkServicePtr network_service_;
URLResponsePtr initial_response_; URLResponsePtr initial_response_;
Setup* setup_; Setup* setup_;
DISALLOW_COPY_AND_ASSIGN(HTMLViewerApplication); DISALLOW_COPY_AND_ASSIGN(HTMLDocumentApplicationDelegate);
}; };
class ContentHandlerImpl : public mojo::ContentHandler { class ContentHandlerImpl : public mojo::ContentHandler {
public: public:
ContentHandlerImpl(Setup* setup, ContentHandlerImpl(Setup* setup,
mojo::ApplicationImpl* app,
mojo::InterfaceRequest<ContentHandler> request) mojo::InterfaceRequest<ContentHandler> request)
: setup_(setup), : setup_(setup), app_(app), binding_(this, request.Pass()) {}
binding_(this, request.Pass()) {}
~ContentHandlerImpl() override {} ~ContentHandlerImpl() override {}
private: private:
// Overridden from ContentHandler: // Overridden from ContentHandler:
void StartApplication(InterfaceRequest<mojo::Application> request, void StartApplication(InterfaceRequest<mojo::Application> request,
URLResponsePtr response) override { URLResponsePtr response) override {
// HTMLViewerApplication is owned by the binding. // HTMLDocumentApplicationDelegate deletes itself.
new HTMLViewerApplication(request.Pass(), response.Pass(), setup_); new HTMLDocumentApplicationDelegate(
request.Pass(), response.Pass(), setup_,
app_->app_lifetime_helper()->CreateAppRefCount());
} }
Setup* setup_; Setup* setup_;
mojo::ApplicationImpl* app_;
mojo::StrongBinding<mojo::ContentHandler> binding_; mojo::StrongBinding<mojo::ContentHandler> binding_;
DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl);
...@@ -138,12 +146,13 @@ class ContentHandlerImpl : public mojo::ContentHandler { ...@@ -138,12 +146,13 @@ class ContentHandlerImpl : public mojo::ContentHandler {
class HTMLViewer : public mojo::ApplicationDelegate, class HTMLViewer : public mojo::ApplicationDelegate,
public mojo::InterfaceFactory<ContentHandler> { public mojo::InterfaceFactory<ContentHandler> {
public: public:
HTMLViewer() {} HTMLViewer() : app_(nullptr) {}
~HTMLViewer() override {} ~HTMLViewer() override {}
private: private:
// Overridden from ApplicationDelegate: // Overridden from ApplicationDelegate:
void Initialize(mojo::ApplicationImpl* app) override { void Initialize(mojo::ApplicationImpl* app) override {
app_ = app;
setup_.reset(new Setup(app)); setup_.reset(new Setup(app));
} }
...@@ -162,10 +171,11 @@ class HTMLViewer : public mojo::ApplicationDelegate, ...@@ -162,10 +171,11 @@ class HTMLViewer : public mojo::ApplicationDelegate,
// Overridden from InterfaceFactory<ContentHandler> // Overridden from InterfaceFactory<ContentHandler>
void Create(ApplicationConnection* connection, void Create(ApplicationConnection* connection,
mojo::InterfaceRequest<ContentHandler> request) override { mojo::InterfaceRequest<ContentHandler> request) override {
new ContentHandlerImpl(setup_.get(), request.Pass()); new ContentHandlerImpl(setup_.get(), app_, request.Pass());
} }
scoped_ptr<Setup> setup_; scoped_ptr<Setup> setup_;
mojo::ApplicationImpl* app_;
DISALLOW_COPY_AND_ASSIGN(HTMLViewer); DISALLOW_COPY_AND_ASSIGN(HTMLViewer);
}; };
......
...@@ -49,7 +49,6 @@ class Setup { ...@@ -49,7 +49,6 @@ class Setup {
void InitIfNecessary(const gfx::Size& screen_size_in_pixels, void InitIfNecessary(const gfx::Size& screen_size_in_pixels,
float device_pixel_ratio); float device_pixel_ratio);
mojo::ApplicationImpl* app() const { return app_; }
bool is_headless() const { return is_headless_; } bool is_headless() const { return is_headless_; }
bool did_init() const { return did_init_; } bool did_init() const { return did_init_; }
...@@ -66,6 +65,9 @@ class Setup { ...@@ -66,6 +65,9 @@ class Setup {
MediaFactory* media_factory() { return media_factory_.get(); } MediaFactory* media_factory() { return media_factory_.get(); }
private: private:
// App for HTMLViewer, not the document's app.
// WARNING: do not expose this. It's too easy to use the wrong one.
// HTMLDocument should be using the application it creates, not this one.
mojo::ApplicationImpl* app_; mojo::ApplicationImpl* app_;
resource_provider::ResourceLoader resource_loader_; resource_provider::ResourceLoader resource_loader_;
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "mojo/application/public/cpp/app_lifetime_helper.h" #include "mojo/application/public/cpp/app_lifetime_helper.h"
#include "mojo/application/public/cpp/application_connection.h" #include "mojo/application/public/cpp/application_connection.h"
#include "mojo/application/public/cpp/application_delegate.h" #include "mojo/application/public/cpp/application_delegate.h"
...@@ -133,6 +134,7 @@ class ApplicationImpl : public Application, ...@@ -133,6 +134,7 @@ class ApplicationImpl : public Application,
base::Closure termination_closure_; base::Closure termination_closure_;
AppLifetimeHelper app_lifetime_helper_; AppLifetimeHelper app_lifetime_helper_;
bool quit_requested_; bool quit_requested_;
base::WeakPtrFactory<ApplicationImpl> weak_factory_;
MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl);
}; };
......
...@@ -35,7 +35,8 @@ ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, ...@@ -35,7 +35,8 @@ ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
binding_(this, request.Pass()), binding_(this, request.Pass()),
termination_closure_(termination_closure), termination_closure_(termination_closure),
app_lifetime_helper_(this), app_lifetime_helper_(this),
quit_requested_(false) { quit_requested_(false),
weak_factory_(this) {
} }
void ApplicationImpl::ClearConnections() { void ApplicationImpl::ClearConnections() {
...@@ -140,7 +141,10 @@ void ApplicationImpl::OnQuitRequested(const Callback<void(bool)>& callback) { ...@@ -140,7 +141,10 @@ void ApplicationImpl::OnQuitRequested(const Callback<void(bool)>& callback) {
} }
void ApplicationImpl::OnConnectionError() { void ApplicationImpl::OnConnectionError() {
base::WeakPtr<ApplicationImpl> ptr(weak_factory_.GetWeakPtr());
QuitNow(); QuitNow();
if (!ptr)
return;
shell_ = nullptr; shell_ = nullptr;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment