Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / tabs / tabs_api.cc
index 39607d2..d0ca7fe 100644 (file)
@@ -50,9 +50,6 @@
 #include "chrome/common/extensions/api/tabs.h"
 #include "chrome/common/extensions/api/windows.h"
 #include "chrome/common/extensions/extension_constants.h"
-#include "chrome/common/extensions/extension_file_util.h"
-#include "chrome/common/extensions/extension_l10n_util.h"
-#include "chrome/common/extensions/message_bundle.h"
 #include "chrome/common/pref_names.h"
 #include "chrome/common/url_constants.h"
 #include "components/translate/core/common/language_detection_details.h"
 #include "content/public/browser/render_view_host.h"
 #include "content/public/browser/render_widget_host_view.h"
 #include "content/public/browser/web_contents.h"
-#include "content/public/browser/web_contents_view.h"
 #include "content/public/common/url_constants.h"
 #include "extensions/browser/extension_function_dispatcher.h"
 #include "extensions/browser/extension_function_util.h"
 #include "extensions/browser/extension_host.h"
 #include "extensions/browser/file_reader.h"
-#include "extensions/common/constants.h"
 #include "extensions/common/error_utils.h"
 #include "extensions/common/extension.h"
+#include "extensions/common/extension_l10n_util.h"
 #include "extensions/common/extension_messages.h"
 #include "extensions/common/manifest_constants.h"
-#include "extensions/common/manifest_handlers/incognito_info.h"
+#include "extensions/common/message_bundle.h"
 #include "extensions/common/permissions/permissions_data.h"
 #include "extensions/common/user_script.h"
 #include "skia/ext/image_operations.h"
 #include "ui/base/models/list_selection_model.h"
 #include "ui/base/ui_base_types.h"
 
-#if defined(OS_WIN)
-#include "win8/util/win8_util.h"
-#endif  // OS_WIN
-
 #if defined(USE_ASH)
 #include "apps/app_window_registry.h"
 #include "ash/ash_switches.h"
@@ -113,52 +105,19 @@ using api::tabs::InjectDetails;
 
 namespace {
 
-// |error_message| can optionally be passed in a will be set with an appropriate
-// message if the window cannot be found by id.
-Browser* GetBrowserInProfileWithId(Profile* profile,
-                                   const int window_id,
-                                   bool include_incognito,
-                                   std::string* error_message) {
-  Profile* incognito_profile =
-      include_incognito && profile->HasOffTheRecordProfile() ?
-          profile->GetOffTheRecordProfile() : NULL;
-  for (chrome::BrowserIterator it; !it.done(); it.Next()) {
-    Browser* browser = *it;
-    if ((browser->profile() == profile ||
-         browser->profile() == incognito_profile) &&
-        ExtensionTabUtil::GetWindowId(browser) == window_id &&
-        browser->window()) {
-      return browser;
-    }
-  }
-
-  if (error_message)
-    *error_message = ErrorUtils::FormatErrorMessage(
-        keys::kWindowNotFoundError, base::IntToString(window_id));
-
-  return NULL;
-}
-
-bool GetBrowserFromWindowID(ChromeAsyncExtensionFunction* function,
+bool GetBrowserFromWindowID(ChromeUIThreadExtensionFunction* function,
                             int window_id,
                             Browser** browser) {
-  if (window_id == extension_misc::kCurrentWindowId) {
-    *browser = function->GetCurrentBrowser();
-    if (!(*browser) || !(*browser)->window()) {
-      function->SetError(keys::kNoCurrentWindowError);
-      return false;
-    }
-  } else {
-    std::string error;
-    *browser = GetBrowserInProfileWithId(function->GetProfile(),
-                                         window_id,
-                                         function->include_incognito(),
-                                         &error);
-    if (!*browser) {
-      function->SetError(error);
-      return false;
-    }
+  std::string error;
+  Browser* result;
+  result =
+      ExtensionTabUtil::GetBrowserFromWindowID(function, window_id, &error);
+  if (!result) {
+    function->SetError(error);
+    return false;
   }
+
+  *browser = result;
   return true;
 }
 
@@ -190,34 +149,19 @@ bool MatchesBool(bool* boolean, bool value) {
   return !boolean || *boolean == value;
 }
 
-Browser* CreateBrowserWindow(const Browser::CreateParams& params,
-                             Profile* profile,
-                             const std::string& extension_id) {
-  bool use_existing_browser_window = false;
-
-#if defined(OS_WIN)
-  // In windows 8 metro mode we don't allow windows to be created.
-  if (win8::IsSingleWindowMetroMode())
-    use_existing_browser_window = true;
-#endif  // OS_WIN
-
-  Browser* new_window = NULL;
-  if (use_existing_browser_window)
-    // The false parameter passed below is to ensure that we find a browser
-    // object matching the profile passed in, instead of the original profile
-    new_window = chrome::FindTabbedBrowser(profile, false,
-                                           params.host_desktop_type);
-
-  if (!new_window)
-    new_window = new Browser(params);
-  return new_window;
+template <typename T>
+void AssignOptionalValue(const scoped_ptr<T>& source,
+                         scoped_ptr<T>& destination) {
+  if (source.get()) {
+    destination.reset(new T(*source.get()));
+  }
 }
 
 }  // namespace
 
 // Windows ---------------------------------------------------------------------
 
-bool WindowsGetFunction::RunImpl() {
+bool WindowsGetFunction::RunSync() {
   scoped_ptr<windows::Get::Params> params(windows::Get::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
 
@@ -239,7 +183,7 @@ bool WindowsGetFunction::RunImpl() {
   return true;
 }
 
-bool WindowsGetCurrentFunction::RunImpl() {
+bool WindowsGetCurrentFunction::RunSync() {
   scoped_ptr<windows::GetCurrent::Params> params(
       windows::GetCurrent::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
@@ -261,7 +205,7 @@ bool WindowsGetCurrentFunction::RunImpl() {
   return true;
 }
 
-bool WindowsGetLastFocusedFunction::RunImpl() {
+bool WindowsGetLastFocusedFunction::RunSync() {
   scoped_ptr<windows::GetLastFocused::Params> params(
       windows::GetLastFocused::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
@@ -288,7 +232,7 @@ bool WindowsGetLastFocusedFunction::RunImpl() {
   return true;
 }
 
-bool WindowsGetAllFunction::RunImpl() {
+bool WindowsGetAllFunction::RunSync() {
   scoped_ptr<windows::GetAll::Params> params(
       windows::GetAll::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
@@ -362,7 +306,7 @@ bool WindowsCreateFunction::ShouldOpenIncognitoWindow(
   return incognito;
 }
 
-bool WindowsCreateFunction::RunImpl() {
+bool WindowsCreateFunction::RunSync() {
   scoped_ptr<windows::Create::Params> params(
       windows::Create::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -564,8 +508,8 @@ bool WindowsCreateFunction::RunImpl() {
     create_params.initial_bounds = window_bounds;
   } else {
     create_params = Browser::CreateParams::CreateForApp(
-        window_type,
         web_app::GenerateApplicationNameFromExtensionId(extension_id),
+        false /* trusted_source */,
         window_bounds,
         window_profile,
         host_desktop_type);
@@ -573,8 +517,7 @@ bool WindowsCreateFunction::RunImpl() {
   create_params.initial_show_state = ui::SHOW_STATE_NORMAL;
   create_params.host_desktop_type = chrome::GetActiveDesktop();
 
-  Browser* new_window = CreateBrowserWindow(create_params, window_profile,
-                                            extension_id);
+  Browser* new_window = new Browser(create_params);
 
   for (std::vector<GURL>::iterator i = urls.begin(); i != urls.end(); ++i) {
     WebContents* tab = chrome::AddSelectedTabWithURL(
@@ -627,7 +570,7 @@ bool WindowsCreateFunction::RunImpl() {
   return true;
 }
 
-bool WindowsUpdateFunction::RunImpl() {
+bool WindowsUpdateFunction::RunSync() {
   scoped_ptr<windows::Update::Params> params(
       windows::Update::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -637,14 +580,6 @@ bool WindowsUpdateFunction::RunImpl() {
                                             &controller))
     return false;
 
-#if defined(OS_WIN)
-  // Silently ignore changes on the window for metro mode.
-  if (win8::IsSingleWindowMetroMode()) {
-    SetResult(controller->CreateWindowValue());
-    return true;
-  }
-#endif
-
   ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT;  // No change.
   switch (params->update_info.state) {
     case windows::Update::Params::UpdateInfo::STATE_NORMAL:
@@ -755,7 +690,7 @@ bool WindowsUpdateFunction::RunImpl() {
   return true;
 }
 
-bool WindowsRemoveFunction::RunImpl() {
+bool WindowsRemoveFunction::RunSync() {
   scoped_ptr<windows::Remove::Params> params(
       windows::Remove::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params);
@@ -765,13 +700,6 @@ bool WindowsRemoveFunction::RunImpl() {
                                            &controller))
     return false;
 
-#if defined(OS_WIN)
-  // In Windows 8 metro mode, an existing Browser instance is reused for
-  // hosting the extension tab. We should not be closing it as we don't own it.
-  if (win8::IsSingleWindowMetroMode())
-    return false;
-#endif
-
   WindowController::Reason reason;
   if (!controller->CanClose(&reason)) {
     if (reason == WindowController::REASON_NOT_EDITABLE)
@@ -784,7 +712,7 @@ bool WindowsRemoveFunction::RunImpl() {
 
 // Tabs ------------------------------------------------------------------------
 
-bool TabsGetSelectedFunction::RunImpl() {
+bool TabsGetSelectedFunction::RunSync() {
   // windowId defaults to "current" window.
   int window_id = extension_misc::kCurrentWindowId;
 
@@ -811,7 +739,7 @@ bool TabsGetSelectedFunction::RunImpl() {
   return true;
 }
 
-bool TabsGetAllInWindowFunction::RunImpl() {
+bool TabsGetAllInWindowFunction::RunSync() {
   scoped_ptr<tabs::GetAllInWindow::Params> params(
       tabs::GetAllInWindow::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
@@ -829,7 +757,7 @@ bool TabsGetAllInWindowFunction::RunImpl() {
   return true;
 }
 
-bool TabsQueryFunction::RunImpl() {
+bool TabsQueryFunction::RunSync() {
   scoped_ptr<tabs::Query::Params> params(tabs::Query::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
 
@@ -905,7 +833,7 @@ bool TabsQueryFunction::RunImpl() {
 
     TabStripModel* tab_strip = browser->tab_strip_model();
     for (int i = 0; i < tab_strip->count(); ++i) {
-      const WebContents* web_contents = tab_strip->GetWebContentsAt(i);
+      WebContents* web_contents = tab_strip->GetWebContentsAt(i);
 
       if (index > -1 && i != index)
         continue;
@@ -944,145 +872,37 @@ bool TabsQueryFunction::RunImpl() {
   return true;
 }
 
-bool TabsCreateFunction::RunImpl() {
+bool TabsCreateFunction::RunSync() {
   scoped_ptr<tabs::Create::Params> params(tabs::Create::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
 
-  // windowId defaults to "current" window.
-  int window_id = extension_misc::kCurrentWindowId;
-  if (params->create_properties.window_id.get())
-    window_id = *params->create_properties.window_id;
-
-  Browser* browser = NULL;
-  if (!GetBrowserFromWindowID(this, window_id, &browser))
-    return false;
-
-  // Ensure the selected browser is tabbed.
-  if (!browser->is_type_tabbed() && browser->IsAttemptingToCloseBrowser())
-    browser = chrome::FindTabbedBrowser(
-        GetProfile(), include_incognito(), browser->host_desktop_type());
-
-  if (!browser || !browser->window())
-    return false;
-
-  // TODO(jstritar): Add a constant, chrome.tabs.TAB_ID_ACTIVE, that
-  // represents the active tab.
-  WebContents* opener = NULL;
-  if (params->create_properties.opener_tab_id.get()) {
-    int opener_id = *params->create_properties.opener_tab_id;
-
-    if (!ExtensionTabUtil::GetTabById(opener_id,
-                                      GetProfile(),
-                                      include_incognito(),
-                                      NULL,
-                                      NULL,
-                                      &opener,
-                                      NULL)) {
-      return false;
-    }
-  }
-
-  // TODO(rafaelw): handle setting remaining tab properties:
-  // -title
-  // -favIconUrl
-
-  std::string url_string;
-  GURL url;
-  if (params->create_properties.url.get()) {
-    url_string = *params->create_properties.url;
-    url = ExtensionTabUtil::ResolvePossiblyRelativeURL(url_string,
-                                                       GetExtension());
-    if (!url.is_valid()) {
-      error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidUrlError,
-                                                       url_string);
-      return false;
-    }
-  }
-
-  // Don't let extensions crash the browser or renderers.
-  if (ExtensionTabUtil::IsCrashURL(url)) {
-    error_ = keys::kNoCrashBrowserError;
-    return false;
-  }
-
-  // Default to foreground for the new tab. The presence of 'selected' property
-  // will override this default. This property is deprecated ('active' should
-  // be used instead).
-  bool active = true;
-  if (params->create_properties.selected.get())
-    active = *params->create_properties.selected;
-
+  ExtensionTabUtil::OpenTabParams options;
+  AssignOptionalValue(params->create_properties.window_id, options.window_id);
+  AssignOptionalValue(params->create_properties.opener_tab_id,
+                      options.opener_tab_id);
+  AssignOptionalValue(params->create_properties.selected, options.active);
   // The 'active' property has replaced the 'selected' property.
-  if (params->create_properties.active.get())
-    active = *params->create_properties.active;
-
-  // Default to not pinning the tab. Setting the 'pinned' property to true
-  // will override this default.
-  bool pinned = false;
-  if (params->create_properties.pinned.get())
-    pinned = *params->create_properties.pinned;
-
-  // We can't load extension URLs into incognito windows unless the extension
-  // uses split mode. Special case to fall back to a tabbed window.
-  if (url.SchemeIs(kExtensionScheme) &&
-      !IncognitoInfo::IsSplitMode(GetExtension()) &&
-      browser->profile()->IsOffTheRecord()) {
-    Profile* profile = browser->profile()->GetOriginalProfile();
-    chrome::HostDesktopType desktop_type = browser->host_desktop_type();
-
-    browser = chrome::FindTabbedBrowser(profile, false, desktop_type);
-    if (!browser) {
-      browser = new Browser(Browser::CreateParams(Browser::TYPE_TABBED,
-                                                  profile, desktop_type));
-      browser->window()->Show();
-    }
+  AssignOptionalValue(params->create_properties.active, options.active);
+  AssignOptionalValue(params->create_properties.pinned, options.pinned);
+  AssignOptionalValue(params->create_properties.index, options.index);
+  AssignOptionalValue(params->create_properties.url, options.url);
+
+  std::string error;
+  scoped_ptr<base::DictionaryValue> result(
+      ExtensionTabUtil::OpenTab(this, options, &error));
+  if (!result) {
+    SetError(error);
+    return false;
   }
 
-  // If index is specified, honor the value, but keep it bound to
-  // -1 <= index <= tab_strip->count() where -1 invokes the default behavior.
-  int index = -1;
-  if (params->create_properties.index.get())
-    index = *params->create_properties.index;
-
-  TabStripModel* tab_strip = browser->tab_strip_model();
-
-  index = std::min(std::max(index, -1), tab_strip->count());
-
-  int add_types = active ? TabStripModel::ADD_ACTIVE :
-                             TabStripModel::ADD_NONE;
-  add_types |= TabStripModel::ADD_FORCE_INDEX;
-  if (pinned)
-    add_types |= TabStripModel::ADD_PINNED;
-  chrome::NavigateParams navigate_params(
-      browser, url, content::PAGE_TRANSITION_LINK);
-  navigate_params.disposition =
-      active ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB;
-  navigate_params.tabstrip_index = index;
-  navigate_params.tabstrip_add_types = add_types;
-  chrome::Navigate(&navigate_params);
-
-  // The tab may have been created in a different window, so make sure we look
-  // at the right tab strip.
-  tab_strip = navigate_params.browser->tab_strip_model();
-  int new_index = tab_strip->GetIndexOfWebContents(
-      navigate_params.target_contents);
-  if (opener)
-    tab_strip->SetOpenerOfWebContentsAt(new_index, opener);
-
-  if (active)
-    navigate_params.target_contents->GetView()->SetInitialFocus();
-
   // Return data about the newly created tab.
   if (has_callback()) {
-    SetResult(ExtensionTabUtil::CreateTabValue(
-        navigate_params.target_contents,
-        tab_strip, new_index, GetExtension()));
+    SetResult(result.release());
   }
-
   return true;
 }
 
-bool TabsDuplicateFunction::RunImpl() {
+bool TabsDuplicateFunction::RunSync() {
   scoped_ptr<tabs::Duplicate::Params> params(
       tabs::Duplicate::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
@@ -1125,7 +945,7 @@ bool TabsDuplicateFunction::RunImpl() {
   return true;
 }
 
-bool TabsGetFunction::RunImpl() {
+bool TabsGetFunction::RunSync() {
   scoped_ptr<tabs::Get::Params> params(tabs::Get::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
   int tab_id = params->tab_id;
@@ -1150,7 +970,7 @@ bool TabsGetFunction::RunImpl() {
   return true;
 }
 
-bool TabsGetCurrentFunction::RunImpl() {
+bool TabsGetCurrentFunction::RunSync() {
   DCHECK(dispatcher());
 
   WebContents* contents = dispatcher()->delegate()->GetAssociatedWebContents();
@@ -1160,7 +980,7 @@ bool TabsGetCurrentFunction::RunImpl() {
   return true;
 }
 
-bool TabsHighlightFunction::RunImpl() {
+bool TabsHighlightFunction::RunSync() {
   scoped_ptr<tabs::Highlight::Params> params(
       tabs::Highlight::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
@@ -1231,7 +1051,7 @@ bool TabsHighlightFunction::HighlightTab(TabStripModel* tabstrip,
 TabsUpdateFunction::TabsUpdateFunction() : web_contents_(NULL) {
 }
 
-bool TabsUpdateFunction::RunImpl() {
+bool TabsUpdateFunction::RunAsync() {
   scoped_ptr<tabs::Update::Params> params(tabs::Update::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
 
@@ -1366,19 +1186,19 @@ bool TabsUpdateFunction::UpdateURL(const std::string &url_string,
       return false;
     }
 
-    TabHelper::FromWebContents(web_contents_)->
-        script_executor()->ExecuteScript(
-            extension_id(),
-            ScriptExecutor::JAVASCRIPT,
-            url.GetContent(),
-            ScriptExecutor::TOP_FRAME,
-            UserScript::DOCUMENT_IDLE,
-            ScriptExecutor::MAIN_WORLD,
-            ScriptExecutor::DEFAULT_PROCESS,
-            GURL(),
-            user_gesture_,
-            ScriptExecutor::NO_RESULT,
-            base::Bind(&TabsUpdateFunction::OnExecuteCodeFinished, this));
+    TabHelper::FromWebContents(web_contents_)->script_executor()->ExecuteScript(
+        extension_id(),
+        ScriptExecutor::JAVASCRIPT,
+        url.GetContent(),
+        ScriptExecutor::TOP_FRAME,
+        UserScript::DOCUMENT_IDLE,
+        ScriptExecutor::MAIN_WORLD,
+        ScriptExecutor::DEFAULT_PROCESS,
+        GURL(),
+        GURL(),
+        user_gesture_,
+        ScriptExecutor::NO_RESULT,
+        base::Bind(&TabsUpdateFunction::OnExecuteCodeFinished, this));
 
     *is_async = true;
     return true;
@@ -1414,7 +1234,7 @@ void TabsUpdateFunction::OnExecuteCodeFinished(
   SendResponse(error.empty());
 }
 
-bool TabsMoveFunction::RunImpl() {
+bool TabsMoveFunction::RunSync() {
   scoped_ptr<tabs::Move::Params> params(tabs::Move::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
 
@@ -1561,7 +1381,7 @@ bool TabsMoveFunction::MoveTab(int tab_id,
   return true;
 }
 
-bool TabsReloadFunction::RunImpl() {
+bool TabsReloadFunction::RunSync() {
   scoped_ptr<tabs::Reload::Params> params(
       tabs::Reload::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
@@ -1615,7 +1435,7 @@ bool TabsReloadFunction::RunImpl() {
   return true;
 }
 
-bool TabsRemoveFunction::RunImpl() {
+bool TabsRemoveFunction::RunSync() {
   scoped_ptr<tabs::Remove::Params> params(tabs::Remove::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
 
@@ -1701,7 +1521,7 @@ void TabsCaptureVisibleTabFunction::RegisterProfilePrefs(
       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
 }
 
-bool TabsDetectLanguageFunction::RunImpl() {
+bool TabsDetectLanguageFunction::RunAsync() {
   scoped_ptr<tabs::DetectLanguage::Params> params(
       tabs::DetectLanguage::Params::Create(*args_));
   EXTENSION_FUNCTION_VALIDATE(params.get());
@@ -1865,6 +1685,10 @@ bool ExecuteCodeInTabFunction::IsWebView() const {
   return false;
 }
 
+const GURL& ExecuteCodeInTabFunction::GetWebViewSrc() const {
+  return GURL::EmptyGURL();
+}
+
 bool TabsExecuteScriptFunction::ShouldInsertCSS() const {
   return false;
 }