| 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_client.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/lazy_instance.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/AwContentsIoThreadClient_jni.h" |
| 25 |
| 26 using base::android::AttachCurrentThread; |
| 27 using base::android::ConvertUTF8ToJavaString; |
| 28 using base::android::JavaRef; |
| 29 using base::android::ScopedJavaLocalRef; |
| 30 using base::LazyInstance; |
| 31 using content::BrowserThread; |
| 32 using content::RenderViewHost; |
| 33 using content::WebContents; |
| 34 using std::map; |
| 35 using std::pair; |
| 36 |
| 37 namespace android_webview { |
| 38 |
| 39 namespace { |
| 40 |
| 41 typedef map<pair<int, int>, JavaObjectWeakGlobalRef> |
| 42 RenderViewHostToWeakDelegateMapType; |
| 43 |
| 44 static pair<int, int> GetRenderViewHostIdPair(RenderViewHost* rvh) { |
| 45 return pair<int, int>(rvh->GetProcess()->GetID(), rvh->GetRoutingID()); |
| 46 } |
| 47 |
| 48 // RvhToIoThreadClientMap ----------------------------------------------------- |
| 49 class RvhToIoThreadClientMap { |
| 50 public: |
| 51 static RvhToIoThreadClientMap* GetInstance(); |
| 52 void Insert(pair<int, int> rvh_id, JavaObjectWeakGlobalRef jdelegate); |
| 53 ScopedJavaLocalRef<jobject> Get(pair<int, int> rvh_id); |
| 54 void Erase(pair<int, int> rvh_id); |
| 55 |
| 56 private: |
| 57 static LazyInstance<RvhToIoThreadClientMap> s_instance_; |
| 58 base::Lock map_lock_; |
| 59 RenderViewHostToWeakDelegateMapType rvh_to_weak_delegate_map_; |
| 60 }; |
| 61 |
| 62 // static |
| 63 LazyInstance<RvhToIoThreadClientMap> RvhToIoThreadClientMap::s_instance_ = |
| 64 LAZY_INSTANCE_INITIALIZER; |
| 65 |
| 66 // static |
| 67 RvhToIoThreadClientMap* RvhToIoThreadClientMap::GetInstance() { |
| 68 return s_instance_.Pointer(); |
| 69 } |
| 70 |
| 71 void RvhToIoThreadClientMap::Insert(pair<int, int> rvh_id, |
| 72 JavaObjectWeakGlobalRef jdelegate) { |
| 73 base::AutoLock lock(map_lock_); |
| 74 rvh_to_weak_delegate_map_[rvh_id] = jdelegate; |
| 75 } |
| 76 |
| 77 ScopedJavaLocalRef<jobject> RvhToIoThreadClientMap::Get( |
| 78 pair<int, int> rvh_id) { |
| 79 base::AutoLock lock(map_lock_); |
| 80 RenderViewHostToWeakDelegateMapType::iterator weak_delegate_iterator = |
| 81 rvh_to_weak_delegate_map_.find(rvh_id); |
| 82 if (weak_delegate_iterator == rvh_to_weak_delegate_map_.end()) |
| 83 return ScopedJavaLocalRef<jobject>(); |
| 84 |
| 85 JNIEnv* env = AttachCurrentThread(); |
| 86 return weak_delegate_iterator->second.get(env); |
| 87 } |
| 88 |
| 89 void RvhToIoThreadClientMap::Erase(pair<int, int> rvh_id) { |
| 90 base::AutoLock lock(map_lock_); |
| 91 rvh_to_weak_delegate_map_.erase(rvh_id); |
| 92 } |
| 93 |
| 94 // ClientMapEntryUpdater ------------------------------------------------------ |
| 95 |
| 96 class ClientMapEntryUpdater : public content::WebContentsObserver { |
| 97 public: |
| 98 ClientMapEntryUpdater(JNIEnv* env, WebContents* web_contents, |
| 99 jobject jdelegate); |
| 100 |
| 101 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE; |
| 102 virtual void RenderViewForInterstitialPageCreated( |
| 103 RenderViewHost* render_view_host) OVERRIDE; |
| 104 virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE; |
| 105 virtual void WebContentsDestroyed(WebContents* web_contents); |
| 106 |
| 107 private: |
| 108 JavaObjectWeakGlobalRef jdelegate_; |
| 109 }; |
| 110 |
| 111 ClientMapEntryUpdater::ClientMapEntryUpdater(JNIEnv* env, |
| 112 WebContents* web_contents, |
| 113 jobject jdelegate) |
| 114 : content::WebContentsObserver(web_contents), |
| 115 jdelegate_(env, jdelegate) { |
| 116 DCHECK(web_contents); |
| 117 DCHECK(jdelegate); |
| 118 |
| 119 if (web_contents->GetRenderViewHost()) |
| 120 RenderViewCreated(web_contents->GetRenderViewHost()); |
| 121 } |
| 122 |
| 123 void ClientMapEntryUpdater::RenderViewCreated(RenderViewHost* rvh) { |
| 124 RvhToIoThreadClientMap::GetInstance()->Insert( |
| 125 GetRenderViewHostIdPair(rvh), jdelegate_); |
| 126 } |
| 127 |
| 128 void ClientMapEntryUpdater::RenderViewForInterstitialPageCreated( |
| 129 RenderViewHost* rvh) { |
| 130 RenderViewCreated(rvh); |
| 131 } |
| 132 |
| 133 void ClientMapEntryUpdater::RenderViewDeleted(RenderViewHost* rvh) { |
| 134 RvhToIoThreadClientMap::GetInstance()->Erase(GetRenderViewHostIdPair(rvh)); |
| 135 } |
| 136 |
| 137 void ClientMapEntryUpdater::WebContentsDestroyed(WebContents* web_contents) { |
| 138 if (web_contents->GetRenderViewHost()) |
| 139 RenderViewDeleted(web_contents->GetRenderViewHost()); |
| 140 delete this; |
| 141 } |
| 142 |
| 143 } // namespace |
| 144 |
| 145 // AwContentsIoThreadClient --------------------------------------------------- |
| 146 |
| 147 // static |
| 148 void AwContentsIoThreadClient::Associate( |
| 149 WebContents* web_contents, |
| 150 const JavaRef<jobject>& jclient) { |
| 151 JNIEnv* env = AttachCurrentThread(); |
| 152 // The ClientMapEntryUpdater lifespan is tied to the WebContents. |
| 153 new ClientMapEntryUpdater(env, web_contents, jclient.obj()); |
| 154 } |
| 155 |
| 156 // static |
| 157 AwContentsIoThreadClient |
| 158 AwContentsIoThreadClient::FromID(int render_process_id, int render_view_id) { |
| 159 pair<int, int> rvh_id(render_process_id, render_view_id); |
| 160 ScopedJavaLocalRef<jobject> java_delegate = |
| 161 RvhToIoThreadClientMap::GetInstance()->Get(rvh_id); |
| 162 if (java_delegate.is_null()) |
| 163 return AwContentsIoThreadClient(); |
| 164 |
| 165 return AwContentsIoThreadClient(java_delegate); |
| 166 } |
| 167 |
| 168 AwContentsIoThreadClient::AwContentsIoThreadClient() { |
| 169 } |
| 170 |
| 171 AwContentsIoThreadClient::AwContentsIoThreadClient(const JavaRef<jobject>& obj) |
| 172 : java_object_(obj) { |
| 173 } |
| 174 |
| 175 AwContentsIoThreadClient::AwContentsIoThreadClient( |
| 176 const AwContentsIoThreadClient& orig) |
| 177 : java_object_(orig.java_object_) { |
| 178 } |
| 179 |
| 180 void AwContentsIoThreadClient::operator=(const AwContentsIoThreadClient& rhs) { |
| 181 java_object_.Reset(rhs.java_object_); |
| 182 } |
| 183 |
| 184 AwContentsIoThreadClient::~AwContentsIoThreadClient() { |
| 185 } |
| 186 |
| 187 scoped_ptr<InterceptedRequestData> |
| 188 AwContentsIoThreadClient::ShouldInterceptRequest( |
| 189 const net::URLRequest* request) { |
| 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 191 if (java_object_.is_null()) |
| 192 return scoped_ptr<InterceptedRequestData>(); |
| 193 |
| 194 JNIEnv* env = AttachCurrentThread(); |
| 195 ScopedJavaLocalRef<jstring> jstring_url = |
| 196 ConvertUTF8ToJavaString(env, request->url().spec()); |
| 197 ScopedJavaLocalRef<jobject> ret = |
| 198 Java_AwContentsIoThreadClient_shouldInterceptRequest( |
| 199 env, java_object_.obj(), jstring_url.obj()); |
| 200 if (ret.is_null()) |
| 201 return scoped_ptr<InterceptedRequestData>(); |
| 202 return scoped_ptr<InterceptedRequestData>( |
| 203 new InterceptedRequestData(ret)); |
| 204 } |
| 205 |
| 206 bool RegisterAwContentsIoThreadClient(JNIEnv* env) { |
| 207 return RegisterNativesImpl(env); |
| 208 } |
| 209 |
| 210 } // namespace android_webview |
| OLD | NEW |