OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/devtools/devtools_window.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/values.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/chrome_page_zoom.h" |
| 18 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
| 19 #include "chrome/browser/extensions/extension_service.h" |
| 20 #include "chrome/browser/infobars/confirm_infobar_delegate.h" |
| 21 #include "chrome/browser/infobars/infobar_service.h" |
| 22 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/themes/theme_properties.h" |
| 24 #include "chrome/browser/themes/theme_service.h" |
| 25 #include "chrome/browser/themes/theme_service_factory.h" |
| 26 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/browser/ui/browser_iterator.h" |
| 28 #include "chrome/browser/ui/browser_list.h" |
| 29 #include "chrome/browser/ui/browser_window.h" |
| 30 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 31 #include "chrome/common/chrome_switches.h" |
| 32 #include "chrome/common/extensions/manifest_url_handler.h" |
| 33 #include "chrome/common/url_constants.h" |
| 34 #include "components/infobars/core/infobar.h" |
| 35 #include "content/public/browser/devtools_client_host.h" |
| 36 #include "content/public/browser/devtools_manager.h" |
| 37 #include "content/public/browser/favicon_status.h" |
| 38 #include "content/public/browser/navigation_controller.h" |
| 39 #include "content/public/browser/navigation_entry.h" |
| 40 #include "content/public/browser/notification_source.h" |
| 41 #include "content/public/browser/render_frame_host.h" |
| 42 #include "content/public/browser/render_view_host.h" |
| 43 #include "content/public/browser/user_metrics.h" |
| 44 #include "content/public/browser/web_contents.h" |
| 45 #include "content/public/browser/web_contents_observer.h" |
| 46 #include "content/public/browser/web_contents_view.h" |
| 47 #include "content/public/common/page_transition_types.h" |
| 48 #include "content/public/common/renderer_preferences.h" |
| 49 #include "content/public/common/url_constants.h" |
| 50 #include "extensions/browser/extension_system.h" |
| 51 #include "extensions/common/extension_set.h" |
| 52 #include "grit/generated_resources.h" |
| 53 #include "ui/base/l10n/l10n_util.h" |
| 54 |
| 55 using base::DictionaryValue; |
| 56 using content::BrowserThread; |
| 57 |
| 58 namespace { |
| 59 |
| 60 // DevToolsConfirmInfoBarDelegate --------------------------------------------- |
| 61 |
| 62 typedef base::Callback<void(bool)> InfoBarCallback; |
| 63 |
| 64 class DevToolsConfirmInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 65 public: |
| 66 // If |infobar_service| is NULL, runs |callback| with a single argument with |
| 67 // value "false". Otherwise, creates a dev tools confirm infobar and delegate |
| 68 // and adds the infobar to |infobar_service|. |
| 69 static void Create(InfoBarService* infobar_service, |
| 70 const InfoBarCallback& callback, |
| 71 const base::string16& message); |
| 72 |
| 73 private: |
| 74 DevToolsConfirmInfoBarDelegate( |
| 75 const InfoBarCallback& callback, |
| 76 const base::string16& message); |
| 77 virtual ~DevToolsConfirmInfoBarDelegate(); |
| 78 |
| 79 virtual base::string16 GetMessageText() const OVERRIDE; |
| 80 virtual base::string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; |
| 81 virtual bool Accept() OVERRIDE; |
| 82 virtual bool Cancel() OVERRIDE; |
| 83 |
| 84 InfoBarCallback callback_; |
| 85 const base::string16 message_; |
| 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(DevToolsConfirmInfoBarDelegate); |
| 88 }; |
| 89 |
| 90 void DevToolsConfirmInfoBarDelegate::Create( |
| 91 InfoBarService* infobar_service, |
| 92 const InfoBarCallback& callback, |
| 93 const base::string16& message) { |
| 94 if (!infobar_service) { |
| 95 callback.Run(false); |
| 96 return; |
| 97 } |
| 98 |
| 99 infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar( |
| 100 scoped_ptr<ConfirmInfoBarDelegate>( |
| 101 new DevToolsConfirmInfoBarDelegate(callback, message)))); |
| 102 } |
| 103 |
| 104 DevToolsConfirmInfoBarDelegate::DevToolsConfirmInfoBarDelegate( |
| 105 const InfoBarCallback& callback, |
| 106 const base::string16& message) |
| 107 : ConfirmInfoBarDelegate(), |
| 108 callback_(callback), |
| 109 message_(message) { |
| 110 } |
| 111 |
| 112 DevToolsConfirmInfoBarDelegate::~DevToolsConfirmInfoBarDelegate() { |
| 113 if (!callback_.is_null()) |
| 114 callback_.Run(false); |
| 115 } |
| 116 |
| 117 base::string16 DevToolsConfirmInfoBarDelegate::GetMessageText() const { |
| 118 return message_; |
| 119 } |
| 120 |
| 121 base::string16 DevToolsConfirmInfoBarDelegate::GetButtonLabel( |
| 122 InfoBarButton button) const { |
| 123 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? |
| 124 IDS_DEV_TOOLS_CONFIRM_ALLOW_BUTTON : IDS_DEV_TOOLS_CONFIRM_DENY_BUTTON); |
| 125 } |
| 126 |
| 127 bool DevToolsConfirmInfoBarDelegate::Accept() { |
| 128 callback_.Run(true); |
| 129 callback_.Reset(); |
| 130 return true; |
| 131 } |
| 132 |
| 133 bool DevToolsConfirmInfoBarDelegate::Cancel() { |
| 134 callback_.Run(false); |
| 135 callback_.Reset(); |
| 136 return true; |
| 137 } |
| 138 |
| 139 static const char kFrontendHostId[] = "id"; |
| 140 static const char kFrontendHostMethod[] = "method"; |
| 141 static const char kFrontendHostParams[] = "params"; |
| 142 |
| 143 std::string SkColorToRGBAString(SkColor color) { |
| 144 // We avoid StringPrintf because it will use locale specific formatters for |
| 145 // the double (e.g. ',' instead of '.' in German). |
| 146 return "rgba(" + base::IntToString(SkColorGetR(color)) + "," + |
| 147 base::IntToString(SkColorGetG(color)) + "," + |
| 148 base::IntToString(SkColorGetB(color)) + "," + |
| 149 base::DoubleToString(SkColorGetA(color) / 255.0) + ")"; |
| 150 } |
| 151 |
| 152 base::DictionaryValue* CreateFileSystemValue( |
| 153 DevToolsFileHelper::FileSystem file_system) { |
| 154 base::DictionaryValue* file_system_value = new base::DictionaryValue(); |
| 155 file_system_value->SetString("fileSystemName", file_system.file_system_name); |
| 156 file_system_value->SetString("rootURL", file_system.root_url); |
| 157 file_system_value->SetString("fileSystemPath", file_system.file_system_path); |
| 158 return file_system_value; |
| 159 } |
| 160 |
| 161 Browser* FindBrowser(content::WebContents* web_contents) { |
| 162 for (chrome::BrowserIterator it; !it.done(); it.Next()) { |
| 163 int tab_index = it->tab_strip_model()->GetIndexOfWebContents( |
| 164 web_contents); |
| 165 if (tab_index != TabStripModel::kNoTab) |
| 166 return *it; |
| 167 } |
| 168 return NULL; |
| 169 } |
| 170 |
| 171 } // namespace |
| 172 |
| 173 // DevToolsWindowBase::FrontendWebContentsObserver ---------------------------- |
| 174 |
| 175 class DevToolsWindowBase::FrontendWebContentsObserver |
| 176 : public content::WebContentsObserver { |
| 177 public: |
| 178 explicit FrontendWebContentsObserver(DevToolsWindowBase* window); |
| 179 virtual ~FrontendWebContentsObserver(); |
| 180 |
| 181 private: |
| 182 // contents::WebContentsObserver: |
| 183 virtual void AboutToNavigateRenderView( |
| 184 content::RenderViewHost* render_view_host) OVERRIDE; |
| 185 virtual void DocumentOnLoadCompletedInMainFrame(int32 page_id) OVERRIDE; |
| 186 virtual void WebContentsDestroyed(content::WebContents*) OVERRIDE; |
| 187 |
| 188 DevToolsWindowBase* devtools_window_; |
| 189 DISALLOW_COPY_AND_ASSIGN(FrontendWebContentsObserver); |
| 190 }; |
| 191 |
| 192 DevToolsWindowBase::FrontendWebContentsObserver::FrontendWebContentsObserver( |
| 193 DevToolsWindowBase* devtools_window) |
| 194 : WebContentsObserver(devtools_window->web_contents()), |
| 195 devtools_window_(devtools_window) { |
| 196 } |
| 197 |
| 198 void DevToolsWindowBase::FrontendWebContentsObserver::WebContentsDestroyed( |
| 199 content::WebContents* contents) { |
| 200 delete devtools_window_; |
| 201 } |
| 202 |
| 203 DevToolsWindowBase::FrontendWebContentsObserver:: |
| 204 ~FrontendWebContentsObserver() { |
| 205 } |
| 206 |
| 207 void DevToolsWindowBase::FrontendWebContentsObserver::AboutToNavigateRenderView( |
| 208 content::RenderViewHost* render_view_host) { |
| 209 content::DevToolsClientHost::SetupDevToolsFrontendClient(render_view_host); |
| 210 } |
| 211 |
| 212 void DevToolsWindowBase::FrontendWebContentsObserver:: |
| 213 DocumentOnLoadCompletedInMainFrame(int32 page_id) { |
| 214 devtools_window_->DocumentOnLoadCompletedInMainFrame(); |
| 215 } |
| 216 |
| 217 // DevToolsWindowBase --------------------------------------------------------- |
| 218 |
| 219 DevToolsWindowBase::~DevToolsWindowBase() { |
| 220 content::DevToolsManager::GetInstance()->ClientHostClosing( |
| 221 frontend_host_.get()); |
| 222 |
| 223 for (IndexingJobsMap::const_iterator jobs_it(indexing_jobs_.begin()); |
| 224 jobs_it != indexing_jobs_.end(); ++jobs_it) { |
| 225 jobs_it->second->Stop(); |
| 226 } |
| 227 indexing_jobs_.clear(); |
| 228 if (device_listener_enabled_) |
| 229 EnableRemoteDeviceCounter(false); |
| 230 } |
| 231 |
| 232 void DevToolsWindowBase::InspectedContentsClosing() { |
| 233 web_contents_->GetRenderViewHost()->ClosePage(); |
| 234 } |
| 235 |
| 236 DevToolsWindowBase::DevToolsWindowBase(content::WebContents* web_contents, |
| 237 const GURL& url) |
| 238 : profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), |
| 239 web_contents_(web_contents), |
| 240 device_listener_enabled_(false), |
| 241 weak_factory_(this) { |
| 242 frontend_contents_observer_.reset(new FrontendWebContentsObserver(this)); |
| 243 web_contents_->GetMutableRendererPrefs()->can_accept_load_drops = false; |
| 244 |
| 245 web_contents_->GetController().LoadURL( |
| 246 ApplyThemeToURL(url), content::Referrer(), |
| 247 content::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string()); |
| 248 |
| 249 frontend_host_.reset(content::DevToolsClientHost::CreateDevToolsFrontendHost( |
| 250 web_contents_, this)); |
| 251 file_helper_.reset(new DevToolsFileHelper(web_contents_, profile_)); |
| 252 file_system_indexer_ = new DevToolsFileSystemIndexer(); |
| 253 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( |
| 254 web_contents_); |
| 255 |
| 256 // Wipe out page icon so that the default application icon is used. |
| 257 content::NavigationEntry* entry = |
| 258 web_contents_->GetController().GetActiveEntry(); |
| 259 entry->GetFavicon().image = gfx::Image(); |
| 260 entry->GetFavicon().valid = true; |
| 261 |
| 262 // Register on-load actions. |
| 263 registrar_.Add( |
| 264 this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 265 content::Source<ThemeService>( |
| 266 ThemeServiceFactory::GetForProfile(profile_))); |
| 267 |
| 268 embedder_message_dispatcher_.reset( |
| 269 DevToolsEmbedderMessageDispatcher::createForDevToolsFrontend(this)); |
| 270 } |
| 271 |
| 272 GURL DevToolsWindowBase::ApplyThemeToURL(const GURL& base_url) { |
| 273 std::string frontend_url = base_url.spec(); |
| 274 ThemeService* tp = ThemeServiceFactory::GetForProfile(profile_); |
| 275 DCHECK(tp); |
| 276 std::string url_string( |
| 277 frontend_url + |
| 278 ((frontend_url.find("?") == std::string::npos) ? "?" : "&") + |
| 279 "dockSide=undocked" + // TODO(dgozman): remove this support in M38. |
| 280 "&toolbarColor=" + |
| 281 SkColorToRGBAString(tp->GetColor(ThemeProperties::COLOR_TOOLBAR)) + |
| 282 "&textColor=" + |
| 283 SkColorToRGBAString(tp->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT))); |
| 284 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 285 switches::kEnableDevToolsExperiments)) |
| 286 url_string += "&experiments=true"; |
| 287 return GURL(url_string); |
| 288 } |
| 289 |
| 290 void DevToolsWindowBase::Observe(int type, |
| 291 const content::NotificationSource& source, |
| 292 const content::NotificationDetails& details) { |
| 293 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); |
| 294 UpdateTheme(); |
| 295 } |
| 296 |
| 297 void DevToolsWindowBase::DispatchOnEmbedder(const std::string& message) { |
| 298 std::string method; |
| 299 base::ListValue empty_params; |
| 300 base::ListValue* params = &empty_params; |
| 301 |
| 302 base::DictionaryValue* dict = NULL; |
| 303 scoped_ptr<base::Value> parsed_message(base::JSONReader::Read(message)); |
| 304 if (!parsed_message || |
| 305 !parsed_message->GetAsDictionary(&dict) || |
| 306 !dict->GetString(kFrontendHostMethod, &method) || |
| 307 (dict->HasKey(kFrontendHostParams) && |
| 308 !dict->GetList(kFrontendHostParams, ¶ms))) { |
| 309 LOG(ERROR) << "Invalid message was sent to embedder: " << message; |
| 310 return; |
| 311 } |
| 312 |
| 313 int id = 0; |
| 314 dict->GetInteger(kFrontendHostId, &id); |
| 315 |
| 316 std::string error; |
| 317 embedder_message_dispatcher_->Dispatch(method, params, &error); |
| 318 if (id) { |
| 319 scoped_ptr<base::Value> id_value(base::Value::CreateIntegerValue(id)); |
| 320 scoped_ptr<base::Value> error_value(base::Value::CreateStringValue(error)); |
| 321 CallClientFunction("InspectorFrontendAPI.embedderMessageAck", |
| 322 id_value.get(), error_value.get(), NULL); |
| 323 } |
| 324 } |
| 325 |
| 326 void DevToolsWindowBase::ActivateWindow() { |
| 327 web_contents_->GetDelegate()->ActivateContents(web_contents_); |
| 328 web_contents_->GetView()->Focus(); |
| 329 } |
| 330 |
| 331 void DevToolsWindowBase::CloseWindow() { |
| 332 } |
| 333 |
| 334 void DevToolsWindowBase::SetContentsInsets( |
| 335 int top, int left, int bottom, int right) { |
| 336 } |
| 337 |
| 338 void DevToolsWindowBase::SetContentsResizingStrategy( |
| 339 const gfx::Insets& insets, const gfx::Size& min_size) { |
| 340 } |
| 341 |
| 342 void DevToolsWindowBase::MoveWindow(int x, int y) { |
| 343 } |
| 344 |
| 345 void DevToolsWindowBase::SetIsDocked(bool dock_requested) { |
| 346 } |
| 347 |
| 348 void DevToolsWindowBase::InspectElementCompleted() { |
| 349 } |
| 350 |
| 351 void DevToolsWindowBase::OpenInNewTab(const std::string& url) { |
| 352 content::OpenURLParams params( |
| 353 GURL(url), content::Referrer(), NEW_FOREGROUND_TAB, |
| 354 content::PAGE_TRANSITION_LINK, false); |
| 355 Browser* browser = FindBrowser(web_contents_); |
| 356 browser->OpenURL(params); |
| 357 } |
| 358 |
| 359 void DevToolsWindowBase::SaveToFile(const std::string& url, |
| 360 const std::string& content, |
| 361 bool save_as) { |
| 362 file_helper_->Save(url, content, save_as, |
| 363 base::Bind(&DevToolsWindowBase::FileSavedAs, |
| 364 weak_factory_.GetWeakPtr(), url), |
| 365 base::Bind(&DevToolsWindowBase::CanceledFileSaveAs, |
| 366 weak_factory_.GetWeakPtr(), url)); |
| 367 } |
| 368 |
| 369 void DevToolsWindowBase::AppendToFile(const std::string& url, |
| 370 const std::string& content) { |
| 371 file_helper_->Append(url, content, |
| 372 base::Bind(&DevToolsWindowBase::AppendedTo, |
| 373 weak_factory_.GetWeakPtr(), url)); |
| 374 } |
| 375 |
| 376 void DevToolsWindowBase::RequestFileSystems() { |
| 377 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme)); |
| 378 file_helper_->RequestFileSystems(base::Bind( |
| 379 &DevToolsWindowBase::FileSystemsLoaded, weak_factory_.GetWeakPtr())); |
| 380 } |
| 381 |
| 382 void DevToolsWindowBase::AddFileSystem() { |
| 383 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme)); |
| 384 file_helper_->AddFileSystem( |
| 385 base::Bind(&DevToolsWindowBase::FileSystemAdded, |
| 386 weak_factory_.GetWeakPtr()), |
| 387 base::Bind(&DevToolsWindowBase::ShowDevToolsConfirmInfoBar, |
| 388 weak_factory_.GetWeakPtr())); |
| 389 } |
| 390 |
| 391 void DevToolsWindowBase::RemoveFileSystem( |
| 392 const std::string& file_system_path) { |
| 393 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme)); |
| 394 file_helper_->RemoveFileSystem(file_system_path); |
| 395 base::StringValue file_system_path_value(file_system_path); |
| 396 CallClientFunction("InspectorFrontendAPI.fileSystemRemoved", |
| 397 &file_system_path_value, NULL, NULL); |
| 398 } |
| 399 |
| 400 void DevToolsWindowBase::UpgradeDraggedFileSystemPermissions( |
| 401 const std::string& file_system_url) { |
| 402 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme)); |
| 403 file_helper_->UpgradeDraggedFileSystemPermissions( |
| 404 file_system_url, |
| 405 base::Bind(&DevToolsWindowBase::FileSystemAdded, |
| 406 weak_factory_.GetWeakPtr()), |
| 407 base::Bind(&DevToolsWindowBase::ShowDevToolsConfirmInfoBar, |
| 408 weak_factory_.GetWeakPtr())); |
| 409 } |
| 410 |
| 411 void DevToolsWindowBase::IndexPath(int request_id, |
| 412 const std::string& file_system_path) { |
| 413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 414 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme)); |
| 415 if (!file_helper_->IsFileSystemAdded(file_system_path)) { |
| 416 IndexingDone(request_id, file_system_path); |
| 417 return; |
| 418 } |
| 419 indexing_jobs_[request_id] = |
| 420 scoped_refptr<DevToolsFileSystemIndexer::FileSystemIndexingJob>( |
| 421 file_system_indexer_->IndexPath( |
| 422 file_system_path, |
| 423 Bind(&DevToolsWindowBase::IndexingTotalWorkCalculated, |
| 424 weak_factory_.GetWeakPtr(), |
| 425 request_id, |
| 426 file_system_path), |
| 427 Bind(&DevToolsWindowBase::IndexingWorked, |
| 428 weak_factory_.GetWeakPtr(), |
| 429 request_id, |
| 430 file_system_path), |
| 431 Bind(&DevToolsWindowBase::IndexingDone, |
| 432 weak_factory_.GetWeakPtr(), |
| 433 request_id, |
| 434 file_system_path))); |
| 435 } |
| 436 |
| 437 void DevToolsWindowBase::StopIndexing(int request_id) { |
| 438 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 439 IndexingJobsMap::iterator it = indexing_jobs_.find(request_id); |
| 440 if (it == indexing_jobs_.end()) |
| 441 return; |
| 442 it->second->Stop(); |
| 443 indexing_jobs_.erase(it); |
| 444 } |
| 445 |
| 446 void DevToolsWindowBase::SearchInPath(int request_id, |
| 447 const std::string& file_system_path, |
| 448 const std::string& query) { |
| 449 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 450 CHECK(web_contents_->GetURL().SchemeIs(content::kChromeDevToolsScheme)); |
| 451 if (!file_helper_->IsFileSystemAdded(file_system_path)) { |
| 452 SearchCompleted(request_id, file_system_path, std::vector<std::string>()); |
| 453 return; |
| 454 } |
| 455 file_system_indexer_->SearchInPath(file_system_path, |
| 456 query, |
| 457 Bind(&DevToolsWindowBase::SearchCompleted, |
| 458 weak_factory_.GetWeakPtr(), |
| 459 request_id, |
| 460 file_system_path)); |
| 461 } |
| 462 |
| 463 void DevToolsWindowBase::SetWhitelistedShortcuts( |
| 464 const std::string& message) { |
| 465 } |
| 466 |
| 467 void DevToolsWindowBase::ZoomIn() { |
| 468 chrome_page_zoom::Zoom(web_contents(), content::PAGE_ZOOM_IN); |
| 469 } |
| 470 |
| 471 void DevToolsWindowBase::ZoomOut() { |
| 472 chrome_page_zoom::Zoom(web_contents(), content::PAGE_ZOOM_OUT); |
| 473 } |
| 474 |
| 475 void DevToolsWindowBase::ResetZoom() { |
| 476 chrome_page_zoom::Zoom(web_contents(), content::PAGE_ZOOM_RESET); |
| 477 } |
| 478 |
| 479 void DevToolsWindowBase::OpenUrlOnRemoteDeviceAndInspect( |
| 480 const std::string& browser_id, |
| 481 const std::string& url) { |
| 482 if (remote_targets_handler_) |
| 483 remote_targets_handler_->OpenAndInspect(browser_id, url, profile_); |
| 484 } |
| 485 |
| 486 void DevToolsWindowBase::StartRemoteDevicesListener() { |
| 487 remote_targets_handler_ = DevToolsRemoteTargetsUIHandler::CreateForAdb( |
| 488 base::Bind(&DevToolsWindowBase::PopulateRemoteDevices, |
| 489 base::Unretained(this)), |
| 490 profile_); |
| 491 } |
| 492 |
| 493 void DevToolsWindowBase::StopRemoteDevicesListener() { |
| 494 remote_targets_handler_.reset(); |
| 495 } |
| 496 |
| 497 void DevToolsWindowBase::EnableRemoteDeviceCounter(bool enable) { |
| 498 DevToolsAndroidBridge* adb_bridge = |
| 499 DevToolsAndroidBridge::Factory::GetForProfile(profile_); |
| 500 if (!adb_bridge) |
| 501 return; |
| 502 |
| 503 DCHECK(device_listener_enabled_ != enable); |
| 504 device_listener_enabled_ = enable; |
| 505 if (enable) |
| 506 adb_bridge->AddDeviceCountListener(this); |
| 507 else |
| 508 adb_bridge->RemoveDeviceCountListener(this); |
| 509 } |
| 510 |
| 511 void DevToolsWindowBase::DeviceCountChanged(int count) { |
| 512 base::FundamentalValue value(count); |
| 513 CallClientFunction( |
| 514 "InspectorFrontendAPI.setRemoteDeviceCount", &value, NULL, NULL); |
| 515 } |
| 516 |
| 517 void DevToolsWindowBase::PopulateRemoteDevices( |
| 518 const std::string& source, |
| 519 scoped_ptr<base::ListValue> targets) { |
| 520 CallClientFunction( |
| 521 "InspectorFrontendAPI.populateRemoteDevices", targets.get(), NULL, NULL); |
| 522 } |
| 523 |
| 524 void DevToolsWindowBase::FileSavedAs(const std::string& url) { |
| 525 base::StringValue url_value(url); |
| 526 CallClientFunction("InspectorFrontendAPI.savedURL", &url_value, NULL, NULL); |
| 527 } |
| 528 |
| 529 void DevToolsWindowBase::CanceledFileSaveAs(const std::string& url) { |
| 530 base::StringValue url_value(url); |
| 531 CallClientFunction("InspectorFrontendAPI.canceledSaveURL", |
| 532 &url_value, NULL, NULL); |
| 533 } |
| 534 |
| 535 void DevToolsWindowBase::AppendedTo(const std::string& url) { |
| 536 base::StringValue url_value(url); |
| 537 CallClientFunction("InspectorFrontendAPI.appendedToURL", &url_value, NULL, |
| 538 NULL); |
| 539 } |
| 540 |
| 541 void DevToolsWindowBase::FileSystemsLoaded( |
| 542 const std::vector<DevToolsFileHelper::FileSystem>& file_systems) { |
| 543 base::ListValue file_systems_value; |
| 544 for (size_t i = 0; i < file_systems.size(); ++i) |
| 545 file_systems_value.Append(CreateFileSystemValue(file_systems[i])); |
| 546 CallClientFunction("InspectorFrontendAPI.fileSystemsLoaded", |
| 547 &file_systems_value, NULL, NULL); |
| 548 } |
| 549 |
| 550 void DevToolsWindowBase::FileSystemAdded( |
| 551 const DevToolsFileHelper::FileSystem& file_system) { |
| 552 scoped_ptr<base::StringValue> error_string_value( |
| 553 new base::StringValue(std::string())); |
| 554 scoped_ptr<base::DictionaryValue> file_system_value; |
| 555 if (!file_system.file_system_path.empty()) |
| 556 file_system_value.reset(CreateFileSystemValue(file_system)); |
| 557 CallClientFunction("InspectorFrontendAPI.fileSystemAdded", |
| 558 error_string_value.get(), file_system_value.get(), NULL); |
| 559 } |
| 560 |
| 561 void DevToolsWindowBase::IndexingTotalWorkCalculated( |
| 562 int request_id, |
| 563 const std::string& file_system_path, |
| 564 int total_work) { |
| 565 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 566 base::FundamentalValue request_id_value(request_id); |
| 567 base::StringValue file_system_path_value(file_system_path); |
| 568 base::FundamentalValue total_work_value(total_work); |
| 569 CallClientFunction("InspectorFrontendAPI.indexingTotalWorkCalculated", |
| 570 &request_id_value, &file_system_path_value, |
| 571 &total_work_value); |
| 572 } |
| 573 |
| 574 void DevToolsWindowBase::IndexingWorked(int request_id, |
| 575 const std::string& file_system_path, |
| 576 int worked) { |
| 577 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 578 base::FundamentalValue request_id_value(request_id); |
| 579 base::StringValue file_system_path_value(file_system_path); |
| 580 base::FundamentalValue worked_value(worked); |
| 581 CallClientFunction("InspectorFrontendAPI.indexingWorked", &request_id_value, |
| 582 &file_system_path_value, &worked_value); |
| 583 } |
| 584 |
| 585 void DevToolsWindowBase::IndexingDone(int request_id, |
| 586 const std::string& file_system_path) { |
| 587 indexing_jobs_.erase(request_id); |
| 588 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 589 base::FundamentalValue request_id_value(request_id); |
| 590 base::StringValue file_system_path_value(file_system_path); |
| 591 CallClientFunction("InspectorFrontendAPI.indexingDone", &request_id_value, |
| 592 &file_system_path_value, NULL); |
| 593 } |
| 594 |
| 595 void DevToolsWindowBase::SearchCompleted( |
| 596 int request_id, |
| 597 const std::string& file_system_path, |
| 598 const std::vector<std::string>& file_paths) { |
| 599 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 600 base::ListValue file_paths_value; |
| 601 for (std::vector<std::string>::const_iterator it(file_paths.begin()); |
| 602 it != file_paths.end(); ++it) { |
| 603 file_paths_value.AppendString(*it); |
| 604 } |
| 605 base::FundamentalValue request_id_value(request_id); |
| 606 base::StringValue file_system_path_value(file_system_path); |
| 607 CallClientFunction("InspectorFrontendAPI.searchCompleted", &request_id_value, |
| 608 &file_system_path_value, &file_paths_value); |
| 609 } |
| 610 |
| 611 void DevToolsWindowBase::ShowDevToolsConfirmInfoBar( |
| 612 const base::string16& message, |
| 613 const InfoBarCallback& callback) { |
| 614 DevToolsConfirmInfoBarDelegate::Create( |
| 615 InfoBarService::FromWebContents(web_contents_), |
| 616 callback, message); |
| 617 } |
| 618 |
| 619 void DevToolsWindowBase::UpdateTheme() { |
| 620 ThemeService* tp = ThemeServiceFactory::GetForProfile(profile_); |
| 621 DCHECK(tp); |
| 622 |
| 623 std::string command("InspectorFrontendAPI.setToolbarColors(\"" + |
| 624 SkColorToRGBAString(tp->GetColor(ThemeProperties::COLOR_TOOLBAR)) + |
| 625 "\", \"" + |
| 626 SkColorToRGBAString(tp->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT)) + |
| 627 "\")"); |
| 628 web_contents_->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(command)); |
| 629 } |
| 630 |
| 631 void DevToolsWindowBase::AddDevToolsExtensionsToClient() { |
| 632 const ExtensionService* extension_service = extensions::ExtensionSystem::Get( |
| 633 profile_->GetOriginalProfile())->extension_service(); |
| 634 if (!extension_service) |
| 635 return; |
| 636 const extensions::ExtensionSet* extensions = extension_service->extensions(); |
| 637 |
| 638 base::ListValue results; |
| 639 for (extensions::ExtensionSet::const_iterator extension(extensions->begin()); |
| 640 extension != extensions->end(); ++extension) { |
| 641 if (extensions::ManifestURL::GetDevToolsPage(extension->get()).is_empty()) |
| 642 continue; |
| 643 base::DictionaryValue* extension_info = new base::DictionaryValue(); |
| 644 extension_info->Set( |
| 645 "startPage", |
| 646 new base::StringValue( |
| 647 extensions::ManifestURL::GetDevToolsPage( |
| 648 extension->get()).spec())); |
| 649 extension_info->Set("name", new base::StringValue((*extension)->name())); |
| 650 extension_info->Set( |
| 651 "exposeExperimentalAPIs", |
| 652 new base::FundamentalValue((*extension)->HasAPIPermission( |
| 653 extensions::APIPermission::kExperimental))); |
| 654 results.Append(extension_info); |
| 655 } |
| 656 CallClientFunction("WebInspector.addExtensions", &results, NULL, NULL); |
| 657 } |
| 658 |
| 659 void DevToolsWindowBase::CallClientFunction(const std::string& function_name, |
| 660 const base::Value* arg1, |
| 661 const base::Value* arg2, |
| 662 const base::Value* arg3) { |
| 663 std::string params; |
| 664 if (arg1) { |
| 665 std::string json; |
| 666 base::JSONWriter::Write(arg1, &json); |
| 667 params.append(json); |
| 668 if (arg2) { |
| 669 base::JSONWriter::Write(arg2, &json); |
| 670 params.append(", " + json); |
| 671 if (arg3) { |
| 672 base::JSONWriter::Write(arg3, &json); |
| 673 params.append(", " + json); |
| 674 } |
| 675 } |
| 676 } |
| 677 base::string16 javascript = |
| 678 base::UTF8ToUTF16(function_name + "(" + params + ");"); |
| 679 web_contents_->GetMainFrame()->ExecuteJavaScript(javascript); |
| 680 } |
| 681 |
| 682 void DevToolsWindowBase::DocumentOnLoadCompletedInMainFrame() { |
| 683 UpdateTheme(); |
| 684 AddDevToolsExtensionsToClient(); |
| 685 } |
OLD | NEW |