OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sessions/session_tab_helper.h" | 5 #include "chrome/browser/sessions/session_tab_helper.h" |
6 | 6 |
7 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/sessions/session_service.h" | 8 #include "chrome/browser/sessions/session_service.h" |
9 #include "chrome/browser/sessions/session_service_factory.h" | 9 #include "chrome/browser/sessions/session_service_factory.h" |
10 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
11 #include "chrome/common/extensions/extension_messages.h" | 11 #include "chrome/common/extensions/extension_messages.h" |
12 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
13 #include "content/public/browser/render_view_host.h" | 13 #include "content/public/browser/render_view_host.h" |
14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
15 | 15 |
16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SessionTabHelper); | 16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SessionTabHelper); |
17 | 17 |
18 SessionTabHelper::SessionTabHelper(content::WebContents* contents) | 18 SessionTabHelper::SessionTabHelper(content::WebContents* contents) |
19 : content::WebContentsObserver(contents) { | 19 : content::WebContentsObserver(contents) { |
20 } | 20 } |
21 | 21 |
22 SessionTabHelper::~SessionTabHelper() { | 22 SessionTabHelper::SessionTabHelper(content::WebContents* contents, |
| 23 const SessionID& id) |
| 24 : content::WebContentsObserver(contents), |
| 25 session_id_(id) { |
23 } | 26 } |
24 | 27 |
| 28 // static |
| 29 void SessionTabHelper::CreateForWebContentsWithId( |
| 30 content::WebContents* contents, |
| 31 const SessionID& id) { |
| 32 DCHECK(contents); |
| 33 if (FromWebContents(contents)) { |
| 34 DCHECK_EQ(id.id(), FromWebContents(contents)->session_id().id()); |
| 35 return; |
| 36 } |
| 37 contents->SetUserData(UserDataKey(), new SessionTabHelper(contents, id)); |
| 38 } |
| 39 |
| 40 SessionTabHelper::~SessionTabHelper() {} |
| 41 |
25 void SessionTabHelper::SetWindowID(const SessionID& id) { | 42 void SessionTabHelper::SetWindowID(const SessionID& id) { |
26 window_id_ = id; | 43 window_id_ = id; |
27 | 44 |
28 // Extension code in the renderer holds the ID of the window that hosts it. | 45 // Extension code in the renderer holds the ID of the window that hosts it. |
29 // Notify it that the window ID changed. | 46 // Notify it that the window ID changed. |
30 web_contents()->GetRenderViewHost()->Send( | 47 web_contents()->GetRenderViewHost()->Send( |
31 new ExtensionMsg_UpdateBrowserWindowId( | 48 new ExtensionMsg_UpdateBrowserWindowId( |
32 web_contents()->GetRenderViewHost()->GetRoutingID(), id.id())); | 49 web_contents()->GetRenderViewHost()->GetRoutingID(), id.id())); |
33 } | 50 } |
34 | 51 |
35 void SessionTabHelper::RenderViewCreated( | 52 void SessionTabHelper::RenderViewCreated( |
36 content::RenderViewHost* render_view_host) { | 53 content::RenderViewHost* render_view_host) { |
37 render_view_host->Send( | 54 render_view_host->Send( |
38 new ExtensionMsg_UpdateBrowserWindowId(render_view_host->GetRoutingID(), | 55 new ExtensionMsg_UpdateBrowserWindowId(render_view_host->GetRoutingID(), |
39 window_id_.id())); | 56 window_id_.id())); |
40 } | 57 } |
41 | 58 |
42 void SessionTabHelper::UserAgentOverrideSet(const std::string& user_agent) { | 59 void SessionTabHelper::UserAgentOverrideSet(const std::string& user_agent) { |
43 #if defined(ENABLE_SESSION_SERVICE) | 60 #if defined(ENABLE_SESSION_SERVICE) |
44 Profile* profile = | 61 Profile* profile = |
45 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 62 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
46 SessionService* session = SessionServiceFactory::GetForProfile(profile); | 63 SessionService* session = SessionServiceFactory::GetForProfile(profile); |
47 if (session) | 64 if (session) |
48 session->SetTabUserAgentOverride(window_id(), session_id(), user_agent); | 65 session->SetTabUserAgentOverride(window_id(), session_id(), user_agent); |
49 #endif | 66 #endif |
50 } | 67 } |
OLD | NEW |