13551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Copyright 2013 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)
53551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/shell.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/auto_reset.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
99ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_number_conversions.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
1503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "content/public/browser/devtools_agent_host.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/navigation_controller.h"
17b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_view_host.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/web_contents_observer.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/renderer_preferences.h"
223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/notify_done_forwarder.h"
233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/shell_browser_main_parts.h"
243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/shell_content_browser_client.h"
253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/shell_devtools_frontend.h"
263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/shell_javascript_dialog_manager.h"
273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/webkit_test_controller.h"
28b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "content/shell/common/shell_messages.h"
29b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "content/shell/common/shell_switches.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const int Shell::kDefaultTestWindowWidthDip = 800;
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const int Shell::kDefaultTestWindowHeightDip = 600;
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::vector<Shell*> Shell::windows_;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::Callback<void(Shell*)> Shell::shell_created_callback_;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool Shell::quit_message_loop_ = true;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class Shell::DevToolsWebContentsObserver : public WebContentsObserver {
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DevToolsWebContentsObserver(Shell* shell, WebContents* web_contents)
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      : WebContentsObserver(web_contents),
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        shell_(shell) {
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // WebContentsObserver
49010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  virtual void WebContentsDestroyed() OVERRIDE {
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    shell_->OnDevToolsWebContentsDestroyed();
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private:
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  Shell* shell_;
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DevToolsWebContentsObserver);
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Shell::Shell(WebContents* web_contents)
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    : WebContentsObserver(web_contents),
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      devtools_frontend_(NULL),
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      is_fullscreen_(false),
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      window_(NULL),
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url_edit_view_(NULL),
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      headless_(false) {
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (command_line.HasSwitch(switches::kDumpRenderTree))
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    headless_ = true;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  windows_.push_back(this);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!shell_created_callback_.is_null()) {
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell_created_callback_.Run(this);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell_created_callback_.Reset();
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Shell::~Shell() {
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PlatformCleanUp();
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < windows_.size(); ++i) {
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (windows_[i] == this) {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      windows_.erase(windows_.begin() + i);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (windows_.empty() && quit_message_loop_) {
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (headless_)
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PlatformExit();
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    base::MessageLoop::current()->PostTask(FROM_HERE,
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                           base::MessageLoop::QuitClosure());
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)Shell* Shell::CreateShell(WebContents* web_contents,
96b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                          const gfx::Size& initial_size) {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell* shell = new Shell(web_contents);
98b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  shell->PlatformCreateWindow(initial_size.width(), initial_size.height());
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shell->web_contents_.reset(web_contents);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_contents->SetDelegate(shell);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shell->PlatformSetContents();
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shell->PlatformResizeSubViews();
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    web_contents->GetMutableRendererPrefs()->use_custom_colors = false;
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    web_contents->GetRenderViewHost()->SyncRendererPrefs();
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return shell;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Shell::CloseAllWindows() {
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::AutoReset<bool> auto_reset(&quit_message_loop_, false);
11703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DevToolsAgentHost::DetachAllClients();
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<Shell*> open_windows(windows_);
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < open_windows.size(); ++i)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    open_windows[i]->Close();
121f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  PlatformExit();
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MessageLoop::current()->RunUntilIdle();
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Shell::SetShellCreatedCallback(
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::Callback<void(Shell*)> shell_created_callback) {
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(shell_created_callback_.is_null());
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shell_created_callback_ = shell_created_callback;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Shell* Shell::FromRenderViewHost(RenderViewHost* rvh) {
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < windows_.size(); ++i) {
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (windows_[i]->web_contents() &&
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        windows_[i]->web_contents()->GetRenderViewHost() == rvh) {
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return windows_[i];
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Shell::Initialize() {
14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  PlatformInitialize(
14490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      gfx::Size(kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip));
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)gfx::Size Shell::AdjustWindowSize(const gfx::Size& initial_size) {
1484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!initial_size.IsEmpty())
1494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return initial_size;
1504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return gfx::Size(kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip);
1514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
1524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Shell* Shell::CreateNewWindow(BrowserContext* browser_context,
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const GURL& url,
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              SiteInstance* site_instance,
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              int routing_id,
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              const gfx::Size& initial_size) {
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WebContents::CreateParams create_params(browser_context, site_instance);
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  create_params.routing_id = routing_id;
1604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  create_params.initial_size = AdjustWindowSize(initial_size);
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WebContents* web_contents = WebContents::Create(create_params);
162b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  Shell* shell = CreateShell(web_contents, create_params.initial_size);
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!url.is_empty())
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    shell->LoadURL(url);
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return shell;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Shell::LoadURL(const GURL& url) {
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LoadURLForFrame(url, std::string());
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Shell::LoadURLForFrame(const GURL& url, const std::string& frame_name) {
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NavigationController::LoadURLParams params(url);
1741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  params.transition_type = ui::PageTransitionFromInt(
1751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR);
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  params.frame_name = frame_name;
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  web_contents_->GetController().LoadURLWithParams(params);
178010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  web_contents_->Focus();
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid Shell::LoadDataWithBaseURL(const GURL& url, const std::string& data,
1820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const GURL& base_url) {
1830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  const GURL data_url = GURL("data:text/html;charset=utf-8," + data);
1840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  NavigationController::LoadURLParams params(data_url);
1850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  params.load_type = NavigationController::LOAD_TYPE_DATA;
1860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  params.base_url_for_data_url = base_url;
1870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  params.virtual_url_for_data_url = url;
1880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE;
1890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  web_contents_->GetController().LoadURLWithParams(params);
190010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  web_contents_->Focus();
1910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
1920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void Shell::AddNewContents(WebContents* source,
1944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           WebContents* new_contents,
1954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           WindowOpenDisposition disposition,
1964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           const gfx::Rect& initial_pos,
1974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           bool user_gesture,
1984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           bool* was_blocked) {
1994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  CreateShell(new_contents, AdjustWindowSize(initial_pos.size()));
2004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
2014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    NotifyDoneForwarder::CreateForWebContents(new_contents);
2024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
2034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Shell::GoBackOrForward(int offset) {
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_contents_->GetController().GoToOffset(offset);
206010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  web_contents_->Focus();
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Shell::Reload() {
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_contents_->GetController().Reload(false);
211010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  web_contents_->Focus();
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Shell::Stop() {
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_contents_->Stop();
216010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  web_contents_->Focus();
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
219effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid Shell::UpdateNavigationControls(bool to_different_document) {
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int current_index = web_contents_->GetController().GetCurrentEntryIndex();
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int max_index = web_contents_->GetController().GetEntryCount() - 1;
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PlatformEnableUIControl(BACK_BUTTON, current_index > 0);
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PlatformEnableUIControl(FORWARD_BUTTON, current_index < max_index);
225effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  PlatformEnableUIControl(STOP_BUTTON,
226effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      to_different_document && web_contents_->IsLoading());
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Shell::ShowDevTools() {
2300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  InnerShowDevTools("", "");
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void Shell::ShowDevToolsForElementAt(int x, int y) {
2340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  InnerShowDevTools("", "");
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  devtools_frontend_->InspectElementAt(x, y);
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid Shell::ShowDevToolsForTest(const std::string& settings,
2390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                const std::string& frontend_url) {
2400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  InnerShowDevTools(settings, frontend_url);
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Shell::CloseDevTools() {
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!devtools_frontend_)
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
24690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  devtools_observer_.reset();
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  devtools_frontend_->Close();
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  devtools_frontend_ = NULL;
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)gfx::NativeView Shell::GetContentView() {
252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!web_contents_)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
254010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return web_contents_->GetNativeView();
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebContents* Shell::OpenURLFromTab(WebContents* source,
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const OpenURLParams& params) {
259424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // CURRENT_TAB is the only one we implement for now.
260424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (params.disposition != CURRENT_TAB)
261424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      return NULL;
262c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  NavigationController::LoadURLParams load_url_params(params.url);
263c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  load_url_params.referrer = params.referrer;
264f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  load_url_params.frame_tree_node_id = params.frame_tree_node_id;
265c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  load_url_params.transition_type = params.transition;
266c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  load_url_params.extra_headers = params.extra_headers;
267c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  load_url_params.should_replace_current_entry =
268c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch      params.should_replace_current_entry;
269c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch
270c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  if (params.transferred_global_request_id != GlobalRequestID()) {
271c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    load_url_params.is_renderer_initiated = params.is_renderer_initiated;
272c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    load_url_params.transferred_global_request_id =
273c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch        params.transferred_global_request_id;
274c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  } else if (params.is_renderer_initiated) {
275c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch    load_url_params.is_renderer_initiated = true;
276c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  }
277c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch
278c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  source->GetController().LoadURLWithParams(load_url_params);
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return source;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
282effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid Shell::LoadingStateChanged(WebContents* source,
283effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    bool to_different_document) {
284effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  UpdateNavigationControls(to_different_document);
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PlatformSetIsLoading(source->IsLoading());
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Shell::ToggleFullscreenModeForTab(WebContents* web_contents,
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       bool enter_fullscreen) {
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_ANDROID)
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PlatformToggleFullscreenModeForTab(web_contents, enter_fullscreen);
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (is_fullscreen_ != enter_fullscreen) {
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    is_fullscreen_ = enter_fullscreen;
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    web_contents->GetRenderViewHost()->WasResized();
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) const {
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_ANDROID)
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PlatformIsFullscreenForTabOrPending(web_contents);
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else
3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return is_fullscreen_;
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Shell::RequestToLockMouse(WebContents* web_contents,
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               bool user_gesture,
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               bool last_unlocked_by_target) {
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  web_contents->GotResponseToLockMouseRequest(true);
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Shell::CloseContents(WebContents* source) {
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Close();
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool Shell::CanOverscrollContent() const {
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(USE_AURA)
3212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Shell::DidNavigateMainFramePostCommit(WebContents* web_contents) {
3283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  PlatformSetAddressBarURL(web_contents->GetLastCommittedURL());
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)JavaScriptDialogManager* Shell::GetJavaScriptDialogManager() {
332c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!dialog_manager_)
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    dialog_manager_.reset(new ShellJavaScriptDialogManager());
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return dialog_manager_.get();
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool Shell::AddMessageToConsole(WebContents* source,
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                int32 level,
339a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                const base::string16& message,
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                int32 line_no,
341a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                const base::string16& source_id) {
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree);
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void Shell::RendererUnresponsive(WebContents* source) {
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebKitTestController::Get()->RendererUnresponsive();
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Shell::ActivateContents(WebContents* contents) {
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  contents->GetRenderViewHost()->Focus();
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void Shell::DeactivateContents(WebContents* contents) {
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  contents->GetRenderViewHost()->Blur();
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
359a3f7b4e666c476898878fa745f637129375cd889Ben Murdochvoid Shell::WorkerCrashed(WebContents* source) {
360a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
361a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    return;
362a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  WebKitTestController::Get()->WorkerCrashed();
363a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch}
364a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
3655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool Shell::HandleContextMenu(const content::ContextMenuParams& params) {
3665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return PlatformHandleContextMenu(params);
3675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void Shell::WebContentsFocused(WebContents* contents) {
3705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(TOOLKIT_VIEWS)
3715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PlatformWebContentsFocused(contents);
3725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void Shell::TitleWasSet(NavigationEntry* entry, bool explicit_set) {
3764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (entry)
3774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    PlatformSetTitle(entry->GetTitle());
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid Shell::InnerShowDevTools(const std::string& settings,
3810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                              const std::string& frontend_url) {
3825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!devtools_frontend_) {
3830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    devtools_frontend_ = ShellDevToolsFrontend::Show(
3840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        web_contents(), settings, frontend_url);
3855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    devtools_observer_.reset(new DevToolsWebContentsObserver(
3865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        this, devtools_frontend_->frontend_shell()->web_contents()));
3875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  devtools_frontend_->Activate();
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  devtools_frontend_->Focus();
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
39390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void Shell::OnDevToolsWebContentsDestroyed() {
39490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  devtools_observer_.reset();
39590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  devtools_frontend_ = NULL;
39690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
39790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
399