- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / ntp / foreign_session_handler.h
1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_UI_WEBUI_NTP_FOREIGN_SESSION_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_NTP_FOREIGN_SESSION_HANDLER_H_
7
8 #include <vector>
9
10 #include "base/time/time.h"
11 #include "chrome/browser/sessions/session_service.h"
12 #include "chrome/browser/sync/glue/session_model_associator.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "content/public/browser/web_ui.h"
16 #include "content/public/browser/web_ui_message_handler.h"
17
18 namespace user_prefs {
19 class PrefRegistrySyncable;
20 }
21
22 namespace browser_sync {
23
24 class ForeignSessionHandler : public content::WebUIMessageHandler,
25                               public content::NotificationObserver {
26  public:
27   // Invalid value, used to note that we don't have a tab or window number.
28   static const int kInvalidId = -1;
29
30   // WebUIMessageHandler implementation.
31   virtual void RegisterMessages() OVERRIDE;
32
33   ForeignSessionHandler();
34   virtual ~ForeignSessionHandler() {}
35
36   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
37
38   static void OpenForeignSessionTab(content::WebUI* web_ui,
39                                     const std::string& session_string_value,
40                                     SessionID::id_type window_num,
41                                     SessionID::id_type tab_id,
42                                     const WindowOpenDisposition& disposition);
43
44   static void OpenForeignSessionWindows(content::WebUI* web_ui,
45                                         const std::string& session_string_value,
46                                         SessionID::id_type window_num);
47
48   // Helper method to create JSON compatible objects from Session objects.
49   static bool SessionTabToValue(const SessionTab& tab,
50                                 DictionaryValue* dictionary);
51
52   // Returns a pointer to the current session model associator or NULL.
53   static SessionModelAssociator* GetModelAssociator(content::WebUI* web_ui);
54
55  private:
56   // Used to register ForeignSessionHandler for notifications.
57   void Init();
58
59   // Determines how ForeignSessionHandler will interact with the new tab page.
60   virtual void Observe(int type,
61                        const content::NotificationSource& source,
62                        const content::NotificationDetails& details) OVERRIDE;
63
64   // Returns true if tab sync is enabled for this profile, otherwise false.
65   bool IsTabSyncEnabled();
66
67   // Returns a string used to show the user when a session was last modified.
68   string16 FormatSessionTime(const base::Time& time);
69
70   // Determines which session is to be opened, and then calls
71   // OpenForeignSession, to begin the process of opening a new browser window.
72   // This is a javascript callback handler.
73   void HandleOpenForeignSession(const ListValue* args);
74
75   // Determines whether foreign sessions should be obtained from the sync model.
76   // This is a javascript callback handler, and it is also called when the sync
77   // model has changed and the new tab page needs to reflect the changes.
78   void HandleGetForeignSessions(const ListValue* args);
79
80   // Delete a foreign session. This will remove it from the list of foreign
81   // sessions on all devices. It will reappear if the session is re-activated
82   // on the original device.
83   // This is a javascript callback handler.
84   void HandleDeleteForeignSession(const ListValue* args);
85
86   void HandleSetForeignSessionCollapsed(const ListValue* args);
87
88   // Helper method to create JSON compatible objects from Session objects.
89   bool SessionWindowToValue(const SessionWindow& window,
90                             DictionaryValue* dictionary);
91
92   // The Registrar used to register ForeignSessionHandler for notifications.
93   content::NotificationRegistrar registrar_;
94
95   DISALLOW_COPY_AND_ASSIGN(ForeignSessionHandler);
96 };
97
98 }  // namespace browser_sync
99
100 #endif  // CHROME_BROWSER_UI_WEBUI_NTP_FOREIGN_SESSION_HANDLER_H_