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

Side by Side Diff: chrome/browser/extensions/extension_tabs_module.cc

Issue 8373027: Prevent incognito windows from opening when incognito mode is disabled. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Pulled incognito decision logic into a separate function. Created 9 years, 1 month 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 (*browser)->profile() == incognito_profile) && 243 (*browser)->profile() == incognito_profile) &&
244 (*browser)->window()) { 244 (*browser)->window()) {
245 static_cast<ListValue*>(result_.get())-> 245 static_cast<ListValue*>(result_.get())->
246 Append(ExtensionTabUtil::CreateWindowValue(*browser, populate_tabs)); 246 Append(ExtensionTabUtil::CreateWindowValue(*browser, populate_tabs));
247 } 247 }
248 } 248 }
249 249
250 return true; 250 return true;
251 } 251 }
252 252
253 bool CreateWindowFunction::ShouldOpenIncognitoWindow(
254 const base::DictionaryValue* args,
255 std::vector<GURL>* urls,
256 bool* is_error) {
257 *is_error = false;
258 const IncognitoModePrefs::Availability incognito_availability =
259 IncognitoModePrefs::GetAvailability(profile_->GetPrefs());
260 bool incognito = false;
261 if (args && args->HasKey(keys::kIncognitoKey)) {
262 EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(keys::kIncognitoKey,
263 &incognito));
264 if (incognito && incognito_availability == IncognitoModePrefs::DISABLED) {
265 error_ = keys::kIncognitoModeIsDisabled;
266 *is_error = true;
267 return false;
268 }
269 if (!incognito && incognito_availability == IncognitoModePrefs::FORCED) {
270 error_ = keys::kIncognitoModeIsForced;
271 *is_error = true;
272 return false;
273 }
274 } else if (incognito_availability == IncognitoModePrefs::FORCED) {
275 // If incognito argument is not specified explicitly, we default to
276 // incognito when forced so by policy.
277 incognito = true;
278 }
279
280 // If we are opening an incognito window.
281 if (incognito) {
282 std::string first_url_erased;
283 // Guest session is an exception as it always opens in incognito mode.
284 for (size_t i = 0; i < urls->size();) {
285 if (browser::IsURLAllowedInIncognito((*urls)[i]) &&
286 !Profile::IsGuestSession()) {
287 if (first_url_erased.empty())
288 first_url_erased = (*urls)[i].spec();
289 urls->erase(urls->begin() + i);
290 } else {
291 i++;
292 }
293 }
294 if (urls->empty() && !first_url_erased.empty()) {
295 error_ = ExtensionErrorUtils::FormatErrorMessage(
296 keys::kURLsNotAllowedInIncognitoError, first_url_erased);
297 *is_error = true;
298 return false;
299 }
300 }
301 return incognito;
302 }
303
253 bool CreateWindowFunction::RunImpl() { 304 bool CreateWindowFunction::RunImpl() {
254 DictionaryValue* args = NULL; 305 DictionaryValue* args = NULL;
255 std::vector<GURL> urls; 306 std::vector<GURL> urls;
256 TabContentsWrapper* contents = NULL; 307 TabContentsWrapper* contents = NULL;
257 308
258 if (HasOptionalArgument(0)) 309 if (HasOptionalArgument(0))
259 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); 310 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
260 311
261 // Look for optional url. 312 // Look for optional url.
262 if (args) { 313 if (args) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 #else 396 #else
346 popup_bounds = window_bounds; // Use window size as default for popups 397 popup_bounds = window_bounds; // Use window size as default for popups
347 #endif 398 #endif
348 399
349 Profile* window_profile = profile(); 400 Profile* window_profile = profile();
350 Browser::Type window_type = Browser::TYPE_TABBED; 401 Browser::Type window_type = Browser::TYPE_TABBED;
351 bool focused = true; 402 bool focused = true;
352 bool saw_focus_key = false; 403 bool saw_focus_key = false;
353 std::string extension_id; 404 std::string extension_id;
354 405
406 // Decide whether we are opening a normal window or an incognito window.
407 bool is_error;
408 bool open_incognito_window = ShouldOpenIncognitoWindow(args, &urls,
409 &is_error);
410 if (is_error) {
411 // error_ member variable is set inside of ShouldOpenIncognitoWindow.
412 return false;
413 }
414 if (open_incognito_window) {
415 window_profile = window_profile->GetOffTheRecordProfile();
416 }
417
355 if (args) { 418 if (args) {
356 // Any part of the bounds can optionally be set by the caller. 419 // Any part of the bounds can optionally be set by the caller.
357 int bounds_val; 420 int bounds_val;
358 if (args->HasKey(keys::kLeftKey)) { 421 if (args->HasKey(keys::kLeftKey)) {
359 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kLeftKey, 422 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kLeftKey,
360 &bounds_val)); 423 &bounds_val));
361 window_bounds.set_x(bounds_val); 424 window_bounds.set_x(bounds_val);
362 popup_bounds.set_x(bounds_val); 425 popup_bounds.set_x(bounds_val);
363 panel_bounds.set_x(bounds_val); 426 panel_bounds.set_x(bounds_val);
364 } 427 }
(...skipping 15 matching lines...) Expand all
380 } 443 }
381 444
382 if (args->HasKey(keys::kHeightKey)) { 445 if (args->HasKey(keys::kHeightKey)) {
383 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kHeightKey, 446 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kHeightKey,
384 &bounds_val)); 447 &bounds_val));
385 window_bounds.set_height(bounds_val); 448 window_bounds.set_height(bounds_val);
386 popup_bounds.set_height(bounds_val); 449 popup_bounds.set_height(bounds_val);
387 panel_bounds.set_height(bounds_val); 450 panel_bounds.set_height(bounds_val);
388 } 451 }
389 452
390 bool incognito = false;
391 if (args->HasKey(keys::kIncognitoKey)) {
392 EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(keys::kIncognitoKey,
393 &incognito));
394 if (IncognitoModePrefs::GetAvailability(profile_->GetPrefs()) ==
395 IncognitoModePrefs::DISABLED) {
396 error_ = keys::kIncognitoModeIsDisabled;
397 return false;
398 }
399
400 if (incognito) {
401 std::string first_url_erased;
402 // Guest session is an exception as it always opens in incognito mode.
403 for (size_t i = 0; i < urls.size();) {
404 if (browser::IsURLAllowedInIncognito(urls[i]) &&
405 !Profile::IsGuestSession()) {
406 if (first_url_erased.empty())
407 first_url_erased = urls[i].spec();
408 urls.erase(urls.begin() + i);
409 } else {
410 i++;
411 }
412 }
413 if (urls.empty() && !first_url_erased.empty()) {
414 error_ = ExtensionErrorUtils::FormatErrorMessage(
415 keys::kURLsNotAllowedInIncognitoError, first_url_erased);
416 return false;
417 }
418 window_profile = window_profile->GetOffTheRecordProfile();
419 }
420 }
421
422 if (args->HasKey(keys::kFocusedKey)) { 453 if (args->HasKey(keys::kFocusedKey)) {
423 EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(keys::kFocusedKey, 454 EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(keys::kFocusedKey,
424 &focused)); 455 &focused));
425 saw_focus_key = true; 456 saw_focus_key = true;
426 } 457 }
427 458
428 std::string type_str; 459 std::string type_str;
429 if (args->HasKey(keys::kWindowTypeKey)) { 460 if (args->HasKey(keys::kWindowTypeKey)) {
430 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kWindowTypeKey, 461 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kWindowTypeKey,
431 &type_str)); 462 &type_str));
(...skipping 1106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1538 // called for every API call the extension made. 1569 // called for every API call the extension made.
1539 GotLanguage(language); 1570 GotLanguage(language);
1540 } 1571 }
1541 1572
1542 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { 1573 void DetectTabLanguageFunction::GotLanguage(const std::string& language) {
1543 result_.reset(Value::CreateStringValue(language.c_str())); 1574 result_.reset(Value::CreateStringValue(language.c_str()));
1544 SendResponse(true); 1575 SendResponse(true);
1545 1576
1546 Release(); // Balanced in Run() 1577 Release(); // Balanced in Run()
1547 } 1578 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_tabs_module.h ('k') | chrome/browser/extensions/extension_tabs_module_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698