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