1 // Copyright 2014 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 "chrome/browser/renderer_context_menu/context_menu_delegate.h" 6 7 #include "content/public/browser/web_contents.h" 8 9 namespace { 10 11 const char kMenuDelegateUserDataKey[] = "RendererContextMenuMenuDelegate"; 12 13 class ContextMenuDelegateUserData : public base::SupportsUserData::Data { 14 public: 15 explicit ContextMenuDelegateUserData(ContextMenuDelegate* menu_delegate) 16 : menu_delegate_(menu_delegate) { 17 } 18 virtual ~ContextMenuDelegateUserData() {} 19 ContextMenuDelegate* menu_delegate() { return menu_delegate_; } 20 21 private: 22 ContextMenuDelegate* menu_delegate_; // not owned by us. 23 }; 24 25 } // namespace 26 27 ContextMenuDelegate::ContextMenuDelegate(content::WebContents* web_contents) { 28 web_contents->SetUserData(&kMenuDelegateUserDataKey, 29 new ContextMenuDelegateUserData(this)); 30 } 31 32 ContextMenuDelegate::~ContextMenuDelegate() { 33 } 34 35 // static 36 ContextMenuDelegate* ContextMenuDelegate::FromWebContents( 37 content::WebContents* web_contents) { 38 ContextMenuDelegateUserData* user_data = 39 static_cast<ContextMenuDelegateUserData*>( 40 web_contents->GetUserData(&kMenuDelegateUserDataKey)); 41 return user_data ? user_data->menu_delegate() : NULL; 42 } 43