OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/extension_tabs_module.h" | 5 #include "chrome/browser/extensions/extension_tabs_module.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "chrome/browser/browser.h" | 9 #include "chrome/browser/browser.h" |
10 #include "chrome/browser/browser_list.h" | 10 #include "chrome/browser/browser_list.h" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 return result; | 135 return result; |
136 } | 136 } |
137 | 137 |
138 // if |populate| is true, each window gets a list property |tabs| which contains | 138 // if |populate| is true, each window gets a list property |tabs| which contains |
139 // fully populated tab objects. | 139 // fully populated tab objects. |
140 DictionaryValue* ExtensionTabUtil::CreateWindowValue(const Browser* browser, | 140 DictionaryValue* ExtensionTabUtil::CreateWindowValue(const Browser* browser, |
141 bool populate_tabs) { | 141 bool populate_tabs) { |
| 142 DCHECK(browser); |
| 143 DCHECK(browser->window()); |
142 DictionaryValue* result = new DictionaryValue(); | 144 DictionaryValue* result = new DictionaryValue(); |
143 result->SetInteger(keys::kIdKey, ExtensionTabUtil::GetWindowId(browser)); | 145 result->SetInteger(keys::kIdKey, ExtensionTabUtil::GetWindowId(browser)); |
144 bool focused = false; | |
145 if (browser->window()) | |
146 focused = browser->window()->IsActive(); | |
147 | |
148 result->SetBoolean(keys::kIncognitoKey, | 146 result->SetBoolean(keys::kIncognitoKey, |
149 browser->profile()->IsOffTheRecord()); | 147 browser->profile()->IsOffTheRecord()); |
150 result->SetBoolean(keys::kFocusedKey, focused); | 148 result->SetBoolean(keys::kFocusedKey, browser->window()->IsActive()); |
151 gfx::Rect bounds = browser->window()->GetRestoredBounds(); | 149 gfx::Rect bounds = browser->window()->GetRestoredBounds(); |
152 | 150 |
153 // TODO(rafaelw): zIndex ? | |
154 result->SetInteger(keys::kLeftKey, bounds.x()); | 151 result->SetInteger(keys::kLeftKey, bounds.x()); |
155 result->SetInteger(keys::kTopKey, bounds.y()); | 152 result->SetInteger(keys::kTopKey, bounds.y()); |
156 result->SetInteger(keys::kWidthKey, bounds.width()); | 153 result->SetInteger(keys::kWidthKey, bounds.width()); |
157 result->SetInteger(keys::kHeightKey, bounds.height()); | 154 result->SetInteger(keys::kHeightKey, bounds.height()); |
158 result->SetString(keys::kWindowTypeKey, GetWindowTypeText(browser->type())); | 155 result->SetString(keys::kWindowTypeKey, GetWindowTypeText(browser->type())); |
159 | 156 |
160 if (populate_tabs) { | 157 if (populate_tabs) { |
161 result->Set(keys::kTabsKey, ExtensionTabUtil::CreateTabList(browser)); | 158 result->Set(keys::kTabsKey, ExtensionTabUtil::CreateTabList(browser)); |
162 } | 159 } |
163 | 160 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } | 214 } |
218 | 215 |
219 // Windows --------------------------------------------------------------------- | 216 // Windows --------------------------------------------------------------------- |
220 | 217 |
221 bool GetWindowFunction::RunImpl() { | 218 bool GetWindowFunction::RunImpl() { |
222 int window_id; | 219 int window_id; |
223 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 220 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
224 | 221 |
225 Browser* browser = GetBrowserInProfileWithId(profile(), window_id, | 222 Browser* browser = GetBrowserInProfileWithId(profile(), window_id, |
226 include_incognito(), &error_); | 223 include_incognito(), &error_); |
227 if (!browser) | 224 if (!browser || !browser->window()) { |
| 225 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 226 keys::kWindowNotFoundError, IntToString(window_id)); |
228 return false; | 227 return false; |
| 228 } |
229 | 229 |
230 result_.reset(ExtensionTabUtil::CreateWindowValue(browser, false)); | 230 result_.reset(ExtensionTabUtil::CreateWindowValue(browser, false)); |
231 return true; | 231 return true; |
232 } | 232 } |
233 | 233 |
234 bool GetCurrentWindowFunction::RunImpl() { | 234 bool GetCurrentWindowFunction::RunImpl() { |
235 Browser* browser = GetCurrentBrowser(); | 235 Browser* browser = GetCurrentBrowser(); |
236 if (!browser) { | 236 if (!browser || !browser->window()) { |
237 error_ = keys::kNoCurrentWindowError; | 237 error_ = keys::kNoCurrentWindowError; |
238 return false; | 238 return false; |
239 } | 239 } |
240 result_.reset(ExtensionTabUtil::CreateWindowValue(browser, false)); | 240 result_.reset(ExtensionTabUtil::CreateWindowValue(browser, false)); |
241 return true; | 241 return true; |
242 } | 242 } |
243 | 243 |
244 bool GetLastFocusedWindowFunction::RunImpl() { | 244 bool GetLastFocusedWindowFunction::RunImpl() { |
245 Browser* browser = BrowserList::FindBrowserWithType( | 245 Browser* browser = BrowserList::FindBrowserWithType( |
246 profile(), Browser::TYPE_ANY, include_incognito()); | 246 profile(), Browser::TYPE_ANY, include_incognito()); |
247 if (!browser) { | 247 if (!browser || !browser->window()) { |
248 error_ = keys::kNoLastFocusedWindowError; | 248 error_ = keys::kNoLastFocusedWindowError; |
249 return false; | 249 return false; |
250 } | 250 } |
251 result_.reset(ExtensionTabUtil::CreateWindowValue(browser, false)); | 251 result_.reset(ExtensionTabUtil::CreateWindowValue(browser, false)); |
252 return true; | 252 return true; |
253 } | 253 } |
254 | 254 |
255 bool GetAllWindowsFunction::RunImpl() { | 255 bool GetAllWindowsFunction::RunImpl() { |
256 bool populate_tabs = false; | 256 bool populate_tabs = false; |
257 if (HasOptionalArgument(0)) { | 257 if (HasOptionalArgument(0)) { |
258 DictionaryValue* args; | 258 DictionaryValue* args; |
259 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | 259 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); |
260 | 260 |
261 if (args->HasKey(keys::kPopulateKey)) { | 261 if (args->HasKey(keys::kPopulateKey)) { |
262 EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(keys::kPopulateKey, | 262 EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(keys::kPopulateKey, |
263 &populate_tabs)); | 263 &populate_tabs)); |
264 } | 264 } |
265 } | 265 } |
266 | 266 |
267 result_.reset(new ListValue()); | 267 result_.reset(new ListValue()); |
268 Profile* incognito_profile = | 268 Profile* incognito_profile = |
269 include_incognito() ? profile()->GetOffTheRecordProfile() : NULL; | 269 include_incognito() ? profile()->GetOffTheRecordProfile() : NULL; |
270 for (BrowserList::const_iterator browser = BrowserList::begin(); | 270 for (BrowserList::const_iterator browser = BrowserList::begin(); |
271 browser != BrowserList::end(); ++browser) { | 271 browser != BrowserList::end(); ++browser) { |
272 // Only examine browsers in the current profile. | 272 // Only examine browsers in the current profile that have windows. |
273 if ((*browser)->profile() == profile() || | 273 if ((*browser)->profile() == profile() || |
274 (*browser)->profile() == incognito_profile) { | 274 (*browser)->profile() == incognito_profile && |
| 275 (*browser)->window()) { |
275 static_cast<ListValue*>(result_.get())-> | 276 static_cast<ListValue*>(result_.get())-> |
276 Append(ExtensionTabUtil::CreateWindowValue(*browser, populate_tabs)); | 277 Append(ExtensionTabUtil::CreateWindowValue(*browser, populate_tabs)); |
277 } | 278 } |
278 } | 279 } |
279 | 280 |
280 return true; | 281 return true; |
281 } | 282 } |
282 | 283 |
283 bool CreateWindowFunction::RunImpl() { | 284 bool CreateWindowFunction::RunImpl() { |
284 GURL url; | 285 GURL url; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 } | 369 } |
369 | 370 |
370 Browser* new_window = new Browser(window_type, window_profile); | 371 Browser* new_window = new Browser(window_type, window_profile); |
371 new_window->CreateBrowserWindow(); | 372 new_window->CreateBrowserWindow(); |
372 new_window->AddTabWithURL(url, GURL(), PageTransition::LINK, -1, | 373 new_window->AddTabWithURL(url, GURL(), PageTransition::LINK, -1, |
373 TabStripModel::ADD_SELECTED, NULL, std::string()); | 374 TabStripModel::ADD_SELECTED, NULL, std::string()); |
374 | 375 |
375 new_window->window()->SetBounds(bounds); | 376 new_window->window()->SetBounds(bounds); |
376 new_window->window()->Show(); | 377 new_window->window()->Show(); |
377 | 378 |
378 // TODO(rafaelw): support |focused|, |zIndex| | |
379 if (new_window->profile()->IsOffTheRecord() && !include_incognito()) { | 379 if (new_window->profile()->IsOffTheRecord() && !include_incognito()) { |
380 // Don't expose incognito windows if the extension isn't allowed. | 380 // Don't expose incognito windows if the extension isn't allowed. |
381 result_.reset(Value::CreateNullValue()); | 381 result_.reset(Value::CreateNullValue()); |
382 } else { | 382 } else { |
383 result_.reset(ExtensionTabUtil::CreateWindowValue(new_window, false)); | 383 result_.reset(ExtensionTabUtil::CreateWindowValue(new_window, false)); |
384 } | 384 } |
385 | 385 |
386 return true; | 386 return true; |
387 } | 387 } |
388 | 388 |
389 bool UpdateWindowFunction::RunImpl() { | 389 bool UpdateWindowFunction::RunImpl() { |
390 int window_id; | 390 int window_id; |
391 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 391 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
392 DictionaryValue* update_props; | 392 DictionaryValue* update_props; |
393 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); | 393 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); |
394 | 394 |
395 Browser* browser = GetBrowserInProfileWithId(profile(), window_id, | 395 Browser* browser = GetBrowserInProfileWithId(profile(), window_id, |
396 include_incognito(), &error_); | 396 include_incognito(), &error_); |
397 if (!browser) | 397 if (!browser || !browser->window()) { |
| 398 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 399 keys::kWindowNotFoundError, IntToString(window_id)); |
398 return false; | 400 return false; |
| 401 } |
399 | 402 |
400 gfx::Rect bounds = browser->window()->GetRestoredBounds(); | 403 gfx::Rect bounds = browser->window()->GetRestoredBounds(); |
401 // Any part of the bounds can optionally be set by the caller. | 404 // Any part of the bounds can optionally be set by the caller. |
402 int bounds_val; | 405 int bounds_val; |
403 if (update_props->HasKey(keys::kLeftKey)) { | 406 if (update_props->HasKey(keys::kLeftKey)) { |
404 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( | 407 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( |
405 keys::kLeftKey, | 408 keys::kLeftKey, |
406 &bounds_val)); | 409 &bounds_val)); |
407 bounds.set_x(bounds_val); | 410 bounds.set_x(bounds_val); |
408 } | 411 } |
(...skipping 13 matching lines...) Expand all Loading... |
422 } | 425 } |
423 | 426 |
424 if (update_props->HasKey(keys::kHeightKey)) { | 427 if (update_props->HasKey(keys::kHeightKey)) { |
425 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( | 428 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( |
426 keys::kHeightKey, | 429 keys::kHeightKey, |
427 &bounds_val)); | 430 &bounds_val)); |
428 bounds.set_height(bounds_val); | 431 bounds.set_height(bounds_val); |
429 } | 432 } |
430 | 433 |
431 browser->window()->SetBounds(bounds); | 434 browser->window()->SetBounds(bounds); |
432 // TODO(rafaelw): Support |focused|. | |
433 result_.reset(ExtensionTabUtil::CreateWindowValue(browser, false)); | 435 result_.reset(ExtensionTabUtil::CreateWindowValue(browser, false)); |
434 | 436 |
435 return true; | 437 return true; |
436 } | 438 } |
437 | 439 |
438 bool RemoveWindowFunction::RunImpl() { | 440 bool RemoveWindowFunction::RunImpl() { |
439 int window_id; | 441 int window_id; |
440 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 442 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
441 | 443 |
442 Browser* browser = GetBrowserInProfileWithId(profile(), window_id, | 444 Browser* browser = GetBrowserInProfileWithId(profile(), window_id, |
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1045 include_incognito ? profile->GetOffTheRecordProfile() : NULL; | 1047 include_incognito ? profile->GetOffTheRecordProfile() : NULL; |
1046 for (BrowserList::const_iterator browser = BrowserList::begin(); | 1048 for (BrowserList::const_iterator browser = BrowserList::begin(); |
1047 browser != BrowserList::end(); ++browser) { | 1049 browser != BrowserList::end(); ++browser) { |
1048 if (((*browser)->profile() == profile || | 1050 if (((*browser)->profile() == profile || |
1049 (*browser)->profile() == incognito_profile) && | 1051 (*browser)->profile() == incognito_profile) && |
1050 ExtensionTabUtil::GetWindowId(*browser) == window_id) | 1052 ExtensionTabUtil::GetWindowId(*browser) == window_id) |
1051 return *browser; | 1053 return *browser; |
1052 } | 1054 } |
1053 | 1055 |
1054 if (error_message) | 1056 if (error_message) |
1055 *error_message= ExtensionErrorUtils::FormatErrorMessage( | 1057 *error_message = ExtensionErrorUtils::FormatErrorMessage( |
1056 keys::kWindowNotFoundError, IntToString(window_id)); | 1058 keys::kWindowNotFoundError, IntToString(window_id)); |
1057 | 1059 |
1058 return NULL; | 1060 return NULL; |
1059 } | 1061 } |
1060 | 1062 |
1061 static bool GetTabById(int tab_id, Profile* profile, | 1063 static bool GetTabById(int tab_id, Profile* profile, |
1062 bool include_incognito, | 1064 bool include_incognito, |
1063 Browser** browser, | 1065 Browser** browser, |
1064 TabStripModel** tab_strip, | 1066 TabStripModel** tab_strip, |
1065 TabContents** contents, | 1067 TabContents** contents, |
(...skipping 22 matching lines...) Expand all Loading... |
1088 } | 1090 } |
1089 | 1091 |
1090 static GURL ResolvePossiblyRelativeURL(std::string url_string, | 1092 static GURL ResolvePossiblyRelativeURL(std::string url_string, |
1091 Extension* extension) { | 1093 Extension* extension) { |
1092 GURL url = GURL(url_string); | 1094 GURL url = GURL(url_string); |
1093 if (!url.is_valid()) | 1095 if (!url.is_valid()) |
1094 url = extension->GetResourceURL(url_string); | 1096 url = extension->GetResourceURL(url_string); |
1095 | 1097 |
1096 return url; | 1098 return url; |
1097 } | 1099 } |
OLD | NEW |