Chromium Code Reviews| 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 class is only accessed from the UI thread. |
| 37 base::LazyInstance<std::vector<int /* process id */> > g_suspended_processes = | 38 class SuspendedProcessWatcher : public content::RenderProcessHostObserver { |
| 39 public: | |
| 40 | |
| 41 // If the process crashes, stop watching the corresponding RenderProcessHost | |
| 42 // and ensure it doesn't get over-resumed. | |
| 43 virtual void RenderProcessExited(content::RenderProcessHost* host, | |
| 44 base::ProcessHandle handle, | |
| 45 base::TerminationStatus status, | |
| 46 int exit_code) OVERRIDE { | |
| 47 std::vector<int>::iterator pos = std::find(suspended_processes_.begin(), | |
| 48 suspended_processes_.end(), | |
| 49 host->GetID()); | |
| 50 DCHECK_NE(pos, suspended_processes_.end()); | |
| 51 host->RemoveObserver(this); | |
| 52 suspended_processes_.erase(pos); | |
| 53 } | |
| 54 | |
| 55 // Suspends timers in all current render processes. | |
| 56 void SuspendWebKitSharedTimers() { | |
| 57 DCHECK(suspended_processes_.empty()); | |
| 58 | |
| 59 for (content::RenderProcessHost::iterator i( | |
| 60 content::RenderProcessHost::AllHostsIterator()); | |
| 61 !i.IsAtEnd(); i.Advance()) { | |
| 62 content::RenderProcessHost* host = i.GetCurrentValue(); | |
| 63 host->AddObserver(this); | |
|
klobag.chromium
2014/03/11 00:36:19
should we only do so if host has connection?
Othe
Yaron
2014/03/11 00:41:24
Done.
| |
| 64 host->Send(new ViewMsg_SetWebKitSharedTimersSuspended(true)); | |
| 65 suspended_processes_.push_back(host->GetID()); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 // Resumes timers in processes that were previously stopped. | |
| 70 void ResumeWebkitSharedTimers() { | |
| 71 for (std::vector<int>::const_iterator it = suspended_processes_.begin(); | |
| 72 it != suspended_processes_.end(); ++it) { | |
| 73 content::RenderProcessHost* host = | |
| 74 content::RenderProcessHost::FromID(*it); | |
| 75 DCHECK(host); | |
| 76 host->RemoveObserver(this); | |
| 77 host->Send(new ViewMsg_SetWebKitSharedTimersSuspended(false)); | |
| 78 } | |
| 79 suspended_processes_.clear(); | |
| 80 } | |
| 81 | |
| 82 private: | |
| 83 std::vector<int /* RenderProcessHost id */> suspended_processes_; | |
| 84 }; | |
| 85 | |
| 86 base::LazyInstance<SuspendedProcessWatcher> g_suspended_processes_watcher = | |
| 38 LAZY_INSTANCE_INITIALIZER; | 87 LAZY_INSTANCE_INITIALIZER; |
| 39 | 88 |
| 40 // Suspends timers in all current render processes. | |
| 41 void SuspendWebKitSharedTimers(std::vector<int>* suspended_processes) { | |
| 42 for (content::RenderProcessHost::iterator i( | |
| 43 content::RenderProcessHost::AllHostsIterator()); | |
| 44 !i.IsAtEnd(); i.Advance()) { | |
| 45 content::RenderProcessHost* host = i.GetCurrentValue(); | |
| 46 suspended_processes->push_back(host->GetID()); | |
| 47 host->Send(new ViewMsg_SetWebKitSharedTimersSuspended(true)); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 // Resumes timers in processes that were previously stopped. | |
| 52 void ResumeWebkitSharedTimers(const std::vector<int>& suspended_processes) { | |
| 53 for (std::vector<int>::const_iterator it = suspended_processes.begin(); | |
| 54 it != suspended_processes.end(); ++it) { | |
| 55 content::RenderProcessHost* host = content::RenderProcessHost::FromID(*it); | |
| 56 if (host) // The process might have been killed since it was suspended. | |
| 57 host->Send(new ViewMsg_SetWebKitSharedTimersSuspended(false)); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 } // namespace | 89 } // namespace |
| 62 | 90 |
| 63 // Returns the first substring consisting of the address of a physical location. | 91 // Returns the first substring consisting of the address of a physical location. |
| 64 static jstring FindAddress(JNIEnv* env, jclass clazz, jstring addr) { | 92 static jstring FindAddress(JNIEnv* env, jclass clazz, jstring addr) { |
| 65 base::string16 content_16 = ConvertJavaStringToUTF16(env, addr); | 93 base::string16 content_16 = ConvertJavaStringToUTF16(env, addr); |
| 66 base::string16 result_16; | 94 base::string16 result_16; |
| 67 if (content::address_parser::FindAddress(content_16, &result_16)) | 95 if (content::address_parser::FindAddress(content_16, &result_16)) |
| 68 return ConvertUTF16ToJavaString(env, result_16).Release(); | 96 return ConvertUTF16ToJavaString(env, result_16).Release(); |
| 69 return NULL; | 97 return NULL; |
| 70 } | 98 } |
| 71 | 99 |
| 72 static void SetWebKitSharedTimersSuspended(JNIEnv* env, | 100 static void SetWebKitSharedTimersSuspended(JNIEnv* env, |
| 73 jclass obj, | 101 jclass obj, |
| 74 jboolean suspend) { | 102 jboolean suspend) { |
| 75 std::vector<int>* suspended_processes = g_suspended_processes.Pointer(); | |
| 76 if (suspend) { | 103 if (suspend) { |
| 77 DCHECK(suspended_processes->empty()); | 104 g_suspended_processes_watcher.Pointer()->SuspendWebKitSharedTimers(); |
| 78 SuspendWebKitSharedTimers(suspended_processes); | |
| 79 } else { | 105 } else { |
| 80 ResumeWebkitSharedTimers(*suspended_processes); | 106 g_suspended_processes_watcher.Pointer()->ResumeWebkitSharedTimers(); |
| 81 suspended_processes->clear(); | |
| 82 } | 107 } |
| 83 } | 108 } |
| 84 | 109 |
| 85 namespace content { | 110 namespace content { |
| 86 | 111 |
| 87 bool RegisterWebViewStatics(JNIEnv* env) { | 112 bool RegisterWebViewStatics(JNIEnv* env) { |
| 88 return RegisterNativesImpl(env) >= 0; | 113 return RegisterNativesImpl(env); |
| 89 } | 114 } |
| 90 | 115 |
| 91 } // namespace content | 116 } // namespace content |
| OLD | NEW |