/chrome/browser/extensions/api/sessions/sessions_api.h

https://gitlab.com/0072016/Facebook-SDK- · C Header · 143 lines · 99 code · 30 blank · 14 comment · 0 complexity · 58ce23805db38f69c1f62407aa2cc277 MD5 · raw file

  1. // Copyright 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. #ifndef CHROME_BROWSER_EXTENSIONS_API_SESSIONS_SESSIONS_API_H__
  5. #define CHROME_BROWSER_EXTENSIONS_API_SESSIONS_SESSIONS_API_H__
  6. #include <string>
  7. #include "base/macros.h"
  8. #include "chrome/browser/extensions/chrome_extension_function.h"
  9. #include "chrome/common/extensions/api/sessions.h"
  10. #include "chrome/common/extensions/api/tabs.h"
  11. #include "chrome/common/extensions/api/windows.h"
  12. #include "components/sessions/core/tab_restore_service.h"
  13. #include "components/sessions/core/tab_restore_service_observer.h"
  14. #include "extensions/browser/browser_context_keyed_api_factory.h"
  15. #include "extensions/browser/event_router.h"
  16. class Profile;
  17. namespace sync_driver {
  18. struct SyncedSession;
  19. }
  20. namespace extensions {
  21. class SessionId;
  22. class SessionsGetRecentlyClosedFunction : public ChromeSyncExtensionFunction {
  23. protected:
  24. ~SessionsGetRecentlyClosedFunction() override {}
  25. bool RunSync() override;
  26. DECLARE_EXTENSION_FUNCTION("sessions.getRecentlyClosed",
  27. SESSIONS_GETRECENTLYCLOSED)
  28. private:
  29. api::tabs::Tab CreateTabModel(const sessions::TabRestoreService::Tab& tab,
  30. int session_id,
  31. int selected_index);
  32. std::unique_ptr<api::windows::Window> CreateWindowModel(
  33. const sessions::TabRestoreService::Window& window,
  34. int session_id);
  35. std::unique_ptr<api::sessions::Session> CreateSessionModel(
  36. const sessions::TabRestoreService::Entry* entry);
  37. };
  38. class SessionsGetDevicesFunction : public ChromeSyncExtensionFunction {
  39. protected:
  40. ~SessionsGetDevicesFunction() override {}
  41. bool RunSync() override;
  42. DECLARE_EXTENSION_FUNCTION("sessions.getDevices", SESSIONS_GETDEVICES)
  43. private:
  44. api::tabs::Tab CreateTabModel(const std::string& session_tag,
  45. const sessions::SessionTab& tab,
  46. int tab_index,
  47. int selected_index);
  48. std::unique_ptr<api::windows::Window> CreateWindowModel(
  49. const sessions::SessionWindow& window,
  50. const std::string& session_tag);
  51. std::unique_ptr<api::sessions::Session> CreateSessionModel(
  52. const sessions::SessionWindow& window,
  53. const std::string& session_tag);
  54. api::sessions::Device CreateDeviceModel(
  55. const sync_driver::SyncedSession* session);
  56. };
  57. class SessionsRestoreFunction : public ChromeSyncExtensionFunction {
  58. protected:
  59. ~SessionsRestoreFunction() override {}
  60. bool RunSync() override;
  61. DECLARE_EXTENSION_FUNCTION("sessions.restore", SESSIONS_RESTORE)
  62. private:
  63. void SetInvalidIdError(const std::string& invalid_id);
  64. void SetResultRestoredTab(content::WebContents* contents);
  65. bool SetResultRestoredWindow(int window_id);
  66. bool RestoreMostRecentlyClosed(Browser* browser);
  67. bool RestoreLocalSession(const SessionId& session_id, Browser* browser);
  68. bool RestoreForeignSession(const SessionId& session_id,
  69. Browser* browser);
  70. };
  71. class SessionsEventRouter : public sessions::TabRestoreServiceObserver {
  72. public:
  73. explicit SessionsEventRouter(Profile* profile);
  74. ~SessionsEventRouter() override;
  75. // Observer callback for TabRestoreServiceObserver. Sends data on
  76. // recently closed tabs to the javascript side of this page to
  77. // display to the user.
  78. void TabRestoreServiceChanged(sessions::TabRestoreService* service) override;
  79. // Observer callback to notice when our associated TabRestoreService
  80. // is destroyed.
  81. void TabRestoreServiceDestroyed(
  82. sessions::TabRestoreService* service) override;
  83. private:
  84. Profile* profile_;
  85. // TabRestoreService that we are observing.
  86. sessions::TabRestoreService* tab_restore_service_;
  87. DISALLOW_COPY_AND_ASSIGN(SessionsEventRouter);
  88. };
  89. class SessionsAPI : public BrowserContextKeyedAPI,
  90. public extensions::EventRouter::Observer {
  91. public:
  92. explicit SessionsAPI(content::BrowserContext* context);
  93. ~SessionsAPI() override;
  94. // BrowserContextKeyedService implementation.
  95. void Shutdown() override;
  96. // BrowserContextKeyedAPI implementation.
  97. static BrowserContextKeyedAPIFactory<SessionsAPI>* GetFactoryInstance();
  98. // EventRouter::Observer implementation.
  99. void OnListenerAdded(const extensions::EventListenerInfo& details) override;
  100. private:
  101. friend class BrowserContextKeyedAPIFactory<SessionsAPI>;
  102. content::BrowserContext* browser_context_;
  103. // BrowserContextKeyedAPI implementation.
  104. static const char* service_name() {
  105. return "SessionsAPI";
  106. }
  107. static const bool kServiceIsNULLWhileTesting = true;
  108. // Created lazily upon OnListenerAdded.
  109. std::unique_ptr<SessionsEventRouter> sessions_event_router_;
  110. DISALLOW_COPY_AND_ASSIGN(SessionsAPI);
  111. };
  112. } // namespace extensions
  113. #endif // CHROME_BROWSER_EXTENSIONS_API_SESSIONS_SESSIONS_API_H__