session_tab_helper.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/profiles/profile.h"
8#include "chrome/browser/sessions/session_service.h"
9#include "chrome/browser/sessions/session_service_factory.h"
10#include "chrome/browser/ui/tab_contents/tab_contents.h"
11#include "chrome/common/chrome_notification_types.h"
12#include "chrome/common/extensions/extension_messages.h"
13#include "content/public/browser/notification_service.h"
14#include "content/public/browser/render_view_host.h"
15#include "content/public/browser/web_contents.h"
16
17DEFINE_WEB_CONTENTS_USER_DATA_KEY(SessionTabHelper)
18
19SessionTabHelper::SessionTabHelper(content::WebContents* contents)
20    : content::WebContentsObserver(contents) {
21}
22
23SessionTabHelper::~SessionTabHelper() {
24}
25
26void SessionTabHelper::SetWindowID(const SessionID& id) {
27  window_id_ = id;
28
29  // Extension code in the renderer holds the ID of the window that hosts it.
30  // Notify it that the window ID changed.
31  web_contents()->GetRenderViewHost()->Send(
32          new ExtensionMsg_UpdateBrowserWindowId(
33          web_contents()->GetRenderViewHost()->GetRoutingID(), id.id()));
34}
35
36void SessionTabHelper::RenderViewCreated(
37    content::RenderViewHost* render_view_host) {
38  render_view_host->Send(
39      new ExtensionMsg_UpdateBrowserWindowId(render_view_host->GetRoutingID(),
40                                             window_id_.id()));
41}
42
43void SessionTabHelper::UserAgentOverrideSet(const std::string& user_agent) {
44#if defined(ENABLE_SESSION_SERVICE)
45  Profile* profile =
46      Profile::FromBrowserContext(web_contents()->GetBrowserContext());
47  SessionService* session = SessionServiceFactory::GetForProfile(profile);
48  if (session)
49    session->SetTabUserAgentOverride(window_id(), session_id(), user_agent);
50#endif
51}
52