1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/sync/glue/synced_tab_delegate_android.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/ref_counted.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/android/tab_android.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/extensions/tab_helper.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/sessions/session_tab_helper.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/sync/glue/synced_window_delegate.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/browser/navigation_controller.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/browser/navigation_entry.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/browser/web_contents.h"
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using content::NavigationEntry;
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace browser_sync {
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)SyncedTabDelegateAndroid::SyncedTabDelegateAndroid(TabAndroid* tab_android)
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : web_contents_(NULL), tab_android_(tab_android) {}
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)SyncedTabDelegateAndroid::~SyncedTabDelegateAndroid() {}
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)SessionID::id_type SyncedTabDelegateAndroid::GetWindowId() const {
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->GetWindowId();
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)SessionID::id_type SyncedTabDelegateAndroid::GetSessionId() const {
33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return tab_android_->session_id().id();
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool SyncedTabDelegateAndroid::IsBeingDestroyed() const {
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->IsBeingDestroyed();
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)Profile* SyncedTabDelegateAndroid::profile() const {
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->profile();
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::string SyncedTabDelegateAndroid::GetExtensionAppId() const {
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->GetExtensionAppId();
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)int SyncedTabDelegateAndroid::GetCurrentEntryIndex() const {
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->GetCurrentEntryIndex();
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)int SyncedTabDelegateAndroid::GetEntryCount() const {
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->GetEntryCount();
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)int SyncedTabDelegateAndroid::GetPendingEntryIndex() const {
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->GetPendingEntryIndex();
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)NavigationEntry* SyncedTabDelegateAndroid::GetPendingEntry() const {
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->GetPendingEntry();
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)NavigationEntry* SyncedTabDelegateAndroid::GetEntryAtIndex(int i) const {
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->GetEntryAtIndex(i);
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)NavigationEntry* SyncedTabDelegateAndroid::GetActiveEntry() const {
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->GetActiveEntry();
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool SyncedTabDelegateAndroid::IsPinned() const {
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->IsPinned();
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool SyncedTabDelegateAndroid::HasWebContents() const {
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return web_contents_ != NULL;
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
90424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)content::WebContents* SyncedTabDelegateAndroid::GetWebContents() const {
91424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return web_contents_;
92424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
93424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void SyncedTabDelegateAndroid::SetWebContents(
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    content::WebContents* web_contents) {
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  web_contents_ = web_contents;
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TabContentsSyncedTabDelegate::CreateForWebContents(web_contents_);
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void SyncedTabDelegateAndroid::ResetWebContents() { web_contents_ = NULL; }
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
102f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)bool SyncedTabDelegateAndroid::ProfileIsSupervised() const {
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
104f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      ->ProfileIsSupervised();
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const std::vector<const content::NavigationEntry*>*
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)SyncedTabDelegateAndroid::GetBlockedNavigations() const {
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return TabContentsSyncedTabDelegate::FromWebContents(web_contents_)
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->GetBlockedNavigations();
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
113a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)int SyncedTabDelegateAndroid::GetSyncId() const {
1147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return tab_android_->GetSyncId();
1157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
117a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void SyncedTabDelegateAndroid::SetSyncId(int sync_id) {
1187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  tab_android_->SetSyncId(sync_id);
1197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// static
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)SyncedTabDelegate* SyncedTabDelegate::ImplFromWebContents(
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    content::WebContents* web_contents) {
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TabAndroid* tab = TabAndroid::FromWebContents(web_contents);
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return tab ? tab->GetSyncedTabDelegate() : NULL;
126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace browser_sync
128