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