Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / android / xwalk_contents_client_bridge_base.cc
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "xwalk/runtime/browser/android/xwalk_contents_client_bridge_base.h"
7
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/browser/web_contents.h"
11
12 using content::BrowserThread;
13 using content::WebContents;
14
15 namespace xwalk {
16
17 namespace {
18
19 const void* kXWalkContentsClientBridgeBase = &kXWalkContentsClientBridgeBase;
20
21 // This class is invented so that the UserData registry that we inject the
22 // XWalkContentsClientBridgeBase object does not own and destroy it.
23 class UserData : public base::SupportsUserData::Data {
24  public:
25   static XWalkContentsClientBridgeBase* GetContents(
26       content::WebContents* web_contents) {
27     if (!web_contents)
28       return NULL;
29     UserData* data = reinterpret_cast<UserData*>(
30         web_contents->GetUserData(kXWalkContentsClientBridgeBase));
31     return data ? data->contents_ : NULL;
32   }
33
34   explicit UserData(XWalkContentsClientBridgeBase* ptr) : contents_(ptr) {}
35  private:
36   XWalkContentsClientBridgeBase* contents_;
37
38   DISALLOW_COPY_AND_ASSIGN(UserData);
39 };
40
41 }  // namespace
42
43 // static
44 void XWalkContentsClientBridgeBase::Associate(
45     WebContents* web_contents,
46     XWalkContentsClientBridgeBase* handler) {
47   web_contents->SetUserData(kXWalkContentsClientBridgeBase,
48                             new UserData(handler));
49 }
50
51 // static
52 XWalkContentsClientBridgeBase* XWalkContentsClientBridgeBase::FromWebContents(
53     WebContents* web_contents) {
54   return UserData::GetContents(web_contents);
55 }
56
57 // static
58 XWalkContentsClientBridgeBase* XWalkContentsClientBridgeBase::FromID(
59     int render_process_id,
60     int render_view_id) {
61   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
62   const content::RenderViewHost* rvh =
63       content::RenderViewHost::FromID(render_process_id, render_view_id);
64   if (!rvh) return NULL;
65   content::WebContents* web_contents =
66       content::WebContents::FromRenderViewHost(rvh);
67   return UserData::GetContents(web_contents);
68 }
69
70 XWalkContentsClientBridgeBase::~XWalkContentsClientBridgeBase() {
71 }
72
73 }  // namespace xwalk