OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/services/network/network_service_delegate.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/at_exit.h" | |
10 #include "base/base_paths.h" | |
11 #include "base/bind.h" | |
12 #include "base/command_line.h" | |
13 #include "base/files/file_path.h" | |
14 #include "base/macros.h" | |
15 #include "base/message_loop/message_loop.h" | |
16 #include "base/path_service.h" | |
17 #include "mojo/message_pump/message_pump_mojo.h" | |
18 #include "mojo/services/network/cookie_store_impl.h" | |
19 #include "mojo/services/network/network_service_delegate_observer.h" | |
20 #include "mojo/services/network/network_service_impl.h" | |
21 #include "mojo/services/network/url_loader_factory_impl.h" | |
22 #include "mojo/services/network/web_socket_factory_impl.h" | |
23 #include "mojo/shell/public/cpp/connection.h" | |
24 #include "mojo/util/capture_util.h" | |
25 | |
26 namespace { | |
27 | |
28 const char kUserDataDir[] = "user-data-dir"; | |
29 | |
30 } // namespace | |
31 | |
32 namespace mojo { | |
33 | |
34 NetworkServiceDelegate::NetworkServiceDelegate() { | |
35 ref_factory_.set_quit_closure( | |
36 base::Bind(&NetworkServiceDelegate::Quit, base::Unretained(this))); | |
37 } | |
38 NetworkServiceDelegate::~NetworkServiceDelegate() {} | |
39 | |
40 void NetworkServiceDelegate::AddObserver( | |
41 NetworkServiceDelegateObserver* observer) { | |
42 observers_.AddObserver(observer); | |
43 } | |
44 | |
45 void NetworkServiceDelegate::RemoveObserver( | |
46 NetworkServiceDelegateObserver* observer) { | |
47 observers_.RemoveObserver(observer); | |
48 } | |
49 | |
50 void NetworkServiceDelegate::Initialize(Connector* connector, | |
51 const Identity& identity, | |
52 uint32_t id) { | |
53 // TODO(erg): Find everything else that writes to the filesystem and | |
54 // transition it to proxying mojo:filesystem. We shouldn't have any path | |
55 // calculation code here, but sadly need it until the transition is done. In | |
56 // the mean time, manually handle the user-data-dir switch (which gets set in | |
57 // tests) so that tests are writing to a temp dir. | |
58 base::FilePath base_path; | |
59 const base::CommandLine* command_line = | |
60 base::CommandLine::ForCurrentProcess(); | |
61 if (command_line->HasSwitch(kUserDataDir)) { | |
62 base_path = command_line->GetSwitchValuePath(kUserDataDir); | |
63 } else { | |
64 CHECK(PathService::Get(base::DIR_TEMP, &base_path)); | |
65 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); | |
66 } | |
67 | |
68 context_.reset(new NetworkContext(base_path, this)); | |
69 tracing_.Initialize(connector, identity.name()); | |
70 } | |
71 | |
72 bool NetworkServiceDelegate::AcceptConnection(Connection* connection) { | |
73 DCHECK(context_); | |
74 connection->AddInterface<CookieStore>(this); | |
75 connection->AddInterface<NetworkService>(this); | |
76 connection->AddInterface<URLLoaderFactory>(this); | |
77 connection->AddInterface<WebSocketFactory>(this); | |
78 return true; | |
79 } | |
80 | |
81 void NetworkServiceDelegate::Create(Connection* connection, | |
82 InterfaceRequest<NetworkService> request) { | |
83 new NetworkServiceImpl(ref_factory_.CreateRef(), std::move(request)); | |
84 } | |
85 | |
86 void NetworkServiceDelegate::Create(Connection* connection, | |
87 InterfaceRequest<CookieStore> request) { | |
88 // TODO(beng): need to find a way to get content origin. | |
89 new CookieStoreImpl( | |
90 context_.get(), GURL(), | |
91 ref_factory_.CreateRef(), std::move(request)); | |
92 } | |
93 | |
94 void NetworkServiceDelegate::Create( | |
95 Connection* connection, | |
96 InterfaceRequest<WebSocketFactory> request) { | |
97 new WebSocketFactoryImpl(context_.get(), ref_factory_.CreateRef(), | |
98 std::move(request)); | |
99 } | |
100 | |
101 void NetworkServiceDelegate::Create( | |
102 Connection* connection, | |
103 InterfaceRequest<URLLoaderFactory> request) { | |
104 new URLLoaderFactoryImpl(context_.get(), ref_factory_.CreateRef(), | |
105 std::move(request)); | |
106 } | |
107 | |
108 void NetworkServiceDelegate::Quit() { | |
109 // Destroy the NetworkContext now as it requires MessageLoop::current() upon | |
110 // destruction and it is the last moment we know for sure that it is | |
111 // running. | |
112 context_.reset(); | |
113 } | |
114 | |
115 } // namespace mojo | |
OLD | NEW |