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.
5 #include "chrome/browser/sessions/session_tab_helper.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/sessions/session_service.h"
10 #include "chrome/browser/sessions/session_service_factory.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/web_contents.h"
15 #if defined(ENABLE_EXTENSIONS)
16 #include "extensions/common/extension_messages.h"
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SessionTabHelper
);
21 SessionTabHelper::SessionTabHelper(content::WebContents
* contents
)
22 : content::WebContentsObserver(contents
) {
25 SessionTabHelper::~SessionTabHelper() {
28 void SessionTabHelper::SetWindowID(const SessionID
& id
) {
31 #if defined(ENABLE_EXTENSIONS)
32 // Extension code in the renderer holds the ID of the window that hosts it.
33 // Notify it that the window ID changed.
34 web_contents()->GetRenderViewHost()->Send(
35 new ExtensionMsg_UpdateBrowserWindowId(
36 web_contents()->GetRenderViewHost()->GetRoutingID(), id
.id()));
41 SessionID::id_type
SessionTabHelper::IdForTab(const content::WebContents
* tab
) {
42 const SessionTabHelper
* session_tab_helper
=
43 tab
? SessionTabHelper::FromWebContents(tab
) : NULL
;
44 return session_tab_helper
? session_tab_helper
->session_id().id() : -1;
48 SessionID::id_type
SessionTabHelper::IdForWindowContainingTab(
49 const content::WebContents
* tab
) {
50 const SessionTabHelper
* session_tab_helper
=
51 tab
? SessionTabHelper::FromWebContents(tab
) : NULL
;
52 return session_tab_helper
? session_tab_helper
->window_id().id() : -1;
55 #if defined(ENABLE_EXTENSIONS)
56 void SessionTabHelper::RenderViewCreated(
57 content::RenderViewHost
* render_view_host
) {
58 render_view_host
->Send(
59 new ExtensionMsg_UpdateBrowserWindowId(render_view_host
->GetRoutingID(),
64 void SessionTabHelper::UserAgentOverrideSet(const std::string
& user_agent
) {
65 #if defined(ENABLE_SESSION_SERVICE)
67 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
68 SessionService
* session
= SessionServiceFactory::GetForProfile(profile
);
70 session
->SetTabUserAgentOverride(window_id(), session_id(), user_agent
);