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

Side by Side Diff: chrome/browser/devtools/devtools_ui_bindings.cc

Issue 375393002: DevTools: factor out InspectorFrontendHost/API as a separate entity (chrome) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/devtools/devtools_ui_bindings.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/devtools/devtools_ui_bindings.h" 5 #include "chrome/browser/devtools/devtools_ui_bindings.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 using base::DictionaryValue; 56 using base::DictionaryValue;
57 using content::BrowserThread; 57 using content::BrowserThread;
58 58
59 namespace { 59 namespace {
60 60
61 static const char kFrontendHostId[] = "id"; 61 static const char kFrontendHostId[] = "id";
62 static const char kFrontendHostMethod[] = "method"; 62 static const char kFrontendHostMethod[] = "method";
63 static const char kFrontendHostParams[] = "params"; 63 static const char kFrontendHostParams[] = "params";
64 static const char kTitleFormat[] = "Developer Tools - %s"; 64 static const char kTitleFormat[] = "Developer Tools - %s";
65 65
66 static const char kDevicesChanged[] = "DevicesChanged";
67 static const char kDeviceCountChanged[] = "DeviceCountChanged";
68
69 std::string SkColorToRGBAString(SkColor color) { 66 std::string SkColorToRGBAString(SkColor color) {
70 // We avoid StringPrintf because it will use locale specific formatters for 67 // We avoid StringPrintf because it will use locale specific formatters for
71 // the double (e.g. ',' instead of '.' in German). 68 // the double (e.g. ',' instead of '.' in German).
72 return "rgba(" + base::IntToString(SkColorGetR(color)) + "," + 69 return "rgba(" + base::IntToString(SkColorGetR(color)) + "," +
73 base::IntToString(SkColorGetG(color)) + "," + 70 base::IntToString(SkColorGetG(color)) + "," +
74 base::IntToString(SkColorGetB(color)) + "," + 71 base::IntToString(SkColorGetB(color)) + "," +
75 base::DoubleToString(SkColorGetA(color) / 255.0) + ")"; 72 base::DoubleToString(SkColorGetA(color) / 255.0) + ")";
76 } 73 }
77 74
78 base::DictionaryValue* CreateFileSystemValue( 75 base::DictionaryValue* CreateFileSystemValue(
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 switches::kEnableDevToolsExperiments)) 297 switches::kEnableDevToolsExperiments))
301 url_string += "&experiments=true"; 298 url_string += "&experiments=true";
302 return GURL(url_string); 299 return GURL(url_string);
303 } 300 }
304 301
305 DevToolsUIBindings::DevToolsUIBindings(content::WebContents* web_contents, 302 DevToolsUIBindings::DevToolsUIBindings(content::WebContents* web_contents,
306 const GURL& url) 303 const GURL& url)
307 : profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), 304 : profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())),
308 web_contents_(web_contents), 305 web_contents_(web_contents),
309 delegate_(new DefaultBindingsDelegate(web_contents_)), 306 delegate_(new DefaultBindingsDelegate(web_contents_)),
310 device_listener_enabled_(false), 307 device_count_updates_enabled_(false),
308 devices_updates_enabled_(false),
311 url_(url), 309 url_(url),
312 weak_factory_(this) { 310 weak_factory_(this) {
313 frontend_contents_observer_.reset(new FrontendWebContentsObserver(this)); 311 frontend_contents_observer_.reset(new FrontendWebContentsObserver(this));
314 web_contents_->GetMutableRendererPrefs()->can_accept_load_drops = false; 312 web_contents_->GetMutableRendererPrefs()->can_accept_load_drops = false;
315 313
316 frontend_host_.reset(content::DevToolsClientHost::CreateDevToolsFrontendHost( 314 frontend_host_.reset(content::DevToolsClientHost::CreateDevToolsFrontendHost(
317 web_contents_, this)); 315 web_contents_, this));
318 file_helper_.reset(new DevToolsFileHelper(web_contents_, profile_)); 316 file_helper_.reset(new DevToolsFileHelper(web_contents_, profile_));
319 file_system_indexer_ = new DevToolsFileSystemIndexer(); 317 file_system_indexer_ = new DevToolsFileSystemIndexer();
320 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( 318 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
(...skipping 21 matching lines...) Expand all
342 340
343 DevToolsUIBindings::~DevToolsUIBindings() { 341 DevToolsUIBindings::~DevToolsUIBindings() {
344 content::DevToolsManager::GetInstance()->ClientHostClosing( 342 content::DevToolsManager::GetInstance()->ClientHostClosing(
345 frontend_host_.get()); 343 frontend_host_.get());
346 344
347 for (IndexingJobsMap::const_iterator jobs_it(indexing_jobs_.begin()); 345 for (IndexingJobsMap::const_iterator jobs_it(indexing_jobs_.begin());
348 jobs_it != indexing_jobs_.end(); ++jobs_it) { 346 jobs_it != indexing_jobs_.end(); ++jobs_it) {
349 jobs_it->second->Stop(); 347 jobs_it->second->Stop();
350 } 348 }
351 indexing_jobs_.clear(); 349 indexing_jobs_.clear();
352 350 SetDeviceCountUpdatesEnabled(false);
353 while (!subscribers_.empty()) 351 SetDevicesUpdatesEnabled(false);
354 Unsubscribe(*subscribers_.begin());
355 } 352 }
356 353
357 void DevToolsUIBindings::InspectedContentsClosing() { 354 void DevToolsUIBindings::InspectedContentsClosing() {
358 delegate_->InspectedContentsClosing(); 355 delegate_->InspectedContentsClosing();
359 } 356 }
360 357
361 void DevToolsUIBindings::Observe(int type, 358 void DevToolsUIBindings::Observe(int type,
362 const content::NotificationSource& source, 359 const content::NotificationSource& source,
363 const content::NotificationDetails& details) { 360 const content::NotificationDetails& details) {
364 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); 361 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type);
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 561
565 void DevToolsUIBindings::OpenUrlOnRemoteDeviceAndInspect( 562 void DevToolsUIBindings::OpenUrlOnRemoteDeviceAndInspect(
566 const std::string& browser_id, 563 const std::string& browser_id,
567 const std::string& url) { 564 const std::string& url) {
568 if (remote_targets_handler_) { 565 if (remote_targets_handler_) {
569 remote_targets_handler_->Open(browser_id, url, 566 remote_targets_handler_->Open(browser_id, url,
570 base::Bind(&InspectTarget, profile_)); 567 base::Bind(&InspectTarget, profile_));
571 } 568 }
572 } 569 }
573 570
574 void DevToolsUIBindings::Subscribe(const std::string& event_type) { 571 void DevToolsUIBindings::SetDeviceCountUpdatesEnabled(bool enabled) {
575 if (subscribers_.find(event_type) != subscribers_.end()) { 572 if (device_count_updates_enabled_ == enabled)
576 LOG(ERROR) << "Already subscribed for [" << event_type << "].";
577 return; 573 return;
578 }
579
580 subscribers_.insert(event_type);
581
582 if (event_type == kDevicesChanged) {
583 remote_targets_handler_ = DevToolsTargetsUIHandler::CreateForAdb(
584 base::Bind(&DevToolsUIBindings::PopulateRemoteDevices,
585 base::Unretained(this)),
586 profile_);
587 } else if (event_type == kDeviceCountChanged) {
588 EnableRemoteDeviceCounter(true);
589 } else {
590 LOG(ERROR) << "Attempt to start unknown event listener " << event_type;
591 }
592 }
593
594 void DevToolsUIBindings::Unsubscribe(const std::string& event_type) {
595 if (subscribers_.find(event_type) == subscribers_.end()) {
596 LOG(ERROR) << "Not yet subscribed for [" << event_type << "]";
597 return;
598 }
599
600 if (event_type == kDevicesChanged) {
601 remote_targets_handler_.reset();
602 } else if (event_type == kDeviceCountChanged) {
603 EnableRemoteDeviceCounter(false);
604 } else {
605 LOG(ERROR) << "Attempt to stop unknown event listener " << event_type;
606 }
607
608 subscribers_.erase(event_type);
609 }
610
611 void DevToolsUIBindings::EnableRemoteDeviceCounter(bool enable) {
612 DevToolsAndroidBridge* adb_bridge = 574 DevToolsAndroidBridge* adb_bridge =
613 DevToolsAndroidBridge::Factory::GetForProfile(profile_); 575 DevToolsAndroidBridge::Factory::GetForProfile(profile_);
614 if (!adb_bridge) 576 if (!adb_bridge)
615 return; 577 return;
616 578
617 DCHECK(device_listener_enabled_ != enable); 579 device_count_updates_enabled_ = enabled;
618 device_listener_enabled_ = enable; 580 if (enabled)
619 if (enable)
620 adb_bridge->AddDeviceCountListener(this); 581 adb_bridge->AddDeviceCountListener(this);
621 else 582 else
622 adb_bridge->RemoveDeviceCountListener(this); 583 adb_bridge->RemoveDeviceCountListener(this);
623 } 584 }
624 585
625 void DevToolsUIBindings::DeviceCountChanged(int count) { 586 void DevToolsUIBindings::SetDevicesUpdatesEnabled(bool enabled) {
626 DispatchEventOnFrontend(kDeviceCountChanged, base::FundamentalValue(count)); 587 if (devices_updates_enabled_ == enabled)
588 return;
589 devices_updates_enabled_ = enabled;
590 if (enabled) {
591 remote_targets_handler_ = DevToolsTargetsUIHandler::CreateForAdb(
592 base::Bind(&DevToolsUIBindings::DevicesUpdated,
593 base::Unretained(this)),
594 profile_);
595 } else {
596 remote_targets_handler_.reset();
597 }
627 } 598 }
628 599
629 void DevToolsUIBindings::PopulateRemoteDevices( 600 void DevToolsUIBindings::DeviceCountChanged(int count) {
601 base::FundamentalValue value(count);
602 CallClientFunction("InspectorFrontendAPI.deviceCountUpdated", &value, NULL,
603 NULL);
604 }
605
606 void DevToolsUIBindings::DevicesUpdated(
630 const std::string& source, 607 const std::string& source,
631 const base::ListValue& targets) { 608 const base::ListValue& targets) {
632 DispatchEventOnFrontend(kDevicesChanged, targets); 609 CallClientFunction("InspectorFrontendAPI.devicesUpdated", &targets, NULL,
610 NULL);
633 } 611 }
634 612
635 void DevToolsUIBindings::FileSavedAs(const std::string& url) { 613 void DevToolsUIBindings::FileSavedAs(const std::string& url) {
636 base::StringValue url_value(url); 614 base::StringValue url_value(url);
637 CallClientFunction("InspectorFrontendAPI.savedURL", &url_value, NULL, NULL); 615 CallClientFunction("InspectorFrontendAPI.savedURL", &url_value, NULL, NULL);
638 } 616 }
639 617
640 void DevToolsUIBindings::CanceledFileSaveAs(const std::string& url) { 618 void DevToolsUIBindings::CanceledFileSaveAs(const std::string& url) {
641 base::StringValue url_value(url); 619 base::StringValue url_value(url);
642 CallClientFunction("InspectorFrontendAPI.canceledSaveURL", 620 CallClientFunction("InspectorFrontendAPI.canceledSaveURL",
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
786 base::JSONWriter::Write(arg3, &json); 764 base::JSONWriter::Write(arg3, &json);
787 params.append(", " + json); 765 params.append(", " + json);
788 } 766 }
789 } 767 }
790 } 768 }
791 base::string16 javascript = 769 base::string16 javascript =
792 base::UTF8ToUTF16(function_name + "(" + params + ");"); 770 base::UTF8ToUTF16(function_name + "(" + params + ");");
793 web_contents_->GetMainFrame()->ExecuteJavaScript(javascript); 771 web_contents_->GetMainFrame()->ExecuteJavaScript(javascript);
794 } 772 }
795 773
796 void DevToolsUIBindings::DispatchEventOnFrontend(
797 const std::string& event_type,
798 const base::Value& event_data) {
799 if (subscribers_.find(event_type) == subscribers_.end())
800 return;
801 base::StringValue event_type_value(event_type);
802 CallClientFunction("InspectorFrontendAPI.dispatchEventToListeners",
803 &event_type_value,
804 &event_data,
805 NULL);
806 }
807
808 void DevToolsUIBindings::DocumentOnLoadCompletedInMainFrame() { 774 void DevToolsUIBindings::DocumentOnLoadCompletedInMainFrame() {
809 // Call delegate first - it seeds importants bit of information. 775 // Call delegate first - it seeds importants bit of information.
810 delegate_->OnLoadCompleted(); 776 delegate_->OnLoadCompleted();
811 777
812 UpdateTheme(); 778 UpdateTheme();
813 AddDevToolsExtensionsToClient(); 779 AddDevToolsExtensionsToClient();
814 } 780 }
OLDNEW
« no previous file with comments | « chrome/browser/devtools/devtools_ui_bindings.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698