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