[Android] Introduce new UMA action for when user copies Image URL from context menu
[chromium-blink-merge.git] / mojo / services / network / network_context.cc
blob32248eafa475062706c7f4119f4202e647465fae
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
3 // found in the LICENSE file.
5 #include "mojo/services/network/network_context.h"
7 #include <algorithm>
8 #include <vector>
10 #include "base/base_paths.h"
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/path_service.h"
14 #include "mojo/common/user_agent.h"
15 #include "mojo/services/network/url_loader_impl.h"
16 #include "net/log/net_log_util.h"
17 #include "net/log/write_to_file_net_log_observer.h"
18 #include "net/proxy/proxy_service.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_builder.h"
22 namespace mojo {
24 namespace {
25 // Logs network information to the specified file.
26 const char kLogNetLog[] = "log-net-log";
29 class NetworkContext::MojoNetLog : public net::NetLog {
30 public:
31 MojoNetLog() {
32 const base::CommandLine* command_line =
33 base::CommandLine::ForCurrentProcess();
34 if (!command_line->HasSwitch(kLogNetLog))
35 return;
37 base::FilePath log_path = command_line->GetSwitchValuePath(kLogNetLog);
38 base::ScopedFILE file;
39 #if defined(OS_WIN)
40 file.reset(_wfopen(log_path.value().c_str(), L"w"));
41 #elif defined(OS_POSIX)
42 file.reset(fopen(log_path.value().c_str(), "w"));
43 #endif
44 if (!file) {
45 LOG(ERROR) << "Could not open file " << log_path.value()
46 << " for net logging";
47 } else {
48 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver());
49 write_to_file_observer_->set_capture_mode(
50 net::NetLogCaptureMode::IncludeCookiesAndCredentials());
51 write_to_file_observer_->StartObserving(this, file.Pass(), nullptr,
52 nullptr);
56 ~MojoNetLog() override {
57 if (write_to_file_observer_)
58 write_to_file_observer_->StopObserving(nullptr);
61 private:
62 scoped_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_;
64 DISALLOW_COPY_AND_ASSIGN(MojoNetLog);
67 NetworkContext::NetworkContext(
68 scoped_ptr<net::URLRequestContext> url_request_context)
69 : net_log_(new MojoNetLog),
70 url_request_context_(url_request_context.Pass()),
71 in_shutdown_(false) {
72 url_request_context_->set_net_log(net_log_.get());
75 NetworkContext::NetworkContext(const base::FilePath& base_path)
76 : NetworkContext(MakeURLRequestContext(base_path)) {
79 NetworkContext::~NetworkContext() {
80 in_shutdown_ = true;
81 // TODO(darin): Be careful about destruction order of member variables?
83 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the
84 // corresponding net::URLRequestContext is going away with this
85 // NetworkContext. The loaders can be deregistering themselves in Cleanup(),
86 // so iterate over a copy.
87 for (auto& url_loader : url_loaders_) {
88 url_loader->Cleanup();
92 void NetworkContext::RegisterURLLoader(URLLoaderImpl* url_loader) {
93 DCHECK(url_loaders_.count(url_loader) == 0);
94 url_loaders_.insert(url_loader);
97 void NetworkContext::DeregisterURLLoader(URLLoaderImpl* url_loader) {
98 if (!in_shutdown_) {
99 size_t removed_count = url_loaders_.erase(url_loader);
100 DCHECK(removed_count);
104 size_t NetworkContext::GetURLLoaderCountForTesting() {
105 return url_loaders_.size();
108 // static
109 scoped_ptr<net::URLRequestContext> NetworkContext::MakeURLRequestContext(
110 const base::FilePath& base_path) {
111 net::URLRequestContextBuilder builder;
112 builder.set_accept_language("en-us,en");
113 builder.set_user_agent(mojo::common::GetUserAgent());
114 builder.set_proxy_service(net::ProxyService::CreateDirect());
115 builder.set_transport_security_persister_path(base_path);
117 net::URLRequestContextBuilder::HttpCacheParams cache_params;
118 #if defined(OS_ANDROID)
119 // On Android, we store the cache on disk becase we can run only a single
120 // instance of the shell at a time.
121 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::DISK;
122 cache_params.path = base_path.Append(FILE_PATH_LITERAL("Cache"));
123 #else
124 // On desktop, we store the cache in memory so we can run many shells
125 // in parallel when running tests, otherwise the network services in each
126 // shell will corrupt the disk cache.
127 cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY;
128 #endif
130 builder.EnableHttpCache(cache_params);
131 builder.set_file_enabled(true);
133 return make_scoped_ptr(builder.Build());
136 } // namespace mojo