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

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

Issue 150062: [chromium-reviews] Add getLanguage function to tabs extension (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 5 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) 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/string_util.h" 7 #include "base/string_util.h"
8 #include "chrome/browser/browser.h" 8 #include "chrome/browser/browser.h"
9 #include "chrome/browser/browser_list.h" 9 #include "chrome/browser/browser_list.h"
10 #include "chrome/browser/browser_window.h" 10 #include "chrome/browser/browser_window.h"
(...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 608
609 Browser* browser = NULL; 609 Browser* browser = NULL;
610 TabContents* contents = NULL; 610 TabContents* contents = NULL;
611 if (!GetTabById(tab_id, profile(), &browser, NULL, &contents, NULL, &error_)) 611 if (!GetTabById(tab_id, profile(), &browser, NULL, &contents, NULL, &error_))
612 return false; 612 return false;
613 613
614 browser->CloseTabContents(contents); 614 browser->CloseTabContents(contents);
615 return true; 615 return true;
616 } 616 }
617 617
618 bool GetTabLanguageFunction::RunImpl() {
619 int tab_id = 0;
620 Browser* browser = NULL;
621 TabContents* contents = NULL;
622
623 // If |tab_id| is specified, look for it. Otherwise default to selected tab
624 // in the current window.
625 if (!args_->IsType(Value::TYPE_NULL)) {
626 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id));
627 if (!GetTabById(tab_id, profile(), &browser, NULL, &contents, NULL,
628 &error_)) {
629 return false;
630 }
631 if (!browser || !contents)
632 return false;
633 } else {
634 browser = dispatcher()->GetBrowser();
635 if (!browser)
636 return false;
637 contents = browser->tabstrip_model()->GetSelectedTabContents();
638 if (!contents)
639 return false;
640 }
641
642 // Figure out what language |contents| contains. This sends an async call via
643 // the browser to the renderer to determine the language of the tab the
644 // renderer has. The renderer sends back the language of the tab after the
645 // tab loads (it may be delayed) to the browser, which in turn notifies this
646 // object that the language has been received.
647 contents->GetPageLanguage();
648 registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED,
649 NotificationService::AllSources());
650 AddRef(); // balanced in Observe()
651 return true;
652 }
653
654 void GetTabLanguageFunction::Observe(NotificationType type,
655 const NotificationSource& source,
656 const NotificationDetails& details) {
657 DCHECK(type == NotificationType::TAB_LANGUAGE_DETERMINED);
658 std::string language(*Details<std::string>(details).ptr());
659 result_.reset(Value::CreateStringValue(language.c_str()));
660 SendResponse(true);
661 Release(); // balanced in Run()
662 }
663
618 // static helpers 664 // static helpers
619 665
620 // if |populate| is true, each window gets a list property |tabs| which contains 666 // if |populate| is true, each window gets a list property |tabs| which contains
621 // fully populated tab objects. 667 // fully populated tab objects.
622 static DictionaryValue* CreateWindowValue(Browser* browser, 668 static DictionaryValue* CreateWindowValue(Browser* browser,
623 bool populate_tabs) { 669 bool populate_tabs) {
624 DictionaryValue* result = new DictionaryValue(); 670 DictionaryValue* result = new DictionaryValue();
625 result->SetInteger(keys::kIdKey, ExtensionTabUtil::GetWindowId( 671 result->SetInteger(keys::kIdKey, ExtensionTabUtil::GetWindowId(
626 browser)); 672 browser));
627 result->SetBoolean(keys::kFocusedKey, 673 result->SetBoolean(keys::kFocusedKey,
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
684 if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip, 730 if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip,
685 contents, tab_index)) 731 contents, tab_index))
686 return true; 732 return true;
687 733
688 if (error_message) 734 if (error_message)
689 *error_message = ExtensionErrorUtils::FormatErrorMessage( 735 *error_message = ExtensionErrorUtils::FormatErrorMessage(
690 keys::kTabNotFoundError, IntToString(tab_id)); 736 keys::kTabNotFoundError, IntToString(tab_id));
691 737
692 return false; 738 return false;
693 } 739 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698