session_tab_helper.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) 2011 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. #ifndef COMPONENTS_SESSIONS_CONTENT_SESSION_TAB_HELPER_H_
  5. #define COMPONENTS_SESSIONS_CONTENT_SESSION_TAB_HELPER_H_
  6. #include "base/callback.h"
  7. #include "base/callback_list.h"
  8. #include "components/sessions/core/session_id.h"
  9. #include "components/sessions/core/sessions_export.h"
  10. #include "content/public/browser/web_contents_observer.h"
  11. #include "content/public/browser/web_contents_user_data.h"
  12. namespace sessions {
  13. class SessionTabHelperDelegate;
  14. // This class keeps the extension API's windowID up to date with the current
  15. // window of the tab and observes navigation events.
  16. class SESSIONS_EXPORT SessionTabHelper
  17. : public content::WebContentsObserver,
  18. public content::WebContentsUserData<SessionTabHelper> {
  19. public:
  20. using DelegateLookup =
  21. base::RepeatingCallback<SessionTabHelperDelegate*(content::WebContents*)>;
  22. using WindowIdChangedCallbackList =
  23. base::RepeatingCallbackList<void(const SessionID& id)>;
  24. SessionTabHelper(const SessionTabHelper&) = delete;
  25. SessionTabHelper& operator=(const SessionTabHelper&) = delete;
  26. ~SessionTabHelper() override;
  27. // Returns the identifier used by session restore for this tab.
  28. const SessionID& session_id() const { return session_id_; }
  29. // Identifier of the window the tab is in.
  30. void SetWindowID(const SessionID& id);
  31. const SessionID& window_id() const { return window_id_; }
  32. // If the specified WebContents has a SessionTabHelper (probably because it
  33. // was used as the contents of a tab), returns a tab id. This value is
  34. // immutable for a given tab. It will be unique across Chrome within the
  35. // current session, but may be re-used across sessions. Returns
  36. // SessionID::InvalidValue() for a null WebContents or if the WebContents has
  37. // no SessionTabHelper.
  38. static SessionID IdForTab(const content::WebContents* tab);
  39. // If the specified WebContents has a SessionTabHelper (probably because it
  40. // was used as the contents of a tab), and has ever been attached to a Browser
  41. // window, returns Browser::session_id().id() for that Browser. If the tab is
  42. // being dragged between Browser windows, returns the old window's id value.
  43. // If the WebContents has a SessionTabHelper but has never been attached to a
  44. // Browser window, returns an id value that is different from that of any
  45. // Browser. Returns SessionID::InvalidValue() for a null WebContents or if the
  46. // WebContents has no SessionTabHelper.
  47. static SessionID IdForWindowContainingTab(const content::WebContents* tab);
  48. base::CallbackListSubscription RegisterForWindowIdChanged(
  49. WindowIdChangedCallbackList::CallbackType callback);
  50. // content::WebContentsObserver:
  51. void UserAgentOverrideSet(
  52. const blink::UserAgentOverride& ua_override) override;
  53. void NavigationEntryCommitted(
  54. const content::LoadCommittedDetails& load_details) override;
  55. void NavigationListPruned(
  56. const content::PrunedDetails& pruned_details) override;
  57. void NavigationEntriesDeleted() override;
  58. void NavigationEntryChanged(
  59. const content::EntryChangedDetails& change_details) override;
  60. private:
  61. friend class content::WebContentsUserData<SessionTabHelper>;
  62. SessionTabHelper(content::WebContents* contents, DelegateLookup lookup);
  63. sessions::SessionTabHelperDelegate* GetDelegate();
  64. WindowIdChangedCallbackList window_id_changed_callbacks_;
  65. DelegateLookup delegate_lookup_;
  66. // Unique identifier of the tab for session restore. This id is only unique
  67. // within the current session, and is not guaranteed to be unique across
  68. // sessions.
  69. const SessionID session_id_;
  70. // Unique identifier of the window the tab is in.
  71. SessionID window_id_;
  72. WEB_CONTENTS_USER_DATA_KEY_DECL();
  73. };
  74. } // namespace sessions
  75. #endif // COMPONENTS_SESSIONS_CONTENT_SESSION_TAB_HELPER_H_