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

Side by Side Diff: android_webview/native/aw_contents_io_thread_client.cc

Issue 10702083: Add ContentViewDelegate (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix nits Created 8 years, 3 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) 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 base::Lock g_map_lock;
joth 2012/09/13 20:09:12 you still have a global in here! Suggest wrapping
mkosiba (inactive) 2012/09/17 17:48:22 Done.
45 LazyInstance<RenderViewHostToWeakDelegateMapType> g_rvh_to_weak_delegate_map =
46 LAZY_INSTANCE_INITIALIZER;
47
48 static pair<int, int> GetRenderViewHostIdPair(RenderViewHost* rvh) {
49 return pair<int, int>(rvh->GetProcess()->GetID(), rvh->GetRoutingID());
50 }
51
52 // JavaClientMapMaintainer ----------------------------------------------------
joth 2012/09/13 20:09:12 nit: FooMaintainer sounds like something I'd expec
mkosiba (inactive) 2012/09/17 17:48:22 Done.
53
54 class JavaClientMapMaintainer : public content::WebContentsObserver {
55 public:
56 JavaClientMapMaintainer(JNIEnv* env, WebContents* web_contents,
57 jobject jdelegate);
58
59 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE;
60 virtual void RenderViewForInterstitialPageCreated(
61 RenderViewHost* render_view_host) OVERRIDE;
62 virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE;
63 virtual void WebContentsDestroyed(WebContents* web_contents);
64
65 private:
66 JavaObjectWeakGlobalRef jdelegate_;
67 };
68
69 JavaClientMapMaintainer::JavaClientMapMaintainer(
70 JNIEnv* env, WebContents* web_contents, jobject jdelegate)
joth 2012/09/13 20:09:12 one param per line. (and in the class declaration)
mkosiba (inactive) 2012/09/17 17:48:22 Done.
71 : content::WebContentsObserver(web_contents),
72 jdelegate_(env, jdelegate) {
73 DCHECK(web_contents);
74 DCHECK(jdelegate);
75
76 if (web_contents->GetRenderViewHost())
77 RenderViewCreated(web_contents->GetRenderViewHost());
78 }
79
80 void JavaClientMapMaintainer::RenderViewCreated(RenderViewHost* rvh) {
81 pair<int, int> rvh_id = GetRenderViewHostIdPair(rvh);
82 base::AutoLock lock(g_map_lock);
83 g_rvh_to_weak_delegate_map.Get()[rvh_id] = jdelegate_;
84 }
85
86 void JavaClientMapMaintainer::RenderViewForInterstitialPageCreated(
87 RenderViewHost* rvh) {
88 RenderViewCreated(rvh);
89 }
90
91 void JavaClientMapMaintainer::RenderViewDeleted(RenderViewHost* rvh) {
92 pair<int, int> rvh_id = GetRenderViewHostIdPair(rvh);
93 base::AutoLock lock(g_map_lock);
94 g_rvh_to_weak_delegate_map.Get().erase(rvh_id);
95 }
96
97 void JavaClientMapMaintainer::WebContentsDestroyed(
98 WebContents* web_contents) {
99
100 if (web_contents->GetRenderViewHost())
101 RenderViewDeleted(web_contents->GetRenderViewHost());
102
103 delete this;
104 }
105
106 } // namespace
107
108 // AwContentsIoThreadClient ----------------------------------------------
109
110 // JNI Methods ----------------------------------------------------------------
joth 2012/09/13 20:09:12 nit: If you're splitting the file by class name I
mkosiba (inactive) 2012/09/17 17:48:22 Done.
111
112 bool AwContentsIoThreadClient::RegisterAwContentsIoThreadClient(
113 JNIEnv* env) {
114 return RegisterNativesImpl(env);
115 }
116
117 // Static methods -------------------------------------------------------------
118
119 void AwContentsIoThreadClient::Associate(
120 WebContents* web_contents, const JavaRef<jobject>& jclient) {
121 JNIEnv* env = AttachCurrentThread();
122 // The JavaClientMapMaintainer lifespan is tied to the WebContents.
123 new JavaClientMapMaintainer(env, web_contents, jclient.obj());
124 }
125
126 AwContentsIoThreadClient
127 AwContentsIoThreadClient::FromID(int render_process_id, int render_view_id) {
128 pair<int, int> rvh_id(render_process_id, render_view_id);
129 base::AutoLock lock(g_map_lock);
130 RenderViewHostToWeakDelegateMapType::iterator weak_delegate_iterator =
131 g_rvh_to_weak_delegate_map.Get().find(rvh_id);
132 if (weak_delegate_iterator == g_rvh_to_weak_delegate_map.Get().end())
133 return AwContentsIoThreadClient();
134
135 JNIEnv* env = AttachCurrentThread();
136 ScopedJavaLocalRef<jobject> java_delegate =
137 weak_delegate_iterator->second.get(env);
138 if (java_delegate.is_null())
139 return AwContentsIoThreadClient();
140
141 return AwContentsIoThreadClient(java_delegate);
142 }
143
144 // regular methods ------------------------------------------------------------
145 AwContentsIoThreadClient::AwContentsIoThreadClient() {
146 }
147
148 AwContentsIoThreadClient::AwContentsIoThreadClient(const JavaRef<jobject>& obj)
149 : java_object_(obj) {
150 }
151
152 AwContentsIoThreadClient::AwContentsIoThreadClient(
153 const AwContentsIoThreadClient& orig)
154 : java_object_(orig.java_object_) {
155 }
156
157 void AwContentsIoThreadClient::operator=(const AwContentsIoThreadClient& rhs) {
158 java_object_.Reset(rhs.java_object_);
159 }
160
161 AwContentsIoThreadClient::~AwContentsIoThreadClient() {
162 }
163
164 scoped_ptr<InterceptedRequestData>
165 AwContentsIoThreadClient::ShouldInterceptRequest(
166 const net::URLRequest* request) {
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
168 if (java_object_.is_null())
169 return scoped_ptr<InterceptedRequestData>();
170
171 JNIEnv* env = AttachCurrentThread();
172 ScopedJavaLocalRef<jstring> jstring_url =
173 ConvertUTF8ToJavaString(env, request->url().spec());
174 ScopedJavaLocalRef<jobject> ret =
175 Java_AwContentsIoThreadClient_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 }
182
183 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698