tab_contents_synced_tab_delegate.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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/ui/sync/tab_contents_synced_tab_delegate.h"
6
7#include "base/memory/ref_counted.h"
8#include "chrome/browser/extensions/tab_helper.h"
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/sessions/session_tab_helper.h"
11#include "chrome/browser/sync/glue/synced_window_delegate.h"
12#include "content/public/browser/navigation_controller.h"
13#include "content/public/browser/navigation_entry.h"
14#include "content/public/browser/web_contents.h"
15#include "extensions/common/extension.h"
16
17#if defined(ENABLE_MANAGED_USERS)
18#include "chrome/browser/managed_mode/managed_mode_navigation_observer.h"
19#endif
20
21using content::NavigationEntry;
22
23DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabContentsSyncedTabDelegate);
24
25TabContentsSyncedTabDelegate::TabContentsSyncedTabDelegate(
26    content::WebContents* web_contents)
27    : web_contents_(web_contents), sync_session_id_(0) {}
28
29TabContentsSyncedTabDelegate::~TabContentsSyncedTabDelegate() {}
30
31SessionID::id_type TabContentsSyncedTabDelegate::GetWindowId() const {
32  return SessionTabHelper::FromWebContents(web_contents_)->window_id().id();
33}
34
35SessionID::id_type TabContentsSyncedTabDelegate::GetSessionId() const {
36  return SessionTabHelper::FromWebContents(web_contents_)->session_id().id();
37}
38
39bool TabContentsSyncedTabDelegate::IsBeingDestroyed() const {
40  return web_contents_->IsBeingDestroyed();
41}
42
43Profile* TabContentsSyncedTabDelegate::profile() const {
44  return Profile::FromBrowserContext(web_contents_->GetBrowserContext());
45}
46
47std::string TabContentsSyncedTabDelegate::GetExtensionAppId() const {
48  const scoped_refptr<const extensions::Extension> extension_app(
49      extensions::TabHelper::FromWebContents(web_contents_)->extension_app());
50  return (extension_app.get() ? extension_app->id() : std::string());
51}
52
53int TabContentsSyncedTabDelegate::GetCurrentEntryIndex() const {
54  return web_contents_->GetController().GetCurrentEntryIndex();
55}
56
57int TabContentsSyncedTabDelegate::GetEntryCount() const {
58  return web_contents_->GetController().GetEntryCount();
59}
60
61int TabContentsSyncedTabDelegate::GetPendingEntryIndex() const {
62  return web_contents_->GetController().GetPendingEntryIndex();
63}
64
65NavigationEntry* TabContentsSyncedTabDelegate::GetPendingEntry() const {
66  return web_contents_->GetController().GetPendingEntry();
67}
68
69NavigationEntry* TabContentsSyncedTabDelegate::GetEntryAtIndex(int i) const {
70  return web_contents_->GetController().GetEntryAtIndex(i);
71}
72
73NavigationEntry* TabContentsSyncedTabDelegate::GetActiveEntry() const {
74  return web_contents_->GetController().GetVisibleEntry();
75}
76
77bool TabContentsSyncedTabDelegate::ProfileIsManaged() const {
78  return profile()->IsManaged();
79}
80
81const std::vector<const content::NavigationEntry*>*
82TabContentsSyncedTabDelegate::GetBlockedNavigations() const {
83#if defined(ENABLE_MANAGED_USERS)
84  ManagedModeNavigationObserver* navigation_observer =
85      ManagedModeNavigationObserver::FromWebContents(web_contents_);
86  DCHECK(navigation_observer);
87  return navigation_observer->blocked_navigations();
88#else
89  NOTREACHED();
90  return NULL;
91#endif
92}
93
94bool TabContentsSyncedTabDelegate::IsPinned() const {
95  const browser_sync::SyncedWindowDelegate* window =
96      browser_sync::SyncedWindowDelegate::FindSyncedWindowDelegateWithId(
97          GetWindowId());
98  // We might not have a parent window, e.g. Developer Tools.
99  return window ? window->IsTabPinned(this) : false;
100}
101
102bool TabContentsSyncedTabDelegate::HasWebContents() const { return true; }
103
104content::WebContents* TabContentsSyncedTabDelegate::GetWebContents() const {
105  return web_contents_;
106}
107
108int TabContentsSyncedTabDelegate::GetSyncId() const {
109  return sync_session_id_;
110}
111
112void TabContentsSyncedTabDelegate::SetSyncId(int sync_id) {
113  sync_session_id_ = sync_id;
114}
115