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 <jni.h> | 5 #include <jni.h> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "content/browser/android/content_view_statics.h" | 14 #include "content/browser/android/content_view_statics.h" |
15 #include "content/common/android/address_parser.h" | 15 #include "content/common/android/address_parser.h" |
16 #include "content/common/view_messages.h" | 16 #include "content/common/view_messages.h" |
17 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
18 #include "content/public/browser/render_process_host_observer.h" | |
18 #include "jni/ContentViewStatics_jni.h" | 19 #include "jni/ContentViewStatics_jni.h" |
19 | 20 |
20 using base::android::ConvertJavaStringToUTF16; | 21 using base::android::ConvertJavaStringToUTF16; |
21 using base::android::ConvertUTF16ToJavaString; | 22 using base::android::ConvertUTF16ToJavaString; |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // TODO(pliard): http://crbug.com/235909. Move WebKit shared timer toggling | 26 // TODO(pliard): http://crbug.com/235909. Move WebKit shared timer toggling |
26 // functionality out of ContentViewStatistics and not be build on top of | 27 // functionality out of ContentViewStatistics and not be build on top of |
27 // blink::Platform::SuspendSharedTimer. | 28 // blink::Platform::SuspendSharedTimer. |
28 // TODO(pliard): http://crbug.com/235912. Add unit tests for WebKit shared timer | 29 // TODO(pliard): http://crbug.com/235912. Add unit tests for WebKit shared timer |
29 // toggling. | 30 // toggling. |
30 | 31 |
31 // This tracks the renderer processes that received a suspend request. It's | 32 // This tracks the renderer processes that received a suspend request. It's |
32 // important on resume to only resume the renderer processes that were actually | 33 // important on resume to only resume the renderer processes that were actually |
33 // suspended as opposed to all the current renderer processes because the | 34 // suspended as opposed to all the current renderer processes because the |
34 // suspend calls are refcounted within WebKitPlatformSupport and it expects a | 35 // suspend calls are refcounted within WebKitPlatformSupport and it expects a |
35 // perfectly matched number of resume calls. | 36 // perfectly matched number of resume calls. |
36 // Note that this vector is only accessed from the UI thread. | 37 // Note that this vector is only accessed from the UI thread. |
37 base::LazyInstance<std::vector<int /* process id */> > g_suspended_processes = | 38 base::LazyInstance<std::vector<int /* RenderProcessHost id */> > |
39 g_suspended_processes = LAZY_INSTANCE_INITIALIZER; | |
40 | |
41 // Listens for RenderProcessHosts whose renderers have crashed. This is | |
42 // necessary to ensure that we don't over-resume a RenderProcess as described | |
43 // above. | |
44 class SuspendedProcessWatcher : public content::RenderProcessHostObserver { | |
45 virtual void RenderProcessExited(content::RenderProcessHost* host, | |
46 base::ProcessHandle handle, | |
47 base::TerminationStatus status, | |
48 int exit_code) OVERRIDE { | |
49 std::vector<int>* suspended_processes = g_suspended_processes.Pointer(); | |
50 std::vector<int>::iterator pos = std::find(suspended_processes->begin(), | |
51 suspended_processes->end(), | |
52 host->GetID()); | |
53 if (pos != suspended_processes->end()) { | |
54 host->RemoveObserver(this); | |
55 suspended_processes->erase(pos); | |
56 } | |
57 } | |
58 }; | |
59 | |
60 base::LazyInstance<SuspendedProcessWatcher> g_suspended_processes_watcher = | |
38 LAZY_INSTANCE_INITIALIZER; | 61 LAZY_INSTANCE_INITIALIZER; |
39 | 62 |
40 // Suspends timers in all current render processes. | 63 // Suspends timers in all current render processes. |
41 void SuspendWebKitSharedTimers(std::vector<int>* suspended_processes) { | 64 void SuspendWebKitSharedTimers(std::vector<int>* suspended_processes) { |
42 for (content::RenderProcessHost::iterator i( | 65 for (content::RenderProcessHost::iterator i( |
43 content::RenderProcessHost::AllHostsIterator()); | 66 content::RenderProcessHost::AllHostsIterator()); |
44 !i.IsAtEnd(); i.Advance()) { | 67 !i.IsAtEnd(); i.Advance()) { |
45 content::RenderProcessHost* host = i.GetCurrentValue(); | 68 content::RenderProcessHost* host = i.GetCurrentValue(); |
46 suspended_processes->push_back(host->GetID()); | 69 suspended_processes->push_back(host->GetID()); |
47 host->Send(new ViewMsg_SetWebKitSharedTimersSuspended(true)); | 70 host->Send(new ViewMsg_SetWebKitSharedTimersSuspended(true)); |
71 host->AddObserver(g_suspended_processes_watcher.Pointer()); | |
klobag.chromium
2014/02/25 22:19:27
Hmm, are we calling this every time in suspend? Ar
Yaron
2014/03/08 00:56:02
Done.
| |
48 } | 72 } |
49 } | 73 } |
50 | 74 |
51 // Resumes timers in processes that were previously stopped. | 75 // Resumes timers in processes that were previously stopped. |
52 void ResumeWebkitSharedTimers(const std::vector<int>& suspended_processes) { | 76 void ResumeWebkitSharedTimers(const std::vector<int>& suspended_processes) { |
53 for (std::vector<int>::const_iterator it = suspended_processes.begin(); | 77 for (std::vector<int>::const_iterator it = suspended_processes.begin(); |
54 it != suspended_processes.end(); ++it) { | 78 it != suspended_processes.end(); ++it) { |
55 content::RenderProcessHost* host = content::RenderProcessHost::FromID(*it); | 79 content::RenderProcessHost* host = content::RenderProcessHost::FromID(*it); |
56 if (host) // The process might have been killed since it was suspended. | 80 if (host) // The process might have been killed since it was suspended. |
klobag.chromium
2014/02/25 22:19:27
Should we assert host not null here?
Yaron
2014/03/08 00:56:02
Done.
| |
57 host->Send(new ViewMsg_SetWebKitSharedTimersSuspended(false)); | 81 host->Send(new ViewMsg_SetWebKitSharedTimersSuspended(false)); |
58 } | 82 } |
59 } | 83 } |
60 | 84 |
61 } // namespace | 85 } // namespace |
62 | 86 |
63 // Returns the first substring consisting of the address of a physical location. | 87 // Returns the first substring consisting of the address of a physical location. |
64 static jstring FindAddress(JNIEnv* env, jclass clazz, jstring addr) { | 88 static jstring FindAddress(JNIEnv* env, jclass clazz, jstring addr) { |
65 base::string16 content_16 = ConvertJavaStringToUTF16(env, addr); | 89 base::string16 content_16 = ConvertJavaStringToUTF16(env, addr); |
66 base::string16 result_16; | 90 base::string16 result_16; |
(...skipping 15 matching lines...) Expand all Loading... | |
82 } | 106 } |
83 } | 107 } |
84 | 108 |
85 namespace content { | 109 namespace content { |
86 | 110 |
87 bool RegisterWebViewStatics(JNIEnv* env) { | 111 bool RegisterWebViewStatics(JNIEnv* env) { |
88 return RegisterNativesImpl(env) >= 0; | 112 return RegisterNativesImpl(env) >= 0; |
89 } | 113 } |
90 | 114 |
91 } // namespace content | 115 } // namespace content |
OLD | NEW |