Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: sky/viewer/content_handler_impl.cc

Issue 741453002: Make sure that Content Handled application can be connected multiple times. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Follow review Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "sky/viewer/content_handler_impl.h" 5 #include "sky/viewer/content_handler_impl.h"
6 6
7 #include "base/bind.h"
8 #include "mojo/public/cpp/application/connect.h"
9 #include "mojo/services/public/interfaces/network/network_service.mojom.h"
7 #include "sky/viewer/document_view.h" 10 #include "sky/viewer/document_view.h"
8 11
9 namespace sky { 12 namespace sky {
10 13
14 class SkyApplication : public mojo::Application {
15 public:
16 SkyApplication(scoped_refptr<base::MessageLoopProxy> compositor_thread,
17 mojo::ShellPtr shell,
18 mojo::URLResponsePtr response)
19 : compositor_thread_(compositor_thread),
20 url_(response->url),
21 shell_(shell.Pass()),
22 initial_response_(response.Pass()),
23 view_count_(0) {
24 shell_.set_client(this);
25 mojo::ServiceProviderPtr service_provider;
26 shell_->ConnectToApplication("mojo:network_service",
27 mojo::GetProxy(&service_provider));
28 mojo::ConnectToService(service_provider.get(), &network_service_);
29 }
30
31 void Initialize(mojo::Array<mojo::String> args) override {}
32
33 void AcceptConnection(const mojo::String& requestor_url,
34 mojo::ServiceProviderPtr provider) override {
35 ++view_count_;
36 if (initial_response_) {
37 OnResponseReceived(nullptr, provider.Pass(), initial_response_.Pass());
38 } else {
39 mojo::URLLoaderPtr* loader = new mojo::URLLoaderPtr;
40 network_service_->CreateURLLoader(mojo::GetProxy(loader));
41 mojo::URLRequestPtr request(mojo::URLRequest::New());
42 request->url = url_;
43 request->auto_follow_redirects = true;
44 (*loader)->Start(request.Pass(),
45 base::Bind(&SkyApplication::OnResponseReceived,
46 base::Unretained(this), base::Owned(loader),
47 base::Passed(&provider)));
48 }
49 }
50
51 private:
52 void OnViewDestroyed() {
53 --view_count_;
54 if (view_count_ == 0) {
55 delete this;
56 }
57 }
58
59 void OnResponseReceived(mojo::URLLoaderPtr* loader,
60 mojo::ServiceProviderPtr provider,
61 mojo::URLResponsePtr response) {
62 new DocumentView(
63 base::Bind(&SkyApplication::OnViewDestroyed, base::Unretained(this)),
64 provider.Pass(), response.Pass(), shell_.get(), compositor_thread_);
65 }
66
67 scoped_refptr<base::MessageLoopProxy> compositor_thread_;
68 mojo::String url_;
69 mojo::ShellPtr shell_;
70 mojo::NetworkServicePtr network_service_;
71 mojo::URLResponsePtr initial_response_;
72 uint32_t view_count_;
73 };
74
11 ContentHandlerImpl::ContentHandlerImpl( 75 ContentHandlerImpl::ContentHandlerImpl(
12 scoped_refptr<base::MessageLoopProxy> compositor_thread) 76 scoped_refptr<base::MessageLoopProxy> compositor_thread)
13 : compositor_thread_(compositor_thread) { 77 : compositor_thread_(compositor_thread) {
14 } 78 }
15 79
16 ContentHandlerImpl::~ContentHandlerImpl() { 80 ContentHandlerImpl::~ContentHandlerImpl() {
17 } 81 }
18 82
19 void ContentHandlerImpl::StartApplication(mojo::ShellPtr shell, 83 void ContentHandlerImpl::StartApplication(mojo::ShellPtr shell,
20 mojo::URLResponsePtr response) { 84 mojo::URLResponsePtr response) {
21 new DocumentView(response.Pass(), shell.Pass(), compositor_thread_); 85 new SkyApplication(compositor_thread_, shell.Pass(), response.Pass());
22 } 86 }
23 87
24 } // namespace sky 88 } // namespace sky
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698