15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sessions/session_tab_helper.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sync/glue/synced_window_delegate.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/navigation_controller.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/navigation_entry.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(ENABLE_EXTENSIONS)
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/extensions/tab_helper.h"
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(ENABLE_MANAGED_USERS)
21f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/supervised_user/supervised_user_navigation_observer.h"
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::NavigationEntry;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabContentsSyncedTabDelegate);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TabContentsSyncedTabDelegate::TabContentsSyncedTabDelegate(
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    content::WebContents* web_contents)
307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    : web_contents_(web_contents), sync_session_id_(0) {}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TabContentsSyncedTabDelegate::~TabContentsSyncedTabDelegate() {}
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SessionID::id_type TabContentsSyncedTabDelegate::GetWindowId() const {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return SessionTabHelper::FromWebContents(web_contents_)->window_id().id();
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SessionID::id_type TabContentsSyncedTabDelegate::GetSessionId() const {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return SessionTabHelper::FromWebContents(web_contents_)->session_id().id();
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabContentsSyncedTabDelegate::IsBeingDestroyed() const {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return web_contents_->IsBeingDestroyed();
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Profile* TabContentsSyncedTabDelegate::profile() const {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return Profile::FromBrowserContext(web_contents_->GetBrowserContext());
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string TabContentsSyncedTabDelegate::GetExtensionAppId() const {
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(ENABLE_EXTENSIONS)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const scoped_refptr<const extensions::Extension> extension_app(
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      extensions::TabHelper::FromWebContents(web_contents_)->extension_app());
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (extension_app.get())
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return extension_app->id();
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return std::string();
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TabContentsSyncedTabDelegate::GetCurrentEntryIndex() const {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return web_contents_->GetController().GetCurrentEntryIndex();
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TabContentsSyncedTabDelegate::GetEntryCount() const {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return web_contents_->GetController().GetEntryCount();
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TabContentsSyncedTabDelegate::GetPendingEntryIndex() const {
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return web_contents_->GetController().GetPendingEntryIndex();
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NavigationEntry* TabContentsSyncedTabDelegate::GetPendingEntry() const {
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return web_contents_->GetController().GetPendingEntry();
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NavigationEntry* TabContentsSyncedTabDelegate::GetEntryAtIndex(int i) const {
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return web_contents_->GetController().GetEntryAtIndex(i);
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NavigationEntry* TabContentsSyncedTabDelegate::GetActiveEntry() const {
81a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return web_contents_->GetController().GetVisibleEntry();
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
84f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)bool TabContentsSyncedTabDelegate::ProfileIsSupervised() const {
85f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return profile()->IsSupervised();
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const std::vector<const content::NavigationEntry*>*
8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TabContentsSyncedTabDelegate::GetBlockedNavigations() const {
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(ENABLE_MANAGED_USERS)
91f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  SupervisedUserNavigationObserver* navigation_observer =
92f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SupervisedUserNavigationObserver::FromWebContents(web_contents_);
9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(navigation_observer);
9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return navigation_observer->blocked_navigations();
9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else
9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NOTREACHED();
9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return NULL;
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabContentsSyncedTabDelegate::IsPinned() const {
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const browser_sync::SyncedWindowDelegate* window =
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      browser_sync::SyncedWindowDelegate::FindSyncedWindowDelegateWithId(
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          GetWindowId());
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We might not have a parent window, e.g. Developer Tools.
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return window ? window->IsTabPinned(this) : false;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool TabContentsSyncedTabDelegate::HasWebContents() const { return true; }
1107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
111424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)content::WebContents* TabContentsSyncedTabDelegate::GetWebContents() const {
112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return web_contents_;
113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
114424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
115a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)int TabContentsSyncedTabDelegate::GetSyncId() const {
1167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return sync_session_id_;
1177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
119a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void TabContentsSyncedTabDelegate::SetSyncId(int sync_id) {
1207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  sync_session_id_ = sync_id;
1217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
122