OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/android/process_utils.h" | 5 #include "chrome/browser/android/process_utils.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "chrome/browser/browser_process.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/profiles/profile_manager.h" | |
14 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "content/public/browser/render_process_host.h" | 12 #include "content/public/browser/render_process_host.h" |
17 #include "jni/ProcessUtils_jni.h" | 13 #include "jni/ProcessUtils_jni.h" |
18 #include "net/http/http_network_session.h" | |
19 #include "net/http/http_transaction_factory.h" | |
20 #include "net/url_request/url_request_context.h" | |
21 #include "net/url_request/url_request_context_getter.h" | |
22 | 14 |
23 namespace { | 15 namespace { |
24 | 16 |
25 void CloseIdleConnectionsForProfile( | |
26 scoped_refptr<net::URLRequestContextGetter> context_getter) { | |
27 DCHECK(context_getter.get()); | |
28 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) { | |
29 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | |
30 base::Bind(&CloseIdleConnectionsForProfile, | |
31 context_getter)); | |
32 return; | |
33 } | |
34 | |
35 net::URLRequestContext* context = context_getter->GetURLRequestContext(); | |
36 if (!context) | |
37 return; | |
38 net::HttpNetworkSession* session = | |
39 context->http_transaction_factory()->GetSession(); | |
40 if (session) | |
41 session->CloseIdleConnections(); | |
42 } | |
43 | |
44 // Only accessed from the JNI thread by ToggleWebKitSharedTimers() which is | 17 // Only accessed from the JNI thread by ToggleWebKitSharedTimers() which is |
45 // implemented below. | 18 // implemented below. |
46 base::LazyInstance<std::vector<int /* process id */> > g_suspended_processes = | 19 base::LazyInstance<std::vector<int /* process id */> > g_suspended_processes = |
47 LAZY_INSTANCE_INITIALIZER; | 20 LAZY_INSTANCE_INITIALIZER; |
48 | 21 |
49 // Suspends timers in all current render processes. | 22 // Suspends timers in all current render processes. |
50 void SuspendWebKitSharedTimers(std::vector<int>* suspended_processes) { | 23 void SuspendWebKitSharedTimers(std::vector<int>* suspended_processes) { |
51 for (content::RenderProcessHost::iterator i( | 24 for (content::RenderProcessHost::iterator i( |
52 content::RenderProcessHost::AllHostsIterator()); | 25 content::RenderProcessHost::AllHostsIterator()); |
53 !i.IsAtEnd(); i.Advance()) { | 26 !i.IsAtEnd(); i.Advance()) { |
(...skipping 20 matching lines...) Expand all Loading... |
74 std::vector<int>* suspended_processes = &g_suspended_processes.Get(); | 47 std::vector<int>* suspended_processes = &g_suspended_processes.Get(); |
75 if (suspend) { | 48 if (suspend) { |
76 DCHECK(suspended_processes->empty()); | 49 DCHECK(suspended_processes->empty()); |
77 SuspendWebKitSharedTimers(suspended_processes); | 50 SuspendWebKitSharedTimers(suspended_processes); |
78 } else { | 51 } else { |
79 ResumeWebkitSharedTimers(*suspended_processes); | 52 ResumeWebkitSharedTimers(*suspended_processes); |
80 suspended_processes->clear(); | 53 suspended_processes->clear(); |
81 } | 54 } |
82 } | 55 } |
83 | 56 |
84 static void CloseIdleConnections(JNIEnv* env, jclass obj) { | |
85 // Iterate through all loaded profiles (and their associated incognito | |
86 // profiles if created), and close the idle connections associated with each | |
87 // one. | |
88 std::vector<Profile*> profiles( | |
89 g_browser_process->profile_manager()->GetLoadedProfiles()); | |
90 for (std::vector<Profile*>::iterator i = profiles.begin(); | |
91 i != profiles.end(); i++) { | |
92 Profile* profile = *i; | |
93 CloseIdleConnectionsForProfile(profile->GetRequestContext()); | |
94 if (profile->HasOffTheRecordProfile()) { | |
95 CloseIdleConnectionsForProfile( | |
96 profile->GetOffTheRecordProfile()->GetRequestContext()); | |
97 } | |
98 } | |
99 } | |
100 | |
101 bool RegisterProcessUtils(JNIEnv* env) { | 57 bool RegisterProcessUtils(JNIEnv* env) { |
102 return RegisterNativesImpl(env); | 58 return RegisterNativesImpl(env); |
103 } | 59 } |
OLD | NEW |