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 #include "chrome/browser/sessions/session_tab_helper.h" 6 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" 14 15 #if defined(ENABLE_EXTENSIONS) 16 #include "extensions/common/extension_messages.h" 17 #endif 18 19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SessionTabHelper); 20 SessionTabHelper(content::WebContents * contents)21SessionTabHelper::SessionTabHelper(content::WebContents* contents) 22 : content::WebContentsObserver(contents) { 23 } 24 ~SessionTabHelper()25SessionTabHelper::~SessionTabHelper() { 26 } 27 SetWindowID(const SessionID & id)28void SessionTabHelper::SetWindowID(const SessionID& id) { 29 window_id_ = id; 30 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())); 37 #endif 38 } 39 40 // static IdForTab(const content::WebContents * tab)41SessionID::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; 45 } 46 47 // static IdForWindowContainingTab(const content::WebContents * tab)48SessionID::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; 53 } 54 55 #if defined(ENABLE_EXTENSIONS) RenderViewCreated(content::RenderViewHost * render_view_host)56void SessionTabHelper::RenderViewCreated( 57 content::RenderViewHost* render_view_host) { 58 render_view_host->Send( 59 new ExtensionMsg_UpdateBrowserWindowId(render_view_host->GetRoutingID(), 60 window_id_.id())); 61 } 62 #endif 63 UserAgentOverrideSet(const std::string & user_agent)64void SessionTabHelper::UserAgentOverrideSet(const std::string& user_agent) { 65 #if defined(ENABLE_SESSION_SERVICE) 66 Profile* profile = 67 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 68 SessionService* session = SessionServiceFactory::GetForProfile(profile); 69 if (session) 70 session->SetTabUserAgentOverride(window_id(), session_id(), user_agent); 71 #endif 72 } 73