15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_tab_util.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/strings/stringprintf.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
10010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "chrome/browser/extensions/chrome_extension_function.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/extensions/chrome_extension_function_details.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/tab_helper.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/window_controller.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/window_controller_list.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
1603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/browser/sessions/session_tab_helper.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_iterator.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/ui/singleton_tabs.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/common/extensions/api/tabs.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
27f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/url_fixer/url_fixer.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/favicon_status.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/navigation_entry.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/app_window.h"
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/app_window_registry.h"
33010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "extensions/common/constants.h"
34010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "extensions/common/error_utils.h"
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/common/feature_switch.h"
37d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "extensions/common/manifest_constants.h"
38010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "extensions/common/manifest_handlers/incognito_info.h"
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/common/manifest_handlers/options_page_info.h"
404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "extensions/common/permissions/api_permission.h"
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/permissions/permissions_data.h"
427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::NavigationEntry;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::WebContents;
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace extensions {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace keys = tabs_constants;
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)WindowController* GetAppWindowController(const WebContents* contents) {
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  AppWindowRegistry* registry = AppWindowRegistry::Get(profile);
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!registry)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AppWindow* app_window =
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      registry->GetAppWindowForRenderViewHost(contents->GetRenderViewHost());
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!app_window)
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return WindowControllerList::GetInstance()->FindWindowById(
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      app_window->session_id().id());
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
66010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// |error_message| can optionally be passed in and will be set with an
67010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// appropriate message if the window cannot be found by id.
68010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)Browser* GetBrowserInProfileWithId(Profile* profile,
69010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                   const int window_id,
70010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                   bool include_incognito,
71010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                   std::string* error_message) {
72010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  Profile* incognito_profile =
73010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      include_incognito && profile->HasOffTheRecordProfile()
74010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          ? profile->GetOffTheRecordProfile()
75010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          : NULL;
76010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  for (chrome::BrowserIterator it; !it.done(); it.Next()) {
77010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Browser* browser = *it;
78010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if ((browser->profile() == profile ||
79010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)         browser->profile() == incognito_profile) &&
80010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        ExtensionTabUtil::GetWindowId(browser) == window_id &&
81010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        browser->window()) {
82010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      return browser;
83010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
84010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
85010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
86010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (error_message)
87010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    *error_message = ErrorUtils::FormatErrorMessage(
88010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        keys::kWindowNotFoundError, base::IntToString(window_id));
89010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
90010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return NULL;
91010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
92010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
93010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)Browser* CreateBrowser(ChromeUIThreadExtensionFunction* function,
94010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                       int window_id,
95010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                       std::string* error) {
96010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  content::WebContents* web_contents = function->GetAssociatedWebContents();
97010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  chrome::HostDesktopType desktop_type =
98e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch      web_contents && web_contents->GetNativeView()
99e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch          ? chrome::GetHostDesktopTypeForNativeView(
100e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch                web_contents->GetNativeView())
101e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch          : chrome::GetHostDesktopTypeForNativeView(NULL);
102010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  Browser::CreateParams params(
103010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      Browser::TYPE_TABBED, function->GetProfile(), desktop_type);
104010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  Browser* browser = new Browser(params);
105010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  browser->window()->Show();
106010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return browser;
107010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
108010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
111010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ExtensionTabUtil::OpenTabParams::OpenTabParams()
112010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    : create_browser_if_needed(false) {
113010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
114010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
115010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)ExtensionTabUtil::OpenTabParams::~OpenTabParams() {
116010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
117010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
118010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Opens a new tab for a given extension. Returns NULL and sets |error| if an
119010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// error occurs.
120010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)base::DictionaryValue* ExtensionTabUtil::OpenTab(
121010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    ChromeUIThreadExtensionFunction* function,
122010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    const OpenTabParams& params,
123010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    std::string* error) {
124010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // windowId defaults to "current" window.
125010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  int window_id = extension_misc::kCurrentWindowId;
126010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (params.window_id.get())
127010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    window_id = *params.window_id;
128010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
129010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  Browser* browser = GetBrowserFromWindowID(function, window_id, error);
130010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!browser) {
131010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (!params.create_browser_if_needed) {
132010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      return NULL;
133010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
134010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    browser = CreateBrowser(function, window_id, error);
135010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (!browser)
136010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      return NULL;
137010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
138010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
139010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Ensure the selected browser is tabbed.
140010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!browser->is_type_tabbed() && browser->IsAttemptingToCloseBrowser())
141010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    browser = chrome::FindTabbedBrowser(function->GetProfile(),
142010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                        function->include_incognito(),
143010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                        browser->host_desktop_type());
144010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
145010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!browser || !browser->window()) {
146010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // TODO(rpaquay): Error message?
147010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return NULL;
148010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
149010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
150010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // TODO(jstritar): Add a constant, chrome.tabs.TAB_ID_ACTIVE, that
151010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // represents the active tab.
152010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  WebContents* opener = NULL;
153010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (params.opener_tab_id.get()) {
154010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    int opener_id = *params.opener_tab_id;
155010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
156010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (!ExtensionTabUtil::GetTabById(opener_id,
157010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                      function->GetProfile(),
158010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                      function->include_incognito(),
159010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                      NULL,
160010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                      NULL,
161010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                      &opener,
162010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                      NULL)) {
163010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      // TODO(rpaquay): Error message?
164010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      return NULL;
165010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
166010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
167010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
168010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // TODO(rafaelw): handle setting remaining tab properties:
169010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // -title
170010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // -favIconUrl
171010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
172010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  GURL url;
173010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (params.url.get()) {
174cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    std::string url_string= *params.url;
1755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    url = ExtensionTabUtil::ResolvePossiblyRelativeURL(url_string,
1765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                                       function->extension());
177010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (!url.is_valid()) {
178010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      *error =
179010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          ErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, url_string);
180010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      return NULL;
181010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
182cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  } else {
183cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    url = GURL(chrome::kChromeUINewTabURL);
184010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
185010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
186010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Don't let extensions crash the browser or renderers.
187010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (ExtensionTabUtil::IsCrashURL(url)) {
188010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    *error = keys::kNoCrashBrowserError;
189010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return NULL;
190010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
191010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
192010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Default to foreground for the new tab. The presence of 'active' property
193010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // will override this default.
194010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  bool active = true;
195010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (params.active.get())
196010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    active = *params.active;
197010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
198010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Default to not pinning the tab. Setting the 'pinned' property to true
199010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // will override this default.
200010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  bool pinned = false;
201010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (params.pinned.get())
202010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    pinned = *params.pinned;
203010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
204010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // We can't load extension URLs into incognito windows unless the extension
205010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // uses split mode. Special case to fall back to a tabbed window.
206010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (url.SchemeIs(kExtensionScheme) &&
2075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      !IncognitoInfo::IsSplitMode(function->extension()) &&
208010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      browser->profile()->IsOffTheRecord()) {
209010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Profile* profile = browser->profile()->GetOriginalProfile();
210010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    chrome::HostDesktopType desktop_type = browser->host_desktop_type();
211010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
212010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    browser = chrome::FindTabbedBrowser(profile, false, desktop_type);
213010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (!browser) {
214010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      browser = new Browser(
215010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          Browser::CreateParams(Browser::TYPE_TABBED, profile, desktop_type));
216010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      browser->window()->Show();
217010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
218010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
219010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
220010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // If index is specified, honor the value, but keep it bound to
221010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // -1 <= index <= tab_strip->count() where -1 invokes the default behavior.
222010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  int index = -1;
223010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (params.index.get())
224010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    index = *params.index;
225010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
226010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  TabStripModel* tab_strip = browser->tab_strip_model();
227010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
228010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  index = std::min(std::max(index, -1), tab_strip->count());
229010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
230010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  int add_types = active ? TabStripModel::ADD_ACTIVE : TabStripModel::ADD_NONE;
231010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  add_types |= TabStripModel::ADD_FORCE_INDEX;
232010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (pinned)
233010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    add_types |= TabStripModel::ADD_PINNED;
234010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  chrome::NavigateParams navigate_params(
2351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      browser, url, ui::PAGE_TRANSITION_LINK);
236010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  navigate_params.disposition =
237010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      active ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB;
238010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  navigate_params.tabstrip_index = index;
239010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  navigate_params.tabstrip_add_types = add_types;
240010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  chrome::Navigate(&navigate_params);
241010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
242010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // The tab may have been created in a different window, so make sure we look
243010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // at the right tab strip.
244010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  tab_strip = navigate_params.browser->tab_strip_model();
245010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  int new_index =
246010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      tab_strip->GetIndexOfWebContents(navigate_params.target_contents);
247010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (opener)
248010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    tab_strip->SetOpenerOfWebContentsAt(new_index, opener);
249010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
250010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (active)
251010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    navigate_params.target_contents->SetInitialFocus();
252010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
253010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Return data about the newly created tab.
254010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return ExtensionTabUtil::CreateTabValue(navigate_params.target_contents,
255010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                          tab_strip,
256010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                          new_index,
2575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                          function->extension());
258010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
259010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
260010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)Browser* ExtensionTabUtil::GetBrowserFromWindowID(
261010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    ChromeUIThreadExtensionFunction* function,
262010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    int window_id,
263010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    std::string* error) {
264010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (window_id == extension_misc::kCurrentWindowId) {
265010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Browser* result = function->GetCurrentBrowser();
266010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (!result || !result->window()) {
267010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      if (error)
268010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        *error = keys::kNoCurrentWindowError;
269010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      return NULL;
270010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
271010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return result;
272010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  } else {
273010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return GetBrowserInProfileWithId(function->GetProfile(),
274010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                     window_id,
275010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                     function->include_incognito(),
276010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                     error);
277010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
278010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
279010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
2801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciBrowser* ExtensionTabUtil::GetBrowserFromWindowID(
2811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const ChromeExtensionFunctionDetails& details,
2821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    int window_id,
2831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    std::string* error) {
2841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (window_id == extension_misc::kCurrentWindowId) {
2851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    Browser* result = details.GetCurrentBrowser();
2861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (!result || !result->window()) {
2871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      if (error)
2881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        *error = keys::kNoCurrentWindowError;
2891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return NULL;
2901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
2911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return result;
2921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  } else {
2931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return GetBrowserInProfileWithId(details.GetProfile(),
2941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                     window_id,
2951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                     details.function()->include_incognito(),
2961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                     error);
2971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
2981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
2991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int ExtensionTabUtil::GetWindowId(const Browser* browser) {
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return browser->session_id().id();
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int ExtensionTabUtil::GetWindowIdOfTabStripModel(
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const TabStripModel* tab_strip_model) {
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (chrome::BrowserIterator it; !it.done(); it.Next()) {
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (it->tab_strip_model() == tab_strip_model)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return GetWindowId(*it);
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return -1;
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
313cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)int ExtensionTabUtil::GetTabId(const WebContents* web_contents) {
31403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  return SessionTabHelper::IdForTab(web_contents);
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) {
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return is_loading ? keys::kStatusValueLoading : keys::kStatusValueComplete;
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) {
32203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  return SessionTabHelper::IdForWindowContainingTab(web_contents);
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::DictionaryValue* ExtensionTabUtil::CreateTabValue(
326010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    WebContents* contents,
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TabStripModel* tab_strip,
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int tab_index,
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Extension* extension) {
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If we have a matching AppWindow with a controller, get the tab value
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // from its controller instead.
3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WindowController* controller = GetAppWindowController(contents);
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (controller &&
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      (!extension || controller->IsVisibleToExtension(extension))) {
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return controller->CreateTabValue(extension, tab_index);
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::DictionaryValue* result =
3385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      CreateTabValue(contents, tab_strip, tab_index);
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ScrubTabValueForExtension(contents, extension, result);
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return result;
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
343eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbase::ListValue* ExtensionTabUtil::CreateTabList(
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Browser* browser,
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Extension* extension) {
346eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  base::ListValue* tab_list = new base::ListValue();
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TabStripModel* tab_strip = browser->tab_strip_model();
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (int i = 0; i < tab_strip->count(); ++i) {
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    tab_list->Append(CreateTabValue(tab_strip->GetWebContentsAt(i),
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    tab_strip,
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    i,
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    extension));
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return tab_list;
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::DictionaryValue* ExtensionTabUtil::CreateTabValue(
359010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    WebContents* contents,
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TabStripModel* tab_strip,
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int tab_index) {
3625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If we have a matching AppWindow with a controller, get the tab value
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // from its controller instead.
3645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WindowController* controller = GetAppWindowController(contents);
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (controller)
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return controller->CreateTabValue(NULL, tab_index);
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!tab_strip)
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index);
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::DictionaryValue* result = new base::DictionaryValue();
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_loading = contents->IsLoading();
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  result->SetInteger(keys::kIdKey, GetTabId(contents));
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  result->SetInteger(keys::kIndexKey, tab_index);
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  result->SetInteger(keys::kWindowIdKey, GetWindowIdOfTab(contents));
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  result->SetString(keys::kStatusKey, GetTabStatusText(is_loading));
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  result->SetBoolean(keys::kActiveKey,
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     tab_strip && tab_index == tab_strip->active_index());
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  result->SetBoolean(keys::kSelectedKey,
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     tab_strip && tab_index == tab_strip->active_index());
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  result->SetBoolean(keys::kHighlightedKey,
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   tab_strip && tab_strip->IsTabSelected(tab_index));
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  result->SetBoolean(keys::kPinnedKey,
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     tab_strip && tab_strip->IsTabPinned(tab_index));
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  result->SetBoolean(keys::kIncognitoKey,
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     contents->GetBrowserContext()->IsOffTheRecord());
3873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  result->SetInteger(keys::kWidthKey,
388010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                     contents->GetContainerBounds().size().width());
3893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  result->SetInteger(keys::kHeightKey,
390010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                     contents->GetContainerBounds().size().height());
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Privacy-sensitive fields: these should be stripped off by
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ScrubTabValueForExtension if the extension should not see them.
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  result->SetString(keys::kUrlKey, contents->GetURL().spec());
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  result->SetString(keys::kTitleKey, contents->GetTitle());
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!is_loading) {
3973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    NavigationEntry* entry = contents->GetController().GetVisibleEntry();
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (entry && entry->GetFavicon().valid)
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      result->SetString(keys::kFaviconUrlKey, entry->GetFavicon().url.spec());
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (tab_strip) {
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebContents* opener = tab_strip->GetOpenerOfWebContentsAt(tab_index);
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (opener)
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      result->SetInteger(keys::kOpenerTabIdKey, GetTabId(opener));
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ExtensionTabUtil::ScrubTabValueForExtension(
412010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    WebContents* contents,
4135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const Extension* extension,
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DictionaryValue* tab_info) {
41546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool has_permission = extension &&
41646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                        extension->permissions_data()->HasAPIPermissionForTab(
41746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                            GetTabId(contents), APIPermission::kTab);
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!has_permission) {
4202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    tab_info->Remove(keys::kUrlKey, NULL);
4212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    tab_info->Remove(keys::kTitleKey, NULL);
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    tab_info->Remove(keys::kFaviconUrlKey, NULL);
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ExtensionTabUtil::ScrubTabForExtension(const Extension* extension,
427f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                            api::tabs::Tab* tab) {
42846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool has_permission =
42946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      extension &&
43046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      extension->permissions_data()->HasAPIPermission(APIPermission::kTab);
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!has_permission) {
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    tab->url.reset();
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    tab->title.reset();
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    tab->fav_icon_url.reset();
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents,
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        TabStripModel** tab_strip_model,
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        int* tab_index) {
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(web_contents);
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(tab_strip_model);
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(tab_index);
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (chrome::BrowserIterator it; !it.done(); it.Next()) {
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TabStripModel* tab_strip = it->tab_strip_model();
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int index = tab_strip->GetIndexOfWebContents(web_contents);
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (index != -1) {
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *tab_strip_model = tab_strip;
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *tab_index = index;
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return true;
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ExtensionTabUtil::GetDefaultTab(Browser* browser,
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     WebContents** contents,
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     int* tab_id) {
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(browser);
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(contents);
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  *contents = browser->tab_strip_model()->GetActiveWebContents();
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (*contents) {
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (tab_id)
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *tab_id = GetTabId(*contents);
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ExtensionTabUtil::GetTabById(int tab_id,
4761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                  content::BrowserContext* browser_context,
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  bool include_incognito,
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  Browser** browser,
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  TabStripModel** tab_strip,
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  WebContents** contents,
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  int* tab_index) {
4821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Profile* profile = Profile::FromBrowserContext(browser_context);
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* incognito_profile =
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      include_incognito && profile->HasOffTheRecordProfile() ?
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          profile->GetOffTheRecordProfile() : NULL;
4862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (chrome::BrowserIterator it; !it.done(); it.Next()) {
4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Browser* target_browser = *it;
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (target_browser->profile() == profile ||
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        target_browser->profile() == incognito_profile) {
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      TabStripModel* target_tab_strip = target_browser->tab_strip_model();
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (int i = 0; i < target_tab_strip->count(); ++i) {
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        WebContents* target_contents = target_tab_strip->GetWebContentsAt(i);
49303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        if (SessionTabHelper::IdForTab(target_contents) == tab_id) {
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (browser)
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            *browser = target_browser;
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (tab_strip)
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            *tab_strip = target_tab_strip;
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (contents)
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            *contents = target_contents;
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (tab_index)
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            *tab_index = i;
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          return true;
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL ExtensionTabUtil::ResolvePossiblyRelativeURL(const std::string& url_string,
511f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                                  const Extension* extension) {
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL url = GURL(url_string);
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!url.is_valid())
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url = extension->GetResourceURL(url_string);
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return url;
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ExtensionTabUtil::IsCrashURL(const GURL& url) {
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check a fixed-up URL, to normalize the scheme and parse hosts correctly.
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL fixed_url =
522f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      url_fixer::FixupURL(url.possibly_invalid_spec(), std::string());
5235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return (fixed_url.SchemeIs(content::kChromeUIScheme) &&
524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          (fixed_url.host() == content::kChromeUIBrowserCrashHost ||
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           fixed_url.host() == chrome::kChromeUICrashHost));
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionTabUtil::CreateTab(WebContents* web_contents,
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 const std::string& extension_id,
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 WindowOpenDisposition disposition,
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 const gfx::Rect& initial_pos,
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 bool user_gesture) {
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile =
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Profile::FromBrowserContext(web_contents->GetBrowserContext());
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  chrome::HostDesktopType active_desktop = chrome::GetActiveDesktop();
5362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindTabbedBrowser(profile, false, active_desktop);
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const bool browser_created = !browser;
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!browser)
5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    browser = new Browser(Browser::CreateParams(profile, active_desktop));
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  chrome::NavigateParams params(browser, web_contents);
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The extension_app_id parameter ends up as app_name in the Browser
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // which causes the Browser to return true for is_app().  This affects
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // among other things, whether the location bar gets displayed.
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(mpcomplete): This seems wrong. What if the extension content is hosted
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in a tab?
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (disposition == NEW_POPUP)
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    params.extension_app_id = extension_id;
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  params.disposition = disposition;
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  params.window_bounds = initial_pos;
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  params.window_action = chrome::NavigateParams::SHOW_WINDOW;
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  params.user_gesture = user_gesture;
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::Navigate(&params);
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Close the browser if chrome::Navigate created a new one.
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (browser_created && (browser != params.browser))
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    browser->window()->Close();
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionTabUtil::ForEachTab(
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const base::Callback<void(WebContents*)>& callback) {
5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (TabContentsIterator iterator; !iterator.done(); iterator.Next())
5652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    callback.Run(*iterator);
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
569f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)WindowController* ExtensionTabUtil::GetWindowControllerOfTab(
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const WebContents* web_contents) {
5712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (browser != NULL)
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return browser->extension_window_controller();
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
577c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
578c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void ExtensionTabUtil::OpenOptionsPage(const Extension* extension,
579c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                       Browser* browser) {
5801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(OptionsPageInfo::HasOptionsPage(extension));
581c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
582c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Force the options page to open in non-OTR window, because it won't be
583c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // able to save settings from OTR.
5840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<chrome::ScopedTabbedBrowserDisplayer> displayer;
585c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (browser->profile()->IsOffTheRecord()) {
5860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    displayer.reset(new chrome::ScopedTabbedBrowserDisplayer(
5870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        browser->profile()->GetOriginalProfile(),
5880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        browser->host_desktop_type()));
5890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    browser = displayer->browser();
590c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
591c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!OptionsPageInfo::ShouldOpenInTab(extension)) {
5931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // If we should embed the options page for this extension, open
5941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // chrome://extensions in a new tab and show the extension options in an
5951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // embedded popup.
5961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    chrome::NavigateParams params(chrome::GetSingletonTabNavigateParams(
5971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        browser, GURL(chrome::kChromeUIExtensionsURL)));
5981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE;
5991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    GURL::Replacements replacements;
6011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    std::string query =
6021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        base::StringPrintf("options=%s", extension->id().c_str());
6031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    replacements.SetQueryStr(query);
6041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    params.url = params.url.ReplaceComponents(replacements);
6051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    chrome::ShowSingletonTabOverwritingNTP(browser, params);
6071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  } else {
6081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // Otherwise open a new tab with the extension's options page
6091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    content::OpenURLParams params(OptionsPageInfo::GetOptionsPage(extension),
6101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                  content::Referrer(),
6111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                  SINGLETON_TAB,
6121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                  ui::PAGE_TRANSITION_LINK,
6131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                  false);
6141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    browser->OpenURL(params);
6151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    browser->window()->Show();
6161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    WebContents* web_contents =
6171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        browser->tab_strip_model()->GetActiveWebContents();
6181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    web_contents->GetDelegate()->ActivateContents(web_contents);
6191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
620c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
621f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
622f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace extensions
623