Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "android_webview/native/aw_contents_io_thread_delegate.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "android_webview/native/intercepted_request_data.h" | |
| 11 #include "base/android/jni_helper.h" | |
| 12 #include "base/android/jni_string.h" | |
| 13 #include "base/hash_tables.h" | |
| 14 #include "base/memory/linked_ptr.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/synchronization/lock.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "content/public/browser/render_view_host.h" | |
| 19 #include "content/public/browser/render_process_host.h" | |
| 20 #include "content/public/browser/web_contents.h" | |
| 21 #include "content/public/browser/web_contents_observer.h" | |
| 22 #include "net/url_request/url_request.h" | |
| 23 | |
| 24 #include "jni/AwWebContentsIoThreadDelegate_jni.h" | |
| 25 | |
| 26 using namespace base::android; | |
| 27 using content::BrowserThread; | |
| 28 using content::RenderViewHost; | |
| 29 using content::WebContents; | |
| 30 using std::map; | |
| 31 using std::pair; | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 typedef map<pair<int, int>, JavaObjectWeakGlobalRef> | |
| 36 RenderViewHostToWeakDelegateMapType; | |
| 37 | |
| 38 base::Lock g_map_lock; | |
| 39 RenderViewHostToWeakDelegateMapType g_rvh_to_weak_delegate_map; | |
|
joth
2012/09/11 02:06:50
these still need to be lazy instance-ified.
mkosiba (inactive)
2012/09/13 01:48:26
Done.
| |
| 40 | |
| 41 static pair<int, int> GetRenderViewHostIdPair(RenderViewHost* rvh) { | |
| 42 return pair<int, int>(rvh->GetProcess()->GetID(), rvh->GetRoutingID()); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 // JavaDelegateMapMaintainer -------------------------------------------------- | |
| 48 | |
| 49 class JavaDelegateMapMaintainer : public content::WebContentsObserver { | |
| 50 public: | |
| 51 JavaDelegateMapMaintainer(JNIEnv* env, jobject jdelegate, | |
| 52 WebContents* web_contents); | |
| 53 | |
| 54 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE; | |
| 55 virtual void RenderViewForInterstitialPageCreated( | |
| 56 RenderViewHost* render_view_host) OVERRIDE; | |
| 57 virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE; | |
| 58 virtual void WebContentsDestroyed(WebContents* web_contents); | |
| 59 | |
| 60 private: | |
| 61 JavaObjectWeakGlobalRef jdelegate_; | |
| 62 }; | |
| 63 | |
| 64 JavaDelegateMapMaintainer::JavaDelegateMapMaintainer( | |
| 65 JNIEnv* env, jobject jdelegate, WebContents* web_contents) | |
| 66 : content::WebContentsObserver(web_contents), | |
| 67 jdelegate_(env, jdelegate) { | |
|
joth
2012/09/11 02:06:50
I think we can remove jdelegate_ member, and then
mkosiba (inactive)
2012/09/13 01:48:26
changed the code to not do this anymore, but in th
| |
| 68 DCHECK(web_contents); | |
| 69 DCHECK(jdelegate); | |
| 70 | |
| 71 if (web_contents->GetRenderViewHost()) { | |
| 72 pair<int, int> rvh_id = GetRenderViewHostIdPair( | |
| 73 web_contents->GetRenderViewHost()); | |
| 74 base::AutoLock lock(g_map_lock); | |
| 75 g_rvh_to_weak_delegate_map[rvh_id] = jdelegate_; | |
|
joth
2012/09/11 02:06:50
not sure why I didn't suggest this before, but rat
mkosiba (inactive)
2012/09/13 01:48:26
IMHO the lock-based approach is simpler to read.
| |
| 76 } | |
| 77 } | |
| 78 | |
| 79 void JavaDelegateMapMaintainer::RenderViewCreated(RenderViewHost* rvh) { | |
| 80 pair<int, int> rvh_id = GetRenderViewHostIdPair(rvh); | |
| 81 base::AutoLock lock(g_map_lock); | |
| 82 g_rvh_to_weak_delegate_map[rvh_id] = jdelegate_; | |
| 83 } | |
| 84 | |
| 85 void JavaDelegateMapMaintainer::RenderViewForInterstitialPageCreated( | |
| 86 RenderViewHost* rvh) { | |
| 87 RenderViewCreated(rvh); | |
| 88 } | |
| 89 | |
| 90 void JavaDelegateMapMaintainer::RenderViewDeleted(RenderViewHost* rvh) { | |
| 91 pair<int, int> rvh_id = GetRenderViewHostIdPair(rvh); | |
| 92 base::AutoLock lock(g_map_lock); | |
| 93 g_rvh_to_weak_delegate_map.erase(rvh_id); | |
| 94 } | |
| 95 | |
| 96 void JavaDelegateMapMaintainer::WebContentsDestroyed( | |
| 97 WebContents* web_contents) { | |
| 98 | |
| 99 if (web_contents->GetRenderViewHost()) | |
| 100 RenderViewDeleted(web_contents->GetRenderViewHost()); | |
| 101 | |
| 102 JNIEnv* env = AttachCurrentThread(); | |
| 103 ScopedJavaLocalRef<jobject> java_delegate = jdelegate_.get(env); | |
| 104 if (java_delegate.obj()) { | |
| 105 AwWebContentsIoThreadDelegate io_thread_delegate(java_delegate); | |
| 106 io_thread_delegate.OnWebContentsDestroyed(); | |
|
joth
2012/09/11 02:06:50
note this is calling the "IO" thread delegate on t
mkosiba (inactive)
2012/09/13 01:48:26
method removed
| |
| 107 } | |
| 108 | |
| 109 delete this; | |
| 110 } | |
| 111 | |
| 112 // AwWebContentsIoThreadDelegate ---------------------------------------------- | |
| 113 | |
| 114 // JNI Methods ---------------------------------------------------------------- | |
| 115 | |
| 116 void Init(JNIEnv* env, jobject jdelegate, | |
| 117 jint native_web_contents) { | |
| 118 WebContents* web_contents = | |
| 119 reinterpret_cast<WebContents*>(native_web_contents); | |
| 120 // The observer's lifespan is tied to that of the WebContents. | |
| 121 new JavaDelegateMapMaintainer(env, jdelegate, web_contents); | |
| 122 } | |
| 123 | |
| 124 bool AwWebContentsIoThreadDelegate::RegisterAwWebContentsIoThreadDelegate( | |
| 125 JNIEnv* env) { | |
| 126 return RegisterNativesImpl(env); | |
| 127 } | |
| 128 | |
| 129 // Static methods ------------------------------------------------------------- | |
| 130 | |
| 131 scoped_ptr<AwWebContentsIoThreadDelegate> | |
| 132 AwWebContentsIoThreadDelegate::FromID(int render_process_id, | |
| 133 int render_view_id) { | |
| 134 pair<int, int> rvh_id(render_process_id, render_view_id); | |
| 135 base::AutoLock lock(g_map_lock); | |
| 136 RenderViewHostToWeakDelegateMapType::iterator weak_delegate_iterator = | |
| 137 g_rvh_to_weak_delegate_map.find(rvh_id); | |
| 138 if (weak_delegate_iterator == g_rvh_to_weak_delegate_map.end()) | |
| 139 return scoped_ptr<AwWebContentsIoThreadDelegate>(); | |
| 140 | |
| 141 JNIEnv* env = AttachCurrentThread(); | |
| 142 ScopedJavaLocalRef<jobject> java_delegate = | |
| 143 weak_delegate_iterator->second.get(env); | |
| 144 if (java_delegate.is_null()) | |
| 145 return scoped_ptr<AwWebContentsIoThreadDelegate>(); | |
| 146 | |
| 147 return scoped_ptr<AwWebContentsIoThreadDelegate>( | |
| 148 new AwWebContentsIoThreadDelegate(java_delegate)); | |
|
joth
2012/09/11 02:06:50
return make_scoped_ptr(new Foo);
mkosiba (inactive)
2012/09/13 01:48:26
Done.
| |
| 149 } | |
| 150 | |
| 151 // regular methods ------------------------------------------------------------ | |
| 152 | |
| 153 AwWebContentsIoThreadDelegate::AwWebContentsIoThreadDelegate( | |
| 154 const JavaRef<jobject>& obj) | |
| 155 : java_object_(obj) { | |
| 156 } | |
| 157 | |
| 158 AwWebContentsIoThreadDelegate::~AwWebContentsIoThreadDelegate() { | |
| 159 } | |
| 160 | |
| 161 void AwWebContentsIoThreadDelegate::OnWebContentsDestroyed() { | |
| 162 JNIEnv* env = AttachCurrentThread(); | |
| 163 Java_AwWebContentsIoThreadDelegate_onWebContentsDestroyed( | |
| 164 env, java_object_.obj()); | |
| 165 } | |
| 166 | |
| 167 scoped_ptr<InterceptedRequestData> | |
| 168 AwWebContentsIoThreadDelegate::ShouldInterceptRequest( | |
| 169 const net::URLRequest* request) { | |
| 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 171 JNIEnv* env = AttachCurrentThread(); | |
| 172 ScopedJavaLocalRef<jstring> jstring_url = | |
| 173 ConvertUTF8ToJavaString(env, request->url().spec()); | |
| 174 ScopedJavaLocalRef<jobject> ret = | |
| 175 Java_AwWebContentsIoThreadDelegate_shouldInterceptRequest( | |
| 176 env, java_object_.obj(), jstring_url.obj()); | |
| 177 if (ret.is_null()) | |
| 178 return scoped_ptr<InterceptedRequestData>(); | |
| 179 return scoped_ptr<InterceptedRequestData>( | |
| 180 new InterceptedRequestData(ret)); | |
| 181 } | |
| OLD | NEW |