Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: chrome/browser/extensions/api/tabs/tabs_api.cc

Issue 21022018: Sessions API - previously Session Restore API. Supports restoring currently open foreign windows an… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: delete test. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/tabs/tabs_api.h" 5 #include "chrome/browser/extensions/api/tabs/tabs_api.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 #include <vector> 9 #include <vector>
10 10
11 #include "apps/shell_window.h" 11 #include "apps/shell_window.h"
12 #include "base/base64.h" 12 #include "base/base64.h"
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/memory/ref_counted_memory.h" 16 #include "base/memory/ref_counted_memory.h"
17 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
18 #include "base/prefs/pref_service.h" 18 #include "base/prefs/pref_service.h"
19 #include "base/stl_util.h" 19 #include "base/stl_util.h"
20 #include "base/strings/string16.h" 20 #include "base/strings/string16.h"
21 #include "base/strings/string_number_conversions.h" 21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
23 #include "base/strings/stringprintf.h" 23 #include "base/strings/stringprintf.h"
24 #include "base/strings/utf_string_conversions.h" 24 #include "base/strings/utf_string_conversions.h"
25 #include "chrome/browser/chrome_notification_types.h" 25 #include "chrome/browser/chrome_notification_types.h"
26 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" 26 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
27 #include "chrome/browser/extensions/api/tabs/windows_util.h"
27 #include "chrome/browser/extensions/extension_function_dispatcher.h" 28 #include "chrome/browser/extensions/extension_function_dispatcher.h"
28 #include "chrome/browser/extensions/extension_function_util.h" 29 #include "chrome/browser/extensions/extension_function_util.h"
29 #include "chrome/browser/extensions/extension_host.h" 30 #include "chrome/browser/extensions/extension_host.h"
30 #include "chrome/browser/extensions/extension_service.h" 31 #include "chrome/browser/extensions/extension_service.h"
31 #include "chrome/browser/extensions/extension_tab_util.h" 32 #include "chrome/browser/extensions/extension_tab_util.h"
32 #include "chrome/browser/extensions/script_executor.h" 33 #include "chrome/browser/extensions/script_executor.h"
33 #include "chrome/browser/extensions/tab_helper.h" 34 #include "chrome/browser/extensions/tab_helper.h"
34 #include "chrome/browser/extensions/window_controller.h" 35 #include "chrome/browser/extensions/window_controller.h"
35 #include "chrome/browser/extensions/window_controller_list.h" 36 #include "chrome/browser/extensions/window_controller_list.h"
36 #include "chrome/browser/prefs/incognito_mode_prefs.h" 37 #include "chrome/browser/prefs/incognito_mode_prefs.h"
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 *browser = GetBrowserInProfileWithId( 164 *browser = GetBrowserInProfileWithId(
164 function->profile(), window_id, function->include_incognito(), &error); 165 function->profile(), window_id, function->include_incognito(), &error);
165 if (!*browser) { 166 if (!*browser) {
166 function->SetError(error); 167 function->SetError(error);
167 return false; 168 return false;
168 } 169 }
169 } 170 }
170 return true; 171 return true;
171 } 172 }
172 173
173 bool GetWindowFromWindowID(UIThreadExtensionFunction* function,
174 int window_id,
175 WindowController** controller) {
176 if (window_id == extension_misc::kCurrentWindowId) {
177 WindowController* extension_window_controller =
178 function->dispatcher()->delegate()->GetExtensionWindowController();
179 // If there is a window controller associated with this extension, use that.
180 if (extension_window_controller) {
181 *controller = extension_window_controller;
182 } else {
183 // Otherwise get the focused or most recently added window.
184 *controller = WindowControllerList::GetInstance()->
185 CurrentWindowForFunction(function);
186 }
187 if (!(*controller)) {
188 function->SetError(keys::kNoCurrentWindowError);
189 return false;
190 }
191 } else {
192 *controller = WindowControllerList::GetInstance()->
193 FindWindowForFunctionById(function, window_id);
194 if (!(*controller)) {
195 function->SetError(ErrorUtils::FormatErrorMessage(
196 keys::kWindowNotFoundError, base::IntToString(window_id)));
197 return false;
198 }
199 }
200 return true;
201 }
202
203 // |error_message| can optionally be passed in and will be set with an 174 // |error_message| can optionally be passed in and will be set with an
204 // appropriate message if the tab cannot be found by id. 175 // appropriate message if the tab cannot be found by id.
205 bool GetTabById(int tab_id, 176 bool GetTabById(int tab_id,
206 Profile* profile, 177 Profile* profile,
207 bool include_incognito, 178 bool include_incognito,
208 Browser** browser, 179 Browser** browser,
209 TabStripModel** tab_strip, 180 TabStripModel** tab_strip,
210 content::WebContents** contents, 181 content::WebContents** contents,
211 int* tab_index, 182 int* tab_index,
212 std::string* error_message) { 183 std::string* error_message) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 228
258 bool WindowsGetFunction::RunImpl() { 229 bool WindowsGetFunction::RunImpl() {
259 scoped_ptr<Get::Params> params(Get::Params::Create(*args_)); 230 scoped_ptr<Get::Params> params(Get::Params::Create(*args_));
260 EXTENSION_FUNCTION_VALIDATE(params.get()); 231 EXTENSION_FUNCTION_VALIDATE(params.get());
261 232
262 bool populate_tabs = false; 233 bool populate_tabs = false;
263 if (params->get_info.get() && params->get_info->populate.get()) 234 if (params->get_info.get() && params->get_info->populate.get())
264 populate_tabs = *params->get_info->populate; 235 populate_tabs = *params->get_info->populate;
265 236
266 WindowController* controller; 237 WindowController* controller;
267 if (!GetWindowFromWindowID(this, params->window_id, &controller)) 238 if (!windows_util::GetWindowFromWindowID(this,
239 params->window_id,
240 &controller)) {
268 return false; 241 return false;
242 }
269 243
270 if (populate_tabs) 244 if (populate_tabs)
271 SetResult(controller->CreateWindowValueWithTabs(GetExtension())); 245 SetResult(controller->CreateWindowValueWithTabs(GetExtension()));
272 else 246 else
273 SetResult(controller->CreateWindowValue()); 247 SetResult(controller->CreateWindowValue());
274 return true; 248 return true;
275 } 249 }
276 250
277 bool WindowsGetCurrentFunction::RunImpl() { 251 bool WindowsGetCurrentFunction::RunImpl() {
278 scoped_ptr<GetCurrent::Params> params(GetCurrent::Params::Create(*args_)); 252 scoped_ptr<GetCurrent::Params> params(GetCurrent::Params::Create(*args_));
279 EXTENSION_FUNCTION_VALIDATE(params.get()); 253 EXTENSION_FUNCTION_VALIDATE(params.get());
280 254
281 bool populate_tabs = false; 255 bool populate_tabs = false;
282 if (params->get_info.get() && params->get_info->populate.get()) 256 if (params->get_info.get() && params->get_info->populate.get())
283 populate_tabs = *params->get_info->populate; 257 populate_tabs = *params->get_info->populate;
284 258
285 WindowController* controller; 259 WindowController* controller;
286 if (!GetWindowFromWindowID(this, 260 if (!windows_util::GetWindowFromWindowID(this,
287 extension_misc::kCurrentWindowId, 261 extension_misc::kCurrentWindowId,
288 &controller)) { 262 &controller)) {
289 return false; 263 return false;
290 } 264 }
291 if (populate_tabs) 265 if (populate_tabs)
292 SetResult(controller->CreateWindowValueWithTabs(GetExtension())); 266 SetResult(controller->CreateWindowValueWithTabs(GetExtension()));
293 else 267 else
294 SetResult(controller->CreateWindowValue()); 268 SetResult(controller->CreateWindowValue());
295 return true; 269 return true;
296 } 270 }
297 271
298 bool WindowsGetLastFocusedFunction::RunImpl() { 272 bool WindowsGetLastFocusedFunction::RunImpl() {
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 return true; 661 return true;
688 } 662 }
689 663
690 bool WindowsUpdateFunction::RunImpl() { 664 bool WindowsUpdateFunction::RunImpl() {
691 int window_id = extension_misc::kUnknownWindowId; 665 int window_id = extension_misc::kUnknownWindowId;
692 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); 666 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id));
693 base::DictionaryValue* update_props; 667 base::DictionaryValue* update_props;
694 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); 668 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props));
695 669
696 WindowController* controller; 670 WindowController* controller;
697 if (!GetWindowFromWindowID(this, window_id, &controller)) 671 if (!windows_util::GetWindowFromWindowID(this, window_id, &controller))
698 return false; 672 return false;
699 673
700 #if defined(OS_WIN) 674 #if defined(OS_WIN)
701 // Silently ignore changes on the window for metro mode. 675 // Silently ignore changes on the window for metro mode.
702 if (win8::IsSingleWindowMetroMode()) { 676 if (win8::IsSingleWindowMetroMode()) {
703 SetResult(controller->CreateWindowValue()); 677 SetResult(controller->CreateWindowValue());
704 return true; 678 return true;
705 } 679 }
706 #endif 680 #endif
707 681
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 SetResult(controller->CreateWindowValue()); 805 SetResult(controller->CreateWindowValue());
832 806
833 return true; 807 return true;
834 } 808 }
835 809
836 bool WindowsRemoveFunction::RunImpl() { 810 bool WindowsRemoveFunction::RunImpl() {
837 int window_id = -1; 811 int window_id = -1;
838 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); 812 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id));
839 813
840 WindowController* controller; 814 WindowController* controller;
841 if (!GetWindowFromWindowID(this, window_id, &controller)) 815 if (!windows_util::GetWindowFromWindowID(this, window_id, &controller))
842 return false; 816 return false;
843 817
844 #if defined(OS_WIN) 818 #if defined(OS_WIN)
845 // In Windows 8 metro mode, an existing Browser instance is reused for 819 // In Windows 8 metro mode, an existing Browser instance is reused for
846 // hosting the extension tab. We should not be closing it as we don't own it. 820 // hosting the extension tab. We should not be closing it as we don't own it.
847 if (win8::IsSingleWindowMetroMode()) 821 if (win8::IsSingleWindowMetroMode())
848 return false; 822 return false;
849 #endif 823 #endif
850 824
851 WindowController::Reason reason; 825 WindowController::Reason reason;
(...skipping 1213 matching lines...) Expand 10 before | Expand all | Expand 10 after
2065 execute_tab_id_ = tab_id; 2039 execute_tab_id_ = tab_id;
2066 details_ = details.Pass(); 2040 details_ = details.Pass();
2067 return true; 2041 return true;
2068 } 2042 }
2069 2043
2070 bool TabsInsertCSSFunction::ShouldInsertCSS() const { 2044 bool TabsInsertCSSFunction::ShouldInsertCSS() const {
2071 return true; 2045 return true;
2072 } 2046 }
2073 2047
2074 } // namespace extensions 2048 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/sessions/sessions_apitest.cc ('k') | chrome/browser/extensions/api/tabs/windows_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698