Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / android / content_view_statics.cc
blobb6cfde1994e00aef70b738de245456fa7f901d49
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
3 // found in the LICENSE file.
5 #include <jni.h>
6 #include <vector>
8 #include "base/android/jni_android.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/basictypes.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "content/browser/android/content_view_statics.h"
15 #include "content/common/android/address_parser.h"
16 #include "content/common/view_messages.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/render_process_host_observer.h"
19 #include "jni/ContentViewStatics_jni.h"
21 using base::android::ConvertJavaStringToUTF16;
22 using base::android::ConvertUTF16ToJavaString;
24 namespace {
26 // TODO(pliard): http://crbug.com/235909. Move WebKit shared timer toggling
27 // functionality out of ContentViewStatistics and not be build on top of
28 // blink::Platform::SuspendSharedTimer.
29 // TODO(pliard): http://crbug.com/235912. Add unit tests for WebKit shared timer
30 // toggling.
32 // This tracks the renderer processes that received a suspend request. It's
33 // important on resume to only resume the renderer processes that were actually
34 // suspended as opposed to all the current renderer processes because the
35 // suspend calls are refcounted within WebKitPlatformSupport and it expects a
36 // perfectly matched number of resume calls.
37 // Note that this class is only accessed from the UI thread.
38 class SuspendedProcessWatcher : public content::RenderProcessHostObserver {
39 public:
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 StopWatching(host);
50 virtual void RenderProcessHostDestroyed(
51 content::RenderProcessHost* host) OVERRIDE {
52 StopWatching(host);
55 // Suspends timers in all current render processes.
56 void SuspendWebKitSharedTimers() {
57 DCHECK(suspended_processes_.empty());
59 for (content::RenderProcessHost::iterator i(
60 content::RenderProcessHost::AllHostsIterator());
61 !i.IsAtEnd(); i.Advance()) {
62 content::RenderProcessHost* host = i.GetCurrentValue();
63 if (host->HasConnection()) {
64 host->AddObserver(this);
65 host->Send(new ViewMsg_SetWebKitSharedTimersSuspended(true));
66 suspended_processes_.push_back(host->GetID());
71 // Resumes timers in processes that were previously stopped.
72 void ResumeWebkitSharedTimers() {
73 for (std::vector<int>::const_iterator it = suspended_processes_.begin();
74 it != suspended_processes_.end(); ++it) {
75 content::RenderProcessHost* host =
76 content::RenderProcessHost::FromID(*it);
77 DCHECK(host);
78 host->RemoveObserver(this);
79 host->Send(new ViewMsg_SetWebKitSharedTimersSuspended(false));
81 suspended_processes_.clear();
84 private:
85 void StopWatching(content::RenderProcessHost* host) {
86 std::vector<int>::iterator pos = std::find(suspended_processes_.begin(),
87 suspended_processes_.end(),
88 host->GetID());
89 DCHECK_NE(pos, suspended_processes_.end());
90 host->RemoveObserver(this);
91 suspended_processes_.erase(pos);
94 std::vector<int /* RenderProcessHost id */> suspended_processes_;
97 base::LazyInstance<SuspendedProcessWatcher> g_suspended_processes_watcher =
98 LAZY_INSTANCE_INITIALIZER;
100 } // namespace
102 // Returns the first substring consisting of the address of a physical location.
103 static jstring FindAddress(JNIEnv* env, jclass clazz, jstring addr) {
104 base::string16 content_16 = ConvertJavaStringToUTF16(env, addr);
105 base::string16 result_16;
106 if (content::address_parser::FindAddress(content_16, &result_16))
107 return ConvertUTF16ToJavaString(env, result_16).Release();
108 return NULL;
111 static void SetWebKitSharedTimersSuspended(JNIEnv* env,
112 jclass obj,
113 jboolean suspend) {
114 if (suspend) {
115 g_suspended_processes_watcher.Pointer()->SuspendWebKitSharedTimers();
116 } else {
117 g_suspended_processes_watcher.Pointer()->ResumeWebkitSharedTimers();
121 namespace content {
123 bool RegisterWebViewStatics(JNIEnv* env) {
124 return RegisterNativesImpl(env);
127 } // namespace content