OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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 "chrome/browser/suspend_controller.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/task.h" | |
10 #include "base/thread.h" | |
11 #include "chrome/browser/browser_process.h" | |
12 #include "chrome/browser/profile.h" | |
13 #include "chrome/browser/resource_dispatcher_host.h" | |
14 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
15 #include "net/url_request/url_request_job.h" | |
16 #include "net/url_request/url_request_job_tracker.h" | |
17 | |
18 // Static member. | |
19 bool SuspendController::is_suspended_ = false; | |
20 | |
21 // Static method. | |
22 void SuspendController::OnSuspend(Profile* profile) { | |
23 if (is_suspended_) | |
24 return; | |
25 is_suspended_ = true; | |
26 | |
27 LOG(INFO) << "Received APM suspend message"; | |
28 | |
29 SuspendController* controller = g_browser_process->suspend_controller(); | |
30 | |
31 MessageLoop* io_loop = g_browser_process->io_thread()->message_loop(); | |
32 io_loop->PostTask(FROM_HERE, | |
33 NewRunnableMethod(controller, | |
34 &SuspendController::StopRequests, | |
35 profile)); | |
36 | |
37 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); | |
38 SafeBrowsingService* safe_browsing_service = rdh->safe_browsing_service(); | |
39 io_loop->PostTask(FROM_HERE, | |
40 NewRunnableMethod(safe_browsing_service, | |
41 &SafeBrowsingService::OnSuspend)); | |
42 } | |
43 | |
44 // Static method. | |
45 void SuspendController::OnResume(Profile* profile) { | |
46 if (!is_suspended_) | |
47 return; | |
48 is_suspended_ = false; | |
49 | |
50 LOG(INFO) << "Received APM resume message"; | |
51 | |
52 SuspendController* controller = g_browser_process->suspend_controller(); | |
53 | |
54 MessageLoop* io_loop = g_browser_process->io_thread()->message_loop(); | |
55 io_loop->PostTask(FROM_HERE, | |
56 NewRunnableMethod(controller, | |
57 &SuspendController::AllowNewRequests, | |
58 profile)); | |
59 | |
60 ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); | |
61 SafeBrowsingService* safe_browsing_service = rdh->safe_browsing_service(); | |
62 io_loop->PostTask(FROM_HERE, | |
63 NewRunnableMethod(safe_browsing_service, | |
64 &SafeBrowsingService::OnResume)); | |
65 } | |
66 | |
67 void SuspendController::StopRequests(Profile* profile) { | |
68 // Cancel all requests and stop creating new ones. | |
69 URLRequestJobTracker::JobIterator it = g_url_request_job_tracker.begin(); | |
70 for (;it != g_url_request_job_tracker.end(); ++it) { | |
71 URLRequestJob* job = const_cast<URLRequestJob*>(*it); | |
72 job->Kill(); | |
73 } | |
74 | |
75 // Close the network session. | |
76 profile->GetRequestContext()->http_transaction_factory()->Suspend(true); | |
77 } | |
78 | |
79 void SuspendController::AllowNewRequests(Profile* profile) { | |
80 // Re-enable the network session. | |
81 profile->GetRequestContext()->http_transaction_factory()->Suspend(false); | |
82 } | |
83 | |
OLD | NEW |