OLD | NEW |
| (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 "android_webview/browser/aw_contents_client_bridge_base.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "content/public/browser/browser_thread.h" | |
9 #include "content/public/browser/render_frame_host.h" | |
10 #include "content/public/browser/web_contents.h" | |
11 | |
12 using content::BrowserThread; | |
13 using content::WebContents; | |
14 | |
15 namespace android_webview { | |
16 | |
17 namespace { | |
18 | |
19 const void* const kAwContentsClientBridgeBase = &kAwContentsClientBridgeBase; | |
20 | |
21 // This class is invented so that the UserData registry that we inject the | |
22 // AwContentsClientBridgeBase object does not own and destroy it. | |
23 class UserData : public base::SupportsUserData::Data { | |
24 public: | |
25 static AwContentsClientBridgeBase* GetContents( | |
26 content::WebContents* web_contents) { | |
27 if (!web_contents) | |
28 return NULL; | |
29 UserData* data = static_cast<UserData*>( | |
30 web_contents->GetUserData(kAwContentsClientBridgeBase)); | |
31 return data ? data->contents_ : NULL; | |
32 } | |
33 | |
34 explicit UserData(AwContentsClientBridgeBase* ptr) : contents_(ptr) {} | |
35 private: | |
36 AwContentsClientBridgeBase* contents_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(UserData); | |
39 }; | |
40 | |
41 } // namespace | |
42 | |
43 // static | |
44 void AwContentsClientBridgeBase::Associate( | |
45 WebContents* web_contents, | |
46 AwContentsClientBridgeBase* handler) { | |
47 web_contents->SetUserData(kAwContentsClientBridgeBase, | |
48 base::MakeUnique<UserData>(handler)); | |
49 } | |
50 | |
51 // static | |
52 AwContentsClientBridgeBase* AwContentsClientBridgeBase::FromWebContents( | |
53 WebContents* web_contents) { | |
54 return UserData::GetContents(web_contents); | |
55 } | |
56 | |
57 // static | |
58 AwContentsClientBridgeBase* AwContentsClientBridgeBase::FromWebContentsGetter( | |
59 const content::ResourceRequestInfo::WebContentsGetter& | |
60 web_contents_getter) { | |
61 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
62 WebContents* web_contents = web_contents_getter.Run(); | |
63 return UserData::GetContents(web_contents); | |
64 } | |
65 | |
66 // static | |
67 AwContentsClientBridgeBase* AwContentsClientBridgeBase::FromID( | |
68 int render_process_id, | |
69 int render_frame_id) { | |
70 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
71 content::RenderFrameHost* rfh = | |
72 content::RenderFrameHost::FromID(render_process_id, render_frame_id); | |
73 content::WebContents* web_contents = | |
74 content::WebContents::FromRenderFrameHost(rfh); | |
75 return UserData::GetContents(web_contents); | |
76 } | |
77 | |
78 AwContentsClientBridgeBase::~AwContentsClientBridgeBase() { | |
79 } | |
80 | |
81 } // namespace android_webview | |
OLD | NEW |