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

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

Issue 199074: Don't allow updating tabs to javascript URLs without host (Closed)
Patch Set: Remove unchanged file Created 11 years, 3 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
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/gfx/jpeg_codec.h" 7 #include "base/gfx/jpeg_codec.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"
11 #include "chrome/browser/browser_window.h" 11 #include "chrome/browser/browser_window.h"
12 #include "chrome/browser/extensions/extension_function_dispatcher.h" 12 #include "chrome/browser/extensions/extension_function_dispatcher.h"
13 #include "chrome/browser/extensions/extension_tabs_module_constants.h" 13 #include "chrome/browser/extensions/extension_tabs_module_constants.h"
14 #include "chrome/browser/extensions/extensions_service.h" 14 #include "chrome/browser/extensions/extensions_service.h"
15 #include "chrome/browser/profile.h" 15 #include "chrome/browser/profile.h"
16 #include "chrome/browser/renderer_host/backing_store.h" 16 #include "chrome/browser/renderer_host/backing_store.h"
17 #include "chrome/browser/renderer_host/render_view_host.h" 17 #include "chrome/browser/renderer_host/render_view_host.h"
18 #include "chrome/browser/renderer_host/render_view_host_delegate.h" 18 #include "chrome/browser/renderer_host/render_view_host_delegate.h"
19 #include "chrome/browser/tab_contents/navigation_entry.h" 19 #include "chrome/browser/tab_contents/navigation_entry.h"
20 #include "chrome/browser/tab_contents/tab_contents.h" 20 #include "chrome/browser/tab_contents/tab_contents.h"
21 #include "chrome/browser/window_sizer.h" 21 #include "chrome/browser/window_sizer.h"
22 #include "chrome/common/extensions/extension.h" 22 #include "chrome/common/extensions/extension.h"
23 #include "chrome/common/extensions/extension_error_utils.h" 23 #include "chrome/common/extensions/extension_error_utils.h"
24 #include "chrome/common/url_constants.h"
24 #include "net/base/base64.h" 25 #include "net/base/base64.h"
25 #include "skia/ext/image_operations.h" 26 #include "skia/ext/image_operations.h"
26 #include "skia/ext/platform_canvas.h" 27 #include "skia/ext/platform_canvas.h"
27 #include "third_party/skia/include/core/SkBitmap.h" 28 #include "third_party/skia/include/core/SkBitmap.h"
28 29
29 30
30 namespace keys = extension_tabs_module_constants; 31 namespace keys = extension_tabs_module_constants;
31 32
32 // Forward declare static helper functions defined below. 33 // Forward declare static helper functions defined below.
33 34
34 // |error_message| can optionally be passed in a will be set with an appropriate 35 // |error_message| can optionally be passed in a will be set with an appropriate
35 // message if the window cannot be found by id. 36 // message if the window cannot be found by id.
36 static Browser* GetBrowserInProfileWithId(Profile* profile, 37 static Browser* GetBrowserInProfileWithId(Profile* profile,
37 const int window_id, 38 const int window_id,
38 std::string* error_message); 39 std::string* error_message);
39 40
40 // |error_message| can optionally be passed in a will be set with an appropriate 41 // |error_message| can optionally be passed in a will be set with an appropriate
41 // message if the tab cannot be found by id. 42 // message if the tab cannot be found by id.
42 static bool GetTabById(int tab_id, Profile* profile, Browser** browser, 43 static bool GetTabById(int tab_id, Profile* profile, Browser** browser,
43 TabStripModel** tab_strip, 44 TabStripModel** tab_strip,
44 TabContents** contents, 45 TabContents** contents,
45 int* tab_index, std::string* error_message); 46 int* tab_index, std::string* error_message);
46 47
47 // Construct an absolute path from a relative path.
48 static GURL AbsolutePath(Profile* profile, const std::string& extension_id,
49 const std::string& relative_url);
50
51 int ExtensionTabUtil::GetWindowId(const Browser* browser) { 48 int ExtensionTabUtil::GetWindowId(const Browser* browser) {
52 return browser->session_id().id(); 49 return browser->session_id().id();
53 } 50 }
54 51
55 int ExtensionTabUtil::GetTabId(const TabContents* tab_contents) { 52 int ExtensionTabUtil::GetTabId(const TabContents* tab_contents) {
56 return tab_contents->controller().session_id().id(); 53 return tab_contents->controller().session_id().id();
57 } 54 }
58 55
59 ExtensionTabUtil::TabStatus ExtensionTabUtil::GetTabStatus( 56 ExtensionTabUtil::TabStatus ExtensionTabUtil::GetTabStatus(
60 const TabContents* tab_contents) { 57 const TabContents* tab_contents) {
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 // -favIconUrl 461 // -favIconUrl
465 462
466 std::string url_string; 463 std::string url_string;
467 scoped_ptr<GURL> url(new GURL()); 464 scoped_ptr<GURL> url(new GURL());
468 if (args->HasKey(keys::kUrlKey)) { 465 if (args->HasKey(keys::kUrlKey)) {
469 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, 466 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey,
470 &url_string)); 467 &url_string));
471 url.reset(new GURL(url_string)); 468 url.reset(new GURL(url_string));
472 if (!url->is_valid()) { 469 if (!url->is_valid()) {
473 // The path as passed in is not valid. Try converting to absolute path. 470 // The path as passed in is not valid. Try converting to absolute path.
474 *url = AbsolutePath(profile(), extension_id(), url_string); 471 *url = GetExtension()->GetResourceURL(url->spec());
475 if (!url->is_valid()) { 472 if (!url->is_valid()) {
476 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, 473 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError,
477 url_string); 474 url_string);
478 return false; 475 return false;
479 } 476 }
480 } 477 }
481 } 478 }
482 479
483 // Default to foreground for the new tab. The presence of 'selected' property 480 // Default to foreground for the new tab. The presence of 'selected' property
484 // will override this default. 481 // will override this default.
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 548
552 // Navigate the tab to a new location if the url different. 549 // Navigate the tab to a new location if the url different.
553 std::string url; 550 std::string url;
554 if (update_props->HasKey(keys::kUrlKey)) { 551 if (update_props->HasKey(keys::kUrlKey)) {
555 EXTENSION_FUNCTION_VALIDATE(update_props->GetString( 552 EXTENSION_FUNCTION_VALIDATE(update_props->GetString(
556 keys::kUrlKey, &url)); 553 keys::kUrlKey, &url));
557 GURL new_gurl(url); 554 GURL new_gurl(url);
558 555
559 if (!new_gurl.is_valid()) { 556 if (!new_gurl.is_valid()) {
560 // The path as passed in is not valid. Try converting to absolute path. 557 // The path as passed in is not valid. Try converting to absolute path.
561 new_gurl = AbsolutePath(profile(), extension_id(), url); 558 new_gurl = GetExtension()->GetResourceURL(url);
562 if (!new_gurl.is_valid()) { 559 if (!new_gurl.is_valid()) {
563 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, 560 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError,
564 url); 561 url);
565 return false; 562 return false;
566 } 563 }
567 } 564 }
568 565
566 // JavaScript URLs can do the same kinds of things as cross-origin XHR, so
567 // we need to check host permissions before allowing them.
568 if (new_gurl.SchemeIs(chrome::kJavaScriptScheme)) {
569 if (!GetExtension()->CanAccessHost(contents->GetURL())) {
570 error_ = ExtensionErrorUtils::FormatErrorMessage(
571 keys::kCannotAccessPageError, contents->GetURL().spec());
572 return false;
573 }
574
575 // TODO(aa): How does controller queue URLs? Is there any chance that this
576 // JavaScript URL will end up applying to something other than
577 // controller->GetURL()?
578 }
579
569 controller.LoadURL(new_gurl, GURL(), PageTransition::LINK); 580 controller.LoadURL(new_gurl, GURL(), PageTransition::LINK);
570 DCHECK_EQ(new_gurl.spec(), contents->GetURL().spec()); 581
582 // The URL of a tab contents never actually changes to a JavaScript URL, so
583 // this check only makes sense in other cases.
584 if (!new_gurl.SchemeIs(chrome::kJavaScriptScheme))
585 DCHECK_EQ(new_gurl.spec(), contents->GetURL().spec());
571 } 586 }
572 587
573 bool selected = false; 588 bool selected = false;
574 // TODO(rafaelw): Setting |selected| from js doesn't make much sense. 589 // TODO(rafaelw): Setting |selected| from js doesn't make much sense.
575 // Move tab selection management up to window. 590 // Move tab selection management up to window.
576 if (update_props->HasKey(keys::kSelectedKey)) { 591 if (update_props->HasKey(keys::kSelectedKey)) {
577 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( 592 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean(
578 keys::kSelectedKey, 593 keys::kSelectedKey,
579 &selected)); 594 &selected));
580 if (selected && tab_strip->selected_index() != tab_index) { 595 if (selected && tab_strip->selected_index() != tab_index) {
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
821 return *browser; 836 return *browser;
822 } 837 }
823 838
824 if (error_message) 839 if (error_message)
825 *error_message= ExtensionErrorUtils::FormatErrorMessage( 840 *error_message= ExtensionErrorUtils::FormatErrorMessage(
826 keys::kWindowNotFoundError, IntToString(window_id)); 841 keys::kWindowNotFoundError, IntToString(window_id));
827 842
828 return NULL; 843 return NULL;
829 } 844 }
830 845
831 static GURL AbsolutePath(Profile* profile, const std::string& extension_id,
832 const std::string& relative_url) {
833 ExtensionsService* service = profile->GetExtensionsService();
834 Extension* extension = service->GetExtensionById(extension_id);
835 return Extension::GetResourceURL(extension->url(), relative_url);
836 }
837
838 static bool GetTabById(int tab_id, Profile* profile, Browser** browser, 846 static bool GetTabById(int tab_id, Profile* profile, Browser** browser,
839 TabStripModel** tab_strip, 847 TabStripModel** tab_strip,
840 TabContents** contents, 848 TabContents** contents,
841 int* tab_index, 849 int* tab_index,
842 std::string* error_message) { 850 std::string* error_message) {
843 if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip, 851 if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip,
844 contents, tab_index)) 852 contents, tab_index))
845 return true; 853 return true;
846 854
847 if (error_message) 855 if (error_message)
848 *error_message = ExtensionErrorUtils::FormatErrorMessage( 856 *error_message = ExtensionErrorUtils::FormatErrorMessage(
849 keys::kTabNotFoundError, IntToString(tab_id)); 857 keys::kTabNotFoundError, IntToString(tab_id));
850 858
851 return false; 859 return false;
852 } 860 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698