blob: 2cffb588e864a5829bf989fe13b4586ca352d3a8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_
#define CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_
#include "base/supports_user_data.h"
#include "content/public/browser/web_contents.h"
// A base class for classes attached to, and scoped to, the lifetime of a
// WebContents. For example:
//
// --- in foo_tab_helper.h ---
// class FooTabHelper : public WebContentsUserData<FooTabHelper> {
// public:
// virtual ~FooTabHelper();
// // ... more public stuff here ...
// private:
// explicit FooTabHelper(content::WebContents* contents);
// static int kUserDataKey;
// friend class WebContentsUserData<FooTabHelper>;
// // ... more private stuff here ...
// }
// --- in foo_tab_helper.cc ---
// int FooTabHelper::kUserDataKey;
//
template <typename T>
class WebContentsUserData : public base::SupportsUserData::Data {
public:
// Creates an object of type T, and attaches it to the specified WebContents.
// If an instance is already attached, does nothing.
static void CreateForWebContents(content::WebContents* contents) {
if (!FromWebContents(contents))
contents->SetUserData(&T::kUserDataKey, new T(contents));
}
// Retrieves the instance of type T that was attached to the specified
// WebContents (via CreateForWebContents above) and returns it. If no instance
// of the type was attached, returns NULL.
static T* FromWebContents(content::WebContents* contents) {
return static_cast<T*>(contents->GetUserData(&T::kUserDataKey));
}
static const T* FromWebContents(const content::WebContents* contents) {
return static_cast<const T*>(contents->GetUserData(&T::kUserDataKey));
}
};
#endif // CHROME_BROWSER_COMMON_WEB_CONTENTS_USER_DATA_H_
|