Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / browser_tabrestore.cc
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/browser_tabrestore.h"
6
7 #include "base/memory/scoped_vector.h"
8 #include "chrome/browser/extensions/tab_helper.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/sessions/session_service.h"
11 #include "chrome/browser/sessions/session_service_factory.h"
12 #include "chrome/browser/tab_contents/tab_util.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/browser/ui/web_contents_sizer.h"
17 #include "components/sessions/content/content_serialized_navigation_builder.h"
18 #include "content/public/browser/navigation_controller.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/session_storage_namespace.h"
21 #include "content/public/browser/web_contents.h"
22
23 using content::WebContents;
24 using content::NavigationController;
25 using content::NavigationEntry;
26 using sessions::ContentSerializedNavigationBuilder;
27 using sessions::SerializedNavigationEntry;
28
29 namespace chrome {
30
31 namespace {
32
33 NavigationController::RestoreType GetRestoreType(Browser* browser,
34                                                  bool from_last_session) {
35   if (!from_last_session)
36     return NavigationController::RESTORE_CURRENT_SESSION;
37   return browser->profile()->GetLastSessionExitType() == Profile::EXIT_CRASHED ?
38       NavigationController::RESTORE_LAST_SESSION_CRASHED :
39       NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY;
40 }
41
42 WebContents* CreateRestoredTab(
43     Browser* browser,
44     const std::vector<SerializedNavigationEntry>& navigations,
45     int selected_navigation,
46     const std::string& extension_app_id,
47     bool from_last_session,
48     content::SessionStorageNamespace* session_storage_namespace,
49     const std::string& user_agent_override,
50     bool initially_hidden) {
51   GURL restore_url = navigations.at(selected_navigation).virtual_url();
52   // TODO(ajwong): Remove the temporary session_storage_namespace_map when
53   // we teach session restore to understand that one tab can have multiple
54   // SessionStorageNamespace objects. Also remove the
55   // session_storage_namespace.h include since we only need that to assign
56   // into the map.
57   content::SessionStorageNamespaceMap session_storage_namespace_map;
58   session_storage_namespace_map[std::string()] = session_storage_namespace;
59   WebContents::CreateParams create_params(
60       browser->profile(),
61       tab_util::GetSiteInstanceForNewTab(browser->profile(), restore_url));
62   create_params.initially_hidden = initially_hidden;
63   WebContents* base_web_contents =
64       browser->tab_strip_model()->GetActiveWebContents();
65   if (base_web_contents) {
66     create_params.initial_size =
67         base_web_contents->GetContainerBounds().size();
68   }
69   WebContents* web_contents = content::WebContents::CreateWithSessionStorage(
70       create_params,
71       session_storage_namespace_map);
72   extensions::TabHelper::CreateForWebContents(web_contents);
73   extensions::TabHelper::FromWebContents(web_contents)->
74       SetExtensionAppById(extension_app_id);
75   ScopedVector<NavigationEntry> scoped_entries =
76       ContentSerializedNavigationBuilder::ToNavigationEntries(
77           navigations, browser->profile());
78   // NavigationController::Restore() expects to take ownership of the entries.
79   std::vector<NavigationEntry*> entries;
80   scoped_entries.release(&entries);
81   web_contents->SetUserAgentOverride(user_agent_override);
82   web_contents->GetController().Restore(
83       selected_navigation, GetRestoreType(browser, from_last_session),
84       &entries);
85   DCHECK_EQ(0u, entries.size());
86
87   return web_contents;
88 }
89
90 }  // namespace
91
92 content::WebContents* AddRestoredTab(
93     Browser* browser,
94     const std::vector<SerializedNavigationEntry>& navigations,
95     int tab_index,
96     int selected_navigation,
97     const std::string& extension_app_id,
98     bool select,
99     bool pin,
100     bool from_last_session,
101     content::SessionStorageNamespace* session_storage_namespace,
102     const std::string& user_agent_override) {
103   WebContents* web_contents = CreateRestoredTab(browser,
104                                                 navigations,
105                                                 selected_navigation,
106                                                 extension_app_id,
107                                                 from_last_session,
108                                                 session_storage_namespace,
109                                                 user_agent_override,
110                                                 !select);
111
112   int add_types = select ? TabStripModel::ADD_ACTIVE
113                          : TabStripModel::ADD_NONE;
114   if (pin) {
115     int first_mini_tab_idx =
116         browser->tab_strip_model()->IndexOfFirstNonMiniTab();
117     tab_index = std::min(tab_index, first_mini_tab_idx);
118     add_types |= TabStripModel::ADD_PINNED;
119   }
120   browser->tab_strip_model()->InsertWebContentsAt(tab_index, web_contents,
121                                                   add_types);
122   if (select) {
123     browser->window()->Activate();
124   } else {
125     // We set the size of the view here, before Blink does its initial layout.
126     // If we don't, the initial layout of background tabs will be performed
127     // with a view width of 0, which may cause script outputs and anchor link
128     // location calculations to be incorrect even after a new layout with
129     // proper view dimensions. TabStripModel::AddWebContents() contains similar
130     // logic.
131     gfx::Size size = browser->window()->GetBounds().size();
132     // Fallback to the restore bounds if it's empty as the window is not shown
133     // yet and the bounds may not be available on all platforms.
134     if (size.IsEmpty())
135       size = browser->window()->GetRestoredBounds().size();
136     ResizeWebContents(web_contents, size);
137     web_contents->WasHidden();
138   }
139   SessionService* session_service =
140       SessionServiceFactory::GetForProfileIfExisting(browser->profile());
141   if (session_service)
142     session_service->TabRestored(web_contents, pin);
143   return web_contents;
144 }
145
146 content::WebContents* ReplaceRestoredTab(
147     Browser* browser,
148     const std::vector<SerializedNavigationEntry>& navigations,
149     int selected_navigation,
150     bool from_last_session,
151     const std::string& extension_app_id,
152     content::SessionStorageNamespace* session_storage_namespace,
153     const std::string& user_agent_override) {
154   WebContents* web_contents = CreateRestoredTab(browser,
155                                                 navigations,
156                                                 selected_navigation,
157                                                 extension_app_id,
158                                                 from_last_session,
159                                                 session_storage_namespace,
160                                                 user_agent_override,
161                                                 false);
162
163   // ReplaceWebContentsAt won't animate in the restoration, so manually do the
164   // equivalent of ReplaceWebContentsAt.
165   TabStripModel* tab_strip = browser->tab_strip_model();
166   int insertion_index = tab_strip->active_index();
167   tab_strip->InsertWebContentsAt(insertion_index + 1,
168                                  web_contents,
169                                  TabStripModel::ADD_ACTIVE |
170                                  TabStripModel::ADD_INHERIT_GROUP);
171   tab_strip->CloseWebContentsAt(insertion_index, TabStripModel::CLOSE_NONE);
172   return web_contents;
173 }
174
175 }  // namespace chrome