Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(480)

Side by Side Diff: content/browser/renderer_host/java/jni_helper.cc

Issue 23835020: Android: cleanup jni_android to minimize external dependencies. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 "content/browser/renderer_host/java/jni_helper.h"
6
7 #include <map>
8
9 #include "base/android/jni_android.h"
10 #include "base/lazy_instance.h"
11 #include "base/threading/platform_thread.h"
12
13 namespace {
14
15 struct MethodIdentifier {
16 const char* class_name;
17 const char* method;
18 const char* jni_signature;
19
20 bool operator<(const MethodIdentifier& other) const {
21 int r = strcmp(class_name, other.class_name);
22 if (r < 0) {
23 return true;
24 } else if (r > 0) {
25 return false;
26 }
27
28 r = strcmp(method, other.method);
29 if (r < 0) {
30 return true;
31 } else if (r > 0) {
32 return false;
33 }
34
35 return strcmp(jni_signature, other.jni_signature) < 0;
36 }
37 };
38
39 typedef std::map<MethodIdentifier, jmethodID> MethodIDMap;
40
41 const base::subtle::AtomicWord kUnlocked = 0;
42 const base::subtle::AtomicWord kLocked = 1;
43 base::subtle::AtomicWord g_method_id_map_lock = kUnlocked;
44
45 base::LazyInstance<MethodIDMap> g_method_id_map = LAZY_INSTANCE_INITIALIZER;
46
47 } // namespace
48
49 jmethodID GetMethodIDFromClassName(JNIEnv* env,
50 const char* class_name,
51 const char* method,
52 const char* jni_signature) {
53 MethodIdentifier key;
54 key.class_name = class_name;
55 key.method = method;
56 key.jni_signature = jni_signature;
57
58 MethodIDMap* map = g_method_id_map.Pointer();
59 bool found = false;
60
61 while (base::subtle::Acquire_CompareAndSwap(&g_method_id_map_lock,
62 kUnlocked,
63 kLocked) != kUnlocked) {
64 base::PlatformThread::YieldCurrentThread();
65 }
66 MethodIDMap::const_iterator iter = map->find(key);
67 if (iter != map->end()) {
68 found = true;
69 }
70 base::subtle::Release_Store(&g_method_id_map_lock, kUnlocked);
71
72 // Addition to the map does not invalidate this iterator.
73 if (found) {
74 return iter->second;
75 }
76
77 base::android::ScopedJavaLocalRef<jclass> clazz(
78 env, env->FindClass(class_name));
79 jmethodID id = base::android::MethodID::Get<
80 base::android::MethodID::TYPE_INSTANCE>(
81 env, clazz.obj(), method, jni_signature);
82
83 while (base::subtle::Acquire_CompareAndSwap(&g_method_id_map_lock,
84 kUnlocked,
85 kLocked) != kUnlocked) {
86 base::PlatformThread::YieldCurrentThread();
87 }
88 // Another thread may have populated the map already.
89 std::pair<MethodIDMap::const_iterator, bool> result =
90 map->insert(std::make_pair(key, id));
91 DCHECK_EQ(id, result.first->second);
92 base::subtle::Release_Store(&g_method_id_map_lock, kUnlocked);
93
94 return id;
95 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698