1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <algorithm>
#include "base/command_line.h"
#include "base/json/json_writer.h"
#include "base/lazy_instance.h"
#include "base/stringprintf.h"
#include "base/strings/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/devtools/devtools_window.h"
#include "chrome/browser/extensions/api/debugger/debugger_api.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/file_select_helper.h"
#include "chrome/browser/prefs/pref_service_syncable.h"
#include "chrome/browser/prefs/scoped_user_pref_update.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_tab_helper.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_iterator.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/webui/devtools_ui.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/manifest_url_handler.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_client_host.h"
#include "content/public/browser/devtools_manager.h"
#include "content/public/browser/favicon_status.h"
#include "content/public/browser/load_notification_details.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_view.h"
#include "content/public/common/bindings_policy.h"
#include "content/public/common/content_client.h"
#include "content/public/common/page_transition_types.h"
#include "content/public/common/url_constants.h"
#include "grit/generated_resources.h"
typedef std::vector<DevToolsWindow*> DevToolsWindowList;
namespace {
base::LazyInstance<DevToolsWindowList>::Leaky
g_instances = LAZY_INSTANCE_INITIALIZER;
} // namespace
using base::Bind;
using content::DevToolsAgentHost;
using content::DevToolsClientHost;
using content::DevToolsManager;
using content::FileChooserParams;
using content::NativeWebKeyboardEvent;
using content::NavigationController;
using content::NavigationEntry;
using content::OpenURLParams;
using content::RenderViewHost;
using content::WebContents;
const char DevToolsWindow::kDevToolsApp[] = "DevToolsApp";
const char kOldPrefBottom[] = "bottom";
const char kOldPrefRight[] = "right";
const char kPrefBottom[] = "dock_bottom";
const char kPrefRight[] = "dock_right";
const char kPrefUndocked[] = "undocked";
const char kDockSideBottom[] = "bottom";
const char kDockSideRight[] = "right";
const char kDockSideUndocked[] = "undocked";
const char kDockSideMinimized[] = "minimized";
// Minimal height of devtools pane or content pane when devtools are docked
// to the browser window.
const int kMinDevToolsHeight = 50;
const int kMinDevToolsWidth = 150;
const int kMinContentsSize = 50;
const int kMinimizedDevToolsHeight = 24;
class DevToolsWindow::InspectedWebContentsObserver
: public content::WebContentsObserver {
public:
explicit InspectedWebContentsObserver(content::WebContents* web_contents)
: WebContentsObserver(web_contents) {
}
content::WebContents* Get() { return web_contents(); }
};
class DevToolsWindow::FrontendWebContentsObserver
: public content::WebContentsObserver {
public:
explicit FrontendWebContentsObserver(content::WebContents* web_contents)
: WebContentsObserver(web_contents) {
}
private:
// Overriden from contents::WebContentsObserver.
virtual void AboutToNavigateRenderView(
RenderViewHost* render_view_host) OVERRIDE {
content::DevToolsClientHost::SetupDevToolsFrontendClient(render_view_host);
}
};
// static
std::string DevToolsWindow::GetDevToolsWindowPlacementPrefKey() {
std::string wp_key;
wp_key.append(prefs::kBrowserWindowPlacement);
wp_key.append("_");
wp_key.append(kDevToolsApp);
return wp_key;
}
// static
void DevToolsWindow::RegisterUserPrefs(PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(prefs::kDevToolsOpenDocked,
true,
PrefRegistrySyncable::UNSYNCABLE_PREF);
registry->RegisterStringPref(prefs::kDevToolsDockSide,
kDockSideBottom,
PrefRegistrySyncable::UNSYNCABLE_PREF);
registry->RegisterDictionaryPref(prefs::kDevToolsEditedFiles,
PrefRegistrySyncable::UNSYNCABLE_PREF);
registry->RegisterDictionaryPref(prefs::kDevToolsFileSystemPaths,
PrefRegistrySyncable::UNSYNCABLE_PREF);
registry->RegisterDictionaryPref(GetDevToolsWindowPlacementPrefKey().c_str(),
PrefRegistrySyncable::UNSYNCABLE_PREF);
}
// static
DevToolsWindow* DevToolsWindow::GetDockedInstanceForInspectedTab(
WebContents* inspected_web_contents) {
if (!inspected_web_contents)
return NULL;
if (!DevToolsAgentHost::HasFor(inspected_web_contents->GetRenderViewHost()))
return NULL;
scoped_refptr<DevToolsAgentHost> agent(DevToolsAgentHost::GetOrCreateFor(
inspected_web_contents->GetRenderViewHost()));
DevToolsWindow* window = FindDevToolsWindow(agent);
return window && window->IsDocked() ? window : NULL;
}
// static
bool DevToolsWindow::IsDevToolsWindow(RenderViewHost* window_rvh) {
return AsDevToolsWindow(window_rvh) != NULL;
}
// static
DevToolsWindow* DevToolsWindow::OpenDevToolsWindowForWorker(
Profile* profile,
DevToolsAgentHost* worker_agent) {
DevToolsWindow* window = FindDevToolsWindow(worker_agent);
if (!window) {
window = DevToolsWindow::CreateDevToolsWindowForWorker(profile);
// Will disconnect the current client host if there is one.
DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor(
worker_agent,
window->frontend_host_.get());
}
window->Show(DEVTOOLS_TOGGLE_ACTION_SHOW);
return window;
}
// static
DevToolsWindow* DevToolsWindow::CreateDevToolsWindowForWorker(
Profile* profile) {
return Create(profile, GURL(), NULL, DEVTOOLS_DOCK_SIDE_UNDOCKED, true);
}
// static
DevToolsWindow* DevToolsWindow::OpenDevToolsWindow(
RenderViewHost* inspected_rvh) {
return ToggleDevToolsWindow(inspected_rvh, true,
DEVTOOLS_TOGGLE_ACTION_SHOW);
}
// static
DevToolsWindow* DevToolsWindow::ToggleDevToolsWindow(
Browser* browser,
DevToolsToggleAction action) {
if (action == DEVTOOLS_TOGGLE_ACTION_TOGGLE && browser->is_devtools()) {
browser->tab_strip_model()->CloseAllTabs();
return NULL;
}
RenderViewHost* inspected_rvh =
browser->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
return ToggleDevToolsWindow(inspected_rvh,
action == DEVTOOLS_TOGGLE_ACTION_INSPECT,
action);
}
void DevToolsWindow::InspectElement(RenderViewHost* inspected_rvh,
int x,
int y) {
scoped_refptr<DevToolsAgentHost> agent(
DevToolsAgentHost::GetOrCreateFor(inspected_rvh));
agent->InspectElement(x, y);
// TODO(loislo): we should initiate DevTools window opening from within
// renderer. Otherwise, we still can hit a race condition here.
OpenDevToolsWindow(inspected_rvh);
}
// static
void DevToolsWindow::OpenExternalFrontend(
Profile* profile,
const std::string& frontend_url,
content::DevToolsAgentHost* agent_host) {
DevToolsWindow* window = FindDevToolsWindow(agent_host);
if (!window) {
window = Create(profile, DevToolsUI::GetProxyURL(frontend_url), NULL,
DEVTOOLS_DOCK_SIDE_UNDOCKED, false);
DevToolsManager::GetInstance()->RegisterDevToolsClientHostFor(
agent_host, window->frontend_host_.get());
}
window->Show(DEVTOOLS_TOGGLE_ACTION_SHOW);
}
DevToolsWindow* DevToolsWindow::Create(
Profile* profile,
const GURL& frontend_url,
RenderViewHost* inspected_rvh,
DevToolsDockSide dock_side,
bool shared_worker_frontend) {
// Create WebContents with devtools.
GURL url = GetDevToolsURL(profile, frontend_url, dock_side,
shared_worker_frontend);
return new DevToolsWindow(profile, url, inspected_rvh, dock_side);
}
DevToolsWindow::DevToolsWindow(Profile* profile,
const GURL& url,
RenderViewHost* inspected_rvh,
DevToolsDockSide dock_side)
: profile_(profile),
browser_(NULL),
dock_side_(dock_side),
is_loaded_(false),
action_on_load_(DEVTOOLS_TOGGLE_ACTION_SHOW),
ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
width_(-1),
height_(-1),
dock_side_before_minimized_(dock_side) {
web_contents_ = WebContents::Create(WebContents::CreateParams(profile));
frontend_contents_observer_.reset(
new FrontendWebContentsObserver(web_contents_));
web_contents_->GetController().LoadURL(url, content::Referrer(),
content::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string());
RenderViewHost* render_view_host = web_contents_->GetRenderViewHost();
if (url.host() == chrome::kChromeUIDevToolsBundledHost) {
// Only allow file scheme in embedded front-end by default.
int process_id = render_view_host->GetProcess()->GetID();
content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
process_id, chrome::kFileScheme);
}
frontend_host_.reset(
DevToolsClientHost::CreateDevToolsFrontendHost(web_contents_, this));
file_helper_.reset(new DevToolsFileHelper(web_contents_, profile));
g_instances.Get().push_back(this);
// Wipe out page icon so that the default application icon is used.
NavigationEntry* entry = web_contents_->GetController().GetActiveEntry();
entry->GetFavicon().image = gfx::Image();
entry->GetFavicon().valid = true;
// Register on-load actions.
registrar_.Add(
this,
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(&web_contents_->GetController()));
registrar_.Add(
this,
chrome::NOTIFICATION_TAB_CLOSING,
content::Source<NavigationController>(&web_contents_->GetController()));
registrar_.Add(
this,
chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
content::Source<ThemeService>(
ThemeServiceFactory::GetForProfile(profile_)));
// There is no inspected_rvh in case of shared workers.
if (inspected_rvh)
inspected_contents_observer_.reset(new InspectedWebContentsObserver(
WebContents::FromRenderViewHost(inspected_rvh)));
}
DevToolsWindow::~DevToolsWindow() {
DevToolsWindowList& instances = g_instances.Get();
DevToolsWindowList::iterator it = std::find(instances.begin(),
instances.end(),
this);
DCHECK(it != instances.end());
instances.erase(it);
}
content::WebContents* DevToolsWindow::GetInspectedWebContents() {
if (!inspected_contents_observer_)
return NULL;
return inspected_contents_observer_->Get();
}
void DevToolsWindow::InspectedContentsClosing() {
Hide();
}
void DevToolsWindow::Hide() {
if (IsDocked()) {
// Update dev tools to reflect removed dev tools window.
BrowserWindow* inspected_window = GetInspectedBrowserWindow();
if (inspected_window)
inspected_window->UpdateDevTools();
// In case of docked web_contents_, we own it so delete here.
delete web_contents_;
delete this;
} else {
// First, initiate self-destruct to free all the registrars.
// Then close all tabs. Browser will take care of deleting web_contents_
// for us.
Browser* browser = browser_;
delete this;
browser->tab_strip_model()->CloseAllTabs();
}
}
void DevToolsWindow::Show(DevToolsToggleAction action) {
if (IsDocked()) {
Browser* inspected_browser;
int inspected_tab_index;
// Tell inspected browser to update splitter and switch to inspected panel.
if (!IsInspectedBrowserPopup() &&
FindInspectedBrowserAndTabIndex(&inspected_browser,
&inspected_tab_index)) {
BrowserWindow* inspected_window = inspected_browser->window();
web_contents_->SetDelegate(this);
inspected_window->UpdateDevTools();
web_contents_->GetView()->SetInitialFocus();
inspected_window->Show();
TabStripModel* tab_strip_model = inspected_browser->tab_strip_model();
tab_strip_model->ActivateTabAt(inspected_tab_index, true);
ScheduleAction(action);
return;
} else {
// Sometimes we don't know where to dock. Stay undocked.
dock_side_ = DEVTOOLS_DOCK_SIDE_UNDOCKED;
}
}
// Avoid consecutive window switching if the devtools window has been opened
// and the Inspect Element shortcut is pressed in the inspected tab.
bool should_show_window =
!browser_ || action != DEVTOOLS_TOGGLE_ACTION_INSPECT;
if (!browser_)
CreateDevToolsBrowser();
if (should_show_window) {
browser_->window()->Show();
web_contents_->GetView()->SetInitialFocus();
}
ScheduleAction(action);
}
DevToolsClientHost* DevToolsWindow::GetDevToolsClientHostForTest() {
return frontend_host_.get();
}
int DevToolsWindow::GetWidth(int container_width) {
if (width_ == -1) {
width_ = profile_->GetPrefs()->
GetInteger(prefs::kDevToolsVSplitLocation);
}
// By default, size devtools as 1/3 of the browser window.
if (width_ == -1)
width_ = container_width / 3;
// Respect the minimum devtools width preset.
width_ = std::max(kMinDevToolsWidth, width_);
// But it should never compromise the content window size unless the entire
// window is tiny.
width_ = std::min(container_width - kMinContentsSize, width_);
return width_;
}
int DevToolsWindow::GetHeight(int container_height) {
if (height_ == -1) {
height_ = profile_->GetPrefs()->
GetInteger(prefs::kDevToolsHSplitLocation);
}
// By default, size devtools as 1/3 of the browser window.
if (height_ == -1)
height_ = container_height / 3;
// Respect the minimum devtools width preset.
height_ = std::max(kMinDevToolsHeight, height_);
// But it should never compromise the content window size.
height_ = std::min(container_height - kMinContentsSize, height_);
return height_;
}
int DevToolsWindow::GetMinimumWidth() {
return kMinDevToolsWidth;
}
int DevToolsWindow::GetMinimumHeight() {
return kMinDevToolsHeight;
}
void DevToolsWindow::SetWidth(int width) {
width_ = width;
profile_->GetPrefs()->SetInteger(prefs::kDevToolsVSplitLocation, width);
}
void DevToolsWindow::SetHeight(int height) {
height_ = height;
profile_->GetPrefs()->SetInteger(prefs::kDevToolsHSplitLocation, height);
}
int DevToolsWindow::GetMinimizedHeight() {
return kMinimizedDevToolsHeight;
}
RenderViewHost* DevToolsWindow::GetRenderViewHost() {
return web_contents_->GetRenderViewHost();
}
void DevToolsWindow::CreateDevToolsBrowser() {
std::string wp_key = GetDevToolsWindowPlacementPrefKey();
PrefService* prefs = profile_->GetPrefs();
const DictionaryValue* wp_pref = prefs->GetDictionary(wp_key.c_str());
if (!wp_pref || wp_pref->empty()) {
DictionaryPrefUpdate update(prefs, wp_key.c_str());
DictionaryValue* defaults = update.Get();
defaults->SetInteger("left", 100);
defaults->SetInteger("top", 100);
defaults->SetInteger("right", 740);
defaults->SetInteger("bottom", 740);
defaults->SetBoolean("maximized", false);
defaults->SetBoolean("always_on_top", false);
}
chrome::HostDesktopType host_desktop_type =
chrome::GetHostDesktopTypeForNativeView(
web_contents_->GetView()->GetNativeView());
browser_ = new Browser(Browser::CreateParams::CreateForDevTools(
profile_, host_desktop_type));
browser_->tab_strip_model()->AddWebContents(
web_contents_, -1, content::PAGE_TRANSITION_AUTO_TOPLEVEL,
TabStripModel::ADD_ACTIVE);
}
bool DevToolsWindow::FindInspectedBrowserAndTabIndex(Browser** browser,
int* tab) {
content::WebContents* inspected_web_contents = GetInspectedWebContents();
if (!inspected_web_contents)
return false;
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
int tab_index = it->tab_strip_model()->GetIndexOfWebContents(
inspected_web_contents);
if (tab_index != TabStripModel::kNoTab) {
*browser = *it;
*tab = tab_index;
return true;
}
}
return false;
}
BrowserWindow* DevToolsWindow::GetInspectedBrowserWindow() {
Browser* browser = NULL;
int tab;
return FindInspectedBrowserAndTabIndex(&browser, &tab) ?
browser->window() : NULL;
}
bool DevToolsWindow::IsInspectedBrowserPopup() {
Browser* browser = NULL;
int tab;
if (!FindInspectedBrowserAndTabIndex(&browser, &tab))
return false;
return browser->is_type_popup();
}
void DevToolsWindow::UpdateFrontendDockSide() {
base::StringValue dock_side(SideToString(dock_side_));
CallClientFunction("InspectorFrontendAPI.setDockSide", &dock_side);
base::FundamentalValue docked(IsDocked());
CallClientFunction("InspectorFrontendAPI.setAttachedWindow", &docked);
}
void DevToolsWindow::AddDevToolsExtensionsToClient() {
content::WebContents* inspected_web_contents = GetInspectedWebContents();
if (inspected_web_contents) {
SessionTabHelper* session_tab_helper =
SessionTabHelper::FromWebContents(inspected_web_contents);
if (session_tab_helper) {
base::FundamentalValue tabId(session_tab_helper->session_id().id());
CallClientFunction("WebInspector.setInspectedTabId", &tabId);
}
}
ListValue results;
Profile* profile =
Profile::FromBrowserContext(web_contents_->GetBrowserContext());
const ExtensionService* extension_service = extensions::ExtensionSystem::Get(
profile->GetOriginalProfile())->extension_service();
if (!extension_service)
return;
const ExtensionSet* extensions = extension_service->extensions();
for (ExtensionSet::const_iterator extension = extensions->begin();
extension != extensions->end(); ++extension) {
if (extensions::ManifestURL::GetDevToolsPage(*extension).is_empty())
continue;
DictionaryValue* extension_info = new DictionaryValue();
extension_info->Set("startPage", new StringValue(
extensions::ManifestURL::GetDevToolsPage(*extension).spec()));
extension_info->Set("name", new StringValue((*extension)->name()));
bool allow_experimental = (*extension)->HasAPIPermission(
extensions::APIPermission::kExperimental);
extension_info->Set("exposeExperimentalAPIs",
new base::FundamentalValue(allow_experimental));
results.Append(extension_info);
}
CallClientFunction("WebInspector.addExtensions", &results);
}
WebContents* DevToolsWindow::OpenURLFromTab(WebContents* source,
const OpenURLParams& params) {
if (!params.url.SchemeIs(chrome::kChromeDevToolsScheme)) {
content::WebContents* inspected_web_contents = GetInspectedWebContents();
if (inspected_web_contents)
return inspected_web_contents->OpenURL(params);
return NULL;
}
DevToolsManager* manager = DevToolsManager::GetInstance();
scoped_refptr<DevToolsAgentHost> agent_host(
manager->GetDevToolsAgentHostFor(frontend_host_.get()));
if (!agent_host)
return NULL;
manager->ClientHostClosing(frontend_host_.get());
manager->RegisterDevToolsClientHostFor(agent_host, frontend_host_.get());
chrome::NavigateParams nav_params(profile_, params.url, params.transition);
FillNavigateParamsFromOpenURLParams(&nav_params, params);
nav_params.source_contents = source;
nav_params.tabstrip_add_types = TabStripModel::ADD_NONE;
nav_params.window_action = chrome::NavigateParams::SHOW_WINDOW;
nav_params.user_gesture = true;
chrome::Navigate(&nav_params);
return nav_params.target_contents;
}
void DevToolsWindow::CallClientFunction(const std::string& function_name,
const Value* arg1,
const Value* arg2) {
std::string params;
if (arg1) {
std::string json;
base::JSONWriter::Write(arg1, &json);
params.append(json);
if (arg2) {
base::JSONWriter::Write(arg2, &json);
params.append(", " + json);
}
}
string16 javascript = ASCIIToUTF16(function_name + "(" + params + ");");
web_contents_->GetRenderViewHost()->
ExecuteJavascriptInWebFrame(string16(), javascript);
}
void DevToolsWindow::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (type == content::NOTIFICATION_LOAD_STOP && !is_loaded_) {
is_loaded_ = true;
UpdateTheme();
DoAction();
AddDevToolsExtensionsToClient();
} else if (type == chrome::NOTIFICATION_TAB_CLOSING) {
if (content::Source<NavigationController>(source).ptr() ==
&web_contents_->GetController()) {
// This happens when browser closes all of its tabs as a result
// of window.Close event.
// Notify manager that this DevToolsClientHost no longer exists and
// initiate self-destuct here.
DevToolsManager::GetInstance()->ClientHostClosing(frontend_host_.get());
UpdateBrowserToolbar();
delete this;
}
} else if (type == chrome::NOTIFICATION_BROWSER_THEME_CHANGED) {
UpdateTheme();
}
}
void DevToolsWindow::ScheduleAction(DevToolsToggleAction action) {
action_on_load_ = action;
if (is_loaded_)
DoAction();
}
void DevToolsWindow::DoAction() {
UpdateFrontendDockSide();
switch (action_on_load_) {
case DEVTOOLS_TOGGLE_ACTION_SHOW_CONSOLE:
CallClientFunction("InspectorFrontendAPI.showConsole", NULL);
break;
case DEVTOOLS_TOGGLE_ACTION_INSPECT:
CallClientFunction("InspectorFrontendAPI.enterInspectElementMode", NULL);
case DEVTOOLS_TOGGLE_ACTION_SHOW:
case DEVTOOLS_TOGGLE_ACTION_TOGGLE:
// Do nothing.
break;
default:
NOTREACHED();
}
action_on_load_ = DEVTOOLS_TOGGLE_ACTION_SHOW;
}
std::string SkColorToRGBAString(SkColor color) {
// We convert the alpha using DoubleToString because StringPrintf will use
// locale specific formatters (e.g., use , instead of . in German).
return base::StringPrintf("rgba(%d,%d,%d,%s)", SkColorGetR(color),
SkColorGetG(color), SkColorGetB(color),
base::DoubleToString(SkColorGetA(color) / 255.0).c_str());
}
// static
GURL DevToolsWindow::GetDevToolsURL(Profile* profile,
const GURL& base_url,
DevToolsDockSide dock_side,
bool shared_worker_frontend) {
ThemeService* tp = ThemeServiceFactory::GetForProfile(profile);
CHECK(tp);
SkColor color_toolbar =
tp->GetColor(ThemeProperties::COLOR_TOOLBAR);
SkColor color_tab_text =
tp->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT);
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
bool experiments_enabled =
command_line.HasSwitch(switches::kEnableDevToolsExperiments);
std::string frontend_url = base_url.is_empty() ?
chrome::kChromeUIDevToolsURL : base_url.spec();
std::string params_separator =
frontend_url.find("?") == std::string::npos ? "?" : "&";
std::string url_string = base::StringPrintf("%s%s"
"dockSide=%s&toolbarColor=%s&textColor=%s%s%s",
frontend_url.c_str(),
params_separator.c_str(),
SideToString(dock_side).c_str(),
SkColorToRGBAString(color_toolbar).c_str(),
SkColorToRGBAString(color_tab_text).c_str(),
shared_worker_frontend ? "&isSharedWorker=true" : "",
experiments_enabled ? "&experiments=true" : "");
return GURL(url_string);
}
void DevToolsWindow::UpdateTheme() {
ThemeService* tp = ThemeServiceFactory::GetForProfile(profile_);
CHECK(tp);
SkColor color_toolbar =
tp->GetColor(ThemeProperties::COLOR_TOOLBAR);
SkColor color_tab_text =
tp->GetColor(ThemeProperties::COLOR_BOOKMARK_TEXT);
std::string command = base::StringPrintf(
"InspectorFrontendAPI.setToolbarColors(\"%s\", \"%s\")",
SkColorToRGBAString(color_toolbar).c_str(),
SkColorToRGBAString(color_tab_text).c_str());
web_contents_->GetRenderViewHost()->
ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16(command));
}
void DevToolsWindow::AddNewContents(WebContents* source,
WebContents* new_contents,
WindowOpenDisposition disposition,
const gfx::Rect& initial_pos,
bool user_gesture,
bool* was_blocked) {
content::WebContents* inspected_web_contents = GetInspectedWebContents();
if (inspected_web_contents) {
inspected_web_contents->GetDelegate()->AddNewContents(
source, new_contents, disposition, initial_pos, user_gesture,
was_blocked);
}
}
bool DevToolsWindow::PreHandleKeyboardEvent(
WebContents* source,
const NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut) {
if (IsDocked()) {
BrowserWindow* inspected_window = GetInspectedBrowserWindow();
if (inspected_window)
return inspected_window->PreHandleKeyboardEvent(
event, is_keyboard_shortcut);
}
return false;
}
void DevToolsWindow::HandleKeyboardEvent(WebContents* source,
const NativeWebKeyboardEvent& event) {
if (IsDocked()) {
if (event.windowsKeyCode == 0x08) {
// Do not navigate back in history on Windows (http://crbug.com/74156).
return;
}
BrowserWindow* inspected_window = GetInspectedBrowserWindow();
if (inspected_window)
inspected_window->HandleKeyboardEvent(event);
}
}
// static
DevToolsWindow* DevToolsWindow::ToggleDevToolsWindow(
RenderViewHost* inspected_rvh,
bool force_open,
DevToolsToggleAction action) {
scoped_refptr<DevToolsAgentHost> agent(
DevToolsAgentHost::GetOrCreateFor(inspected_rvh));
DevToolsManager* manager = DevToolsManager::GetInstance();
DevToolsWindow* window = FindDevToolsWindow(agent);
bool do_open = force_open;
if (!window) {
Profile* profile = Profile::FromBrowserContext(
inspected_rvh->GetProcess()->GetBrowserContext());
DevToolsDockSide dock_side = GetDockSideFromPrefs(profile);
window = Create(profile, GURL(), inspected_rvh, dock_side, false);
manager->RegisterDevToolsClientHostFor(agent, window->frontend_host_.get());
do_open = true;
}
// Update toolbar to reflect DevTools changes.
window->UpdateBrowserToolbar();
// If window is docked and visible, we hide it on toggle. If window is
// undocked, we show (activate) it. If window is minimized, we maximize it.
if (window->dock_side_ == DEVTOOLS_DOCK_SIDE_MINIMIZED)
window->Restore();
else if (!window->IsDocked() || do_open)
window->Show(action);
else
window->CloseWindow();
return window;
}
// static
DevToolsWindow* DevToolsWindow::FindDevToolsWindow(
DevToolsAgentHost* agent_host) {
DevToolsManager* manager = DevToolsManager::GetInstance();
DevToolsWindowList& instances = g_instances.Get();
for (DevToolsWindowList::iterator it = instances.begin();
it != instances.end(); ++it) {
if (manager->GetDevToolsAgentHostFor((*it)->frontend_host_.get()) ==
agent_host)
return *it;
}
return NULL;
}
// static
DevToolsWindow* DevToolsWindow::AsDevToolsWindow(RenderViewHost* window_rvh) {
if (g_instances == NULL)
return NULL;
DevToolsWindowList& instances = g_instances.Get();
for (DevToolsWindowList::iterator it = instances.begin();
it != instances.end(); ++it) {
if ((*it)->web_contents_->GetRenderViewHost() == window_rvh)
return *it;
}
return NULL;
}
void DevToolsWindow::ActivateWindow() {
if (!IsDocked()) {
if (!browser_->window()->IsActive()) {
browser_->window()->Activate();
}
} else {
BrowserWindow* inspected_window = GetInspectedBrowserWindow();
if (inspected_window)
web_contents_->GetView()->Focus();
}
}
void DevToolsWindow::ChangeAttachedWindowHeight(unsigned height) {
if (dock_side_ != DEVTOOLS_DOCK_SIDE_BOTTOM)
return;
SetHeight(height);
// Update inspected window to adjust heights.
BrowserWindow* inspected_window = GetInspectedBrowserWindow();
if (inspected_window)
inspected_window->UpdateDevTools();
}
void DevToolsWindow::CloseWindow() {
DCHECK(IsDocked());
DevToolsManager::GetInstance()->ClientHostClosing(frontend_host_.get());
Hide();
}
void DevToolsWindow::MoveWindow(int x, int y) {
if (!IsDocked()) {
gfx::Rect bounds = browser_->window()->GetBounds();
bounds.Offset(x, y);
browser_->window()->SetBounds(bounds);
}
}
void DevToolsWindow::SetDockSide(const std::string& side) {
DevToolsDockSide requested_side = SideFromString(side);
bool dock_requested = requested_side != DEVTOOLS_DOCK_SIDE_UNDOCKED;
bool is_docked = IsDocked();
content::WebContents* inspected_web_contents = GetInspectedWebContents();
if (dock_requested && (!inspected_web_contents ||
!GetInspectedBrowserWindow() || IsInspectedBrowserPopup())) {
// Cannot dock, avoid window flashing due to close-reopen cycle.
return;
}
if (dock_side_ != DEVTOOLS_DOCK_SIDE_MINIMIZED &&
requested_side == DEVTOOLS_DOCK_SIDE_MINIMIZED) {
dock_side_before_minimized_ = dock_side_;
}
dock_side_ = requested_side;
if (dock_requested) {
if (!is_docked) {
// Detach window from the external devtools browser. It will lead to
// the browser object's close and delete. Remove observer first.
TabStripModel* tab_strip_model = browser_->tab_strip_model();
tab_strip_model->DetachWebContentsAt(
tab_strip_model->GetIndexOfWebContents(web_contents_));
browser_ = NULL;
}
} else if (is_docked) {
// Update inspected window to hide split and reset it.
BrowserWindow* inspected_window = GetInspectedBrowserWindow();
if (inspected_window)
inspected_window->UpdateDevTools();
}
if (dock_side_ != DEVTOOLS_DOCK_SIDE_MINIMIZED) {
std::string pref_value = kPrefBottom;
switch (dock_side_) {
case DEVTOOLS_DOCK_SIDE_UNDOCKED:
pref_value = kPrefUndocked;
break;
case DEVTOOLS_DOCK_SIDE_RIGHT:
pref_value = kPrefRight;
break;
case DEVTOOLS_DOCK_SIDE_BOTTOM:
pref_value = kPrefBottom;
break;
case DEVTOOLS_DOCK_SIDE_MINIMIZED:
// We don't persist minimized state.
break;
}
profile_->GetPrefs()->SetString(prefs::kDevToolsDockSide, pref_value);
}
Show(DEVTOOLS_TOGGLE_ACTION_SHOW);
}
void DevToolsWindow::Restore() {
if (dock_side_ == DEVTOOLS_DOCK_SIDE_MINIMIZED)
SetDockSide(SideToString(dock_side_before_minimized_));
}
void DevToolsWindow::OpenInNewTab(const std::string& url) {
OpenURLParams params(GURL(url),
content::Referrer(),
NEW_FOREGROUND_TAB,
content::PAGE_TRANSITION_LINK,
false /* is_renderer_initiated */);
content::WebContents* inspected_web_contents = GetInspectedWebContents();
if (inspected_web_contents) {
inspected_web_contents->OpenURL(params);
} else {
chrome::HostDesktopType host_desktop_type;
if (browser_) {
host_desktop_type = browser_->host_desktop_type();
} else {
// There should always be a browser when there are no inspected web
// contents.
NOTREACHED();
host_desktop_type = chrome::GetActiveDesktop();
}
const BrowserList* browser_list =
BrowserList::GetInstance(host_desktop_type);
for (BrowserList::const_iterator it = browser_list->begin();
it != browser_list->end(); ++it) {
if ((*it)->type() == Browser::TYPE_TABBED) {
(*it)->OpenURL(params);
break;
}
}
}
}
void DevToolsWindow::SaveToFile(const std::string& url,
const std::string& content,
bool save_as) {
file_helper_->Save(url, content, save_as, Bind(&DevToolsWindow::FileSavedAs,
weak_factory_.GetWeakPtr(),
url));
}
void DevToolsWindow::AppendToFile(const std::string& url,
const std::string& content) {
file_helper_->Append(url, content, Bind(&DevToolsWindow::AppendedTo,
weak_factory_.GetWeakPtr(),
url));
}
namespace {
DictionaryValue* CreateFileSystemValue(
DevToolsFileHelper::FileSystem file_system) {
DictionaryValue* file_system_value = new DictionaryValue();
file_system_value->SetString("fileSystemName", file_system.file_system_name);
file_system_value->SetString("rootURL", file_system.root_url);
file_system_value->SetString("fileSystemPath", file_system.file_system_path);
return file_system_value;
}
} // namespace
void DevToolsWindow::RequestFileSystems() {
CHECK(web_contents_->GetURL().SchemeIs(chrome::kChromeDevToolsScheme));
file_helper_->RequestFileSystems(
Bind(&DevToolsWindow::FileSystemsLoaded, weak_factory_.GetWeakPtr()));
}
void DevToolsWindow::AddFileSystem() {
CHECK(web_contents_->GetURL().SchemeIs(chrome::kChromeDevToolsScheme));
file_helper_->AddFileSystem(
Bind(&DevToolsWindow::FileSystemAdded, weak_factory_.GetWeakPtr()));
}
void DevToolsWindow::RemoveFileSystem(const std::string& file_system_path) {
CHECK(web_contents_->GetURL().SchemeIs(chrome::kChromeDevToolsScheme));
file_helper_->RemoveFileSystem(file_system_path);
StringValue file_system_path_value(file_system_path);
CallClientFunction("InspectorFrontendAPI.fileSystemRemoved",
&file_system_path_value);
}
void DevToolsWindow::FileSavedAs(const std::string& url) {
StringValue url_value(url);
CallClientFunction("InspectorFrontendAPI.savedURL", &url_value);
}
void DevToolsWindow::AppendedTo(const std::string& url) {
StringValue url_value(url);
CallClientFunction("InspectorFrontendAPI.appendedToURL", &url_value);
}
void DevToolsWindow::FileSystemsLoaded(
const std::vector<DevToolsFileHelper::FileSystem>& file_systems) {
ListValue file_systems_value;
for (size_t i = 0; i < file_systems.size(); ++i) {
file_systems_value.Append(CreateFileSystemValue(file_systems[i]));
}
CallClientFunction("InspectorFrontendAPI.fileSystemsLoaded",
&file_systems_value);
}
void DevToolsWindow::FileSystemAdded(
std::string error_string,
const DevToolsFileHelper::FileSystem& file_system) {
StringValue error_string_value(error_string);
DictionaryValue* file_system_value = NULL;
if (!file_system.file_system_path.empty())
file_system_value = CreateFileSystemValue(file_system);
CallClientFunction("InspectorFrontendAPI.fileSystemAdded",
&error_string_value,
file_system_value);
if (file_system_value)
delete file_system_value;
}
content::JavaScriptDialogManager* DevToolsWindow::GetJavaScriptDialogManager() {
content::WebContents* inspected_web_contents = GetInspectedWebContents();
if (inspected_web_contents && inspected_web_contents->GetDelegate()) {
return inspected_web_contents->GetDelegate()->
GetJavaScriptDialogManager();
}
return content::WebContentsDelegate::GetJavaScriptDialogManager();
}
void DevToolsWindow::RunFileChooser(WebContents* web_contents,
const FileChooserParams& params) {
FileSelectHelper::RunFileChooser(web_contents, params);
}
void DevToolsWindow::WebContentsFocused(WebContents* contents) {
Browser* inspected_browser = NULL;
int inspected_tab_index = -1;
if (IsDocked() && FindInspectedBrowserAndTabIndex(&inspected_browser,
&inspected_tab_index)) {
inspected_browser->window()->WebContentsFocused(contents);
}
}
void DevToolsWindow::UpdateBrowserToolbar() {
content::WebContents* inspected_web_contents = GetInspectedWebContents();
if (!inspected_web_contents)
return;
BrowserWindow* inspected_window = GetInspectedBrowserWindow();
if (inspected_window)
inspected_window->UpdateToolbar(inspected_web_contents, false);
}
bool DevToolsWindow::IsDocked() {
return dock_side_ != DEVTOOLS_DOCK_SIDE_UNDOCKED;
}
// static
DevToolsDockSide DevToolsWindow::GetDockSideFromPrefs(Profile* profile) {
std::string dock_side =
profile->GetPrefs()->GetString(prefs::kDevToolsDockSide);
// Migrate prefs
if (dock_side == kOldPrefBottom || dock_side == kOldPrefRight) {
bool docked = profile->GetPrefs()->GetBoolean(prefs::kDevToolsOpenDocked);
if (dock_side == kOldPrefBottom)
return docked ? DEVTOOLS_DOCK_SIDE_BOTTOM : DEVTOOLS_DOCK_SIDE_UNDOCKED;
else
return docked ? DEVTOOLS_DOCK_SIDE_RIGHT : DEVTOOLS_DOCK_SIDE_UNDOCKED;
}
if (dock_side == kPrefUndocked)
return DEVTOOLS_DOCK_SIDE_UNDOCKED;
else if (dock_side == kPrefRight)
return DEVTOOLS_DOCK_SIDE_RIGHT;
// Default to docked to bottom
return DEVTOOLS_DOCK_SIDE_BOTTOM;
}
// static
std::string DevToolsWindow::SideToString(DevToolsDockSide dock_side) {
std::string dock_side_string;
switch (dock_side) {
case DEVTOOLS_DOCK_SIDE_UNDOCKED: return kDockSideUndocked;
case DEVTOOLS_DOCK_SIDE_RIGHT: return kDockSideRight;
case DEVTOOLS_DOCK_SIDE_BOTTOM: return kDockSideBottom;
case DEVTOOLS_DOCK_SIDE_MINIMIZED: return kDockSideMinimized;
}
return kDockSideUndocked;
}
// static
DevToolsDockSide DevToolsWindow::SideFromString(
const std::string& dock_side) {
if (dock_side == kDockSideRight)
return DEVTOOLS_DOCK_SIDE_RIGHT;
if (dock_side == kDockSideBottom)
return DEVTOOLS_DOCK_SIDE_BOTTOM;
if (dock_side == kDockSideMinimized)
return DEVTOOLS_DOCK_SIDE_MINIMIZED;
return DEVTOOLS_DOCK_SIDE_UNDOCKED;
}
|