15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_SESSIONS_SESSION_TAB_HELPER_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_SESSIONS_SESSION_TAB_HELPER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "components/sessions/session_id.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents_observer.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents_user_data.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class keeps the extension API's windowID up-to-date with the current
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// window of the tab.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SessionTabHelper : public content::WebContentsObserver,
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         public content::WebContentsUserData<SessionTabHelper> {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~SessionTabHelper();
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the identifier used by session restore for this tab.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const SessionID& session_id() const { return session_id_; }
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Identifier of the window the tab is in.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetWindowID(const SessionID& id);
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const SessionID& window_id() const { return window_id_; }
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // If the specified WebContents has a SessionTabHelper (probably because it
2803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // was used as the contents of a tab), returns a tab id. This value is
2903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // immutable for a given tab. It will be unique across Chrome within the
3003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // current session, but may be re-used across sessions. Returns -1
3103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // for a NULL WebContents or if the WebContents has no SessionTabHelper.
3203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  static SessionID::id_type IdForTab(const content::WebContents* tab);
3303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
3403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // If the specified WebContents has a SessionTabHelper (probably because it
3503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // was used as the contents of a tab), and has ever been attached to a Browser
3603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // window, returns Browser::session_id().id() for that Browser. If the tab is
3703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // being dragged between Browser windows, returns the old window's id value.
3803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // If the WebContents has a SessionTabHelper but has never been attached to a
3903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Browser window, returns an id value that is different from that of any
4003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Browser. Returns -1 for a NULL WebContents or if the WebContents has no
4103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // SessionTabHelper.
4203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  static SessionID::id_type IdForWindowContainingTab(
4303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      const content::WebContents* tab);
4403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // content::WebContentsObserver:
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(ENABLE_EXTENSIONS)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RenderViewCreated(
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::RenderViewHost* render_view_host) OVERRIDE;
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void UserAgentOverrideSet(const std::string& user_agent) OVERRIDE;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit SessionTabHelper(content::WebContents* contents);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class content::WebContentsUserData<SessionTabHelper>;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unique identifier of the tab for session restore. This id is only unique
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // within the current session, and is not guaranteed to be unique across
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sessions.
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const SessionID session_id_;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unique identifier of the window the tab is in.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SessionID window_id_;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SessionTabHelper);
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_SESSIONS_SESSION_TAB_HELPER_H_
68