forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrome_frame_automation.cc
1471 lines (1247 loc) · 50.2 KB
/
chrome_frame_automation.cc
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
// Copyright (c) 2011 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 "chrome_frame/chrome_frame_automation.h"
#include "app/app_switches.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/debug/trace_event.h"
#include "base/file_util.h"
#include "base/file_version_info.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/process_util.h"
#include "base/string_util.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/sys_info.h"
#include "base/utf_string_conversions.h"
#include "chrome/app/client_util.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/automation/tab_proxy.h"
#include "chrome_frame/chrome_launcher_utils.h"
#include "chrome_frame/crash_reporting/crash_metrics.h"
#include "chrome_frame/custom_sync_call_context.h"
#include "chrome_frame/navigation_constraints.h"
#include "chrome_frame/simple_resource_loader.h"
#include "chrome_frame/utils.h"
#include "ui/base/ui_base_switches.h"
#ifdef NDEBUG
int64 kAutomationServerReasonableLaunchDelay = 1000; // in milliseconds
#else
int64 kAutomationServerReasonableLaunchDelay = 1000 * 10;
#endif
int kDefaultSendUMADataInterval = 20000; // in milliseconds.
static const wchar_t kUmaSendIntervalValue[] = L"UmaSendInterval";
// This lock ensures that histograms created by ChromeFrame are thread safe.
// The histograms created in ChromeFrame can be initialized on multiple
// threads.
base::Lock g_ChromeFrameHistogramLock;
class ChromeFrameAutomationProxyImpl::TabProxyNotificationMessageFilter
: public IPC::ChannelProxy::MessageFilter {
public:
explicit TabProxyNotificationMessageFilter(AutomationHandleTracker* tracker)
: tracker_(tracker) {
}
void AddTabProxy(AutomationHandle tab_proxy) {
base::AutoLock lock(lock_);
tabs_list_.push_back(tab_proxy);
}
void RemoveTabProxy(AutomationHandle tab_proxy) {
base::AutoLock lock(lock_);
tabs_list_.remove(tab_proxy);
}
virtual bool OnMessageReceived(const IPC::Message& message) {
if (message.is_reply())
return false;
if (!ChromeFrameDelegateImpl::IsTabMessage(message))
return false;
// Get AddRef-ed pointer to corresponding TabProxy object
TabProxy* tab = static_cast<TabProxy*>(tracker_->GetResource(
message.routing_id()));
bool handled = false;
if (tab) {
handled = tab->OnMessageReceived(message);
tab->Release();
} else {
DLOG(ERROR) << "Failed to find TabProxy for tab:" << message.routing_id();
// To prevent subsequent crashes, we set handled to true in this case.
handled = true;
}
return handled;
}
virtual void OnChannelError() {
std::list<AutomationHandle>::const_iterator iter = tabs_list_.begin();
for (; iter != tabs_list_.end(); ++iter) {
// Get AddRef-ed pointer to corresponding TabProxy object
TabProxy* tab = static_cast<TabProxy*>(tracker_->GetResource(*iter));
if (tab) {
tab->OnChannelError();
tab->Release();
}
}
}
private:
AutomationHandleTracker* tracker_;
std::list<AutomationHandle> tabs_list_;
base::Lock lock_;
};
class ChromeFrameAutomationProxyImpl::CFMsgDispatcher
: public SyncMessageReplyDispatcher {
public:
CFMsgDispatcher() : SyncMessageReplyDispatcher() {}
protected:
virtual bool HandleMessageType(const IPC::Message& msg,
SyncMessageCallContext* context) {
switch (context->message_type()) {
case AutomationMsg_CreateExternalTab::ID:
case AutomationMsg_ConnectExternalTab::ID:
InvokeCallback<CreateExternalTabContext>(msg, context);
break;
case AutomationMsg_NavigateExternalTabAtIndex::ID:
case AutomationMsg_NavigateInExternalTab::ID:
InvokeCallback<BeginNavigateContext>(msg, context);
break;
case AutomationMsg_InstallExtension::ID:
InvokeCallback<InstallExtensionContext>(msg, context);
break;
case AutomationMsg_LoadExpandedExtension::ID:
InvokeCallback<InstallExtensionContext>(msg, context);
break;
case AutomationMsg_GetEnabledExtensions::ID:
InvokeCallback<GetEnabledExtensionsContext>(msg, context);
break;
case AutomationMsg_RunUnloadHandlers::ID:
InvokeCallback<UnloadContext>(msg, context);
break;
default:
NOTREACHED();
}
return true;
}
};
ChromeFrameAutomationProxyImpl::ChromeFrameAutomationProxyImpl(
AutomationProxyCacheEntry* entry,
std::string channel_id, int launch_timeout)
: AutomationProxy(launch_timeout, false), proxy_entry_(entry) {
TRACE_EVENT_BEGIN("chromeframe.automationproxy", this, "");
InitializeChannel(channel_id, false);
sync_ = new CFMsgDispatcher();
message_filter_ = new TabProxyNotificationMessageFilter(tracker_.get());
// Order of filters is not important.
channel_->AddFilter(message_filter_.get());
channel_->AddFilter(sync_.get());
}
ChromeFrameAutomationProxyImpl::~ChromeFrameAutomationProxyImpl() {
TRACE_EVENT_END("chromeframe.automationproxy", this, "");
}
void ChromeFrameAutomationProxyImpl::SendAsAsync(
IPC::SyncMessage* msg,
SyncMessageReplyDispatcher::SyncMessageCallContext* context, void* key) {
sync_->Push(msg, context, key);
channel_->ChannelProxy::Send(msg);
}
void ChromeFrameAutomationProxyImpl::CancelAsync(void* key) {
sync_->Cancel(key);
}
void ChromeFrameAutomationProxyImpl::OnChannelError() {
DLOG(ERROR) << "Automation server died";
if (proxy_entry_) {
proxy_entry_->OnChannelError();
} else {
NOTREACHED();
}
}
scoped_refptr<TabProxy> ChromeFrameAutomationProxyImpl::CreateTabProxy(
int handle) {
DCHECK(tracker_->GetResource(handle) == NULL);
TabProxy* tab_proxy = new TabProxy(this, tracker_.get(), handle);
if (tab_proxy != NULL)
message_filter_->AddTabProxy(handle);
return tab_proxy;
}
void ChromeFrameAutomationProxyImpl::ReleaseTabProxy(AutomationHandle handle) {
message_filter_->RemoveTabProxy(handle);
}
struct LaunchTimeStats {
#ifndef NDEBUG
LaunchTimeStats() {
launch_time_begin_ = base::Time::Now();
}
void Dump() {
base::TimeDelta launch_time = base::Time::Now() - launch_time_begin_;
THREAD_SAFE_UMA_HISTOGRAM_TIMES("ChromeFrame.AutomationServerLaunchTime",
launch_time);
const int64 launch_milliseconds = launch_time.InMilliseconds();
if (launch_milliseconds > kAutomationServerReasonableLaunchDelay) {
LOG(WARNING) << "Automation server launch took longer than expected: " <<
launch_milliseconds << " ms.";
}
}
base::Time launch_time_begin_;
#else
void Dump() {}
#endif
};
DISABLE_RUNNABLE_METHOD_REFCOUNT(AutomationProxyCacheEntry);
AutomationProxyCacheEntry::AutomationProxyCacheEntry(
ChromeFrameLaunchParams* params, LaunchDelegate* delegate)
: profile_name(params->profile_name()),
launch_result_(AUTOMATION_LAUNCH_RESULT_INVALID),
snapshots_(NULL), uma_send_interval_(1) {
DCHECK(delegate);
thread_.reset(new base::Thread(WideToASCII(profile_name).c_str()));
thread_->Start();
// Use scoped_refptr so that the params will get released when the task
// has been run.
scoped_refptr<ChromeFrameLaunchParams> ref_params(params);
thread_->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(this,
&AutomationProxyCacheEntry::CreateProxy, ref_params, delegate));
}
AutomationProxyCacheEntry::~AutomationProxyCacheEntry() {
DVLOG(1) << __FUNCTION__ << profile_name;
// Attempt to fix chrome_frame_tests crash seen at times on the IE6/IE7
// builders. It appears that there are cases when we can enter here when the
// AtExitManager is tearing down the global ProxyCache which causes a crash
// while tearing down the AutomationProxy object due to a NULL MessageLoop
// The AutomationProxy class uses the SyncChannel which assumes the existence
// of a MessageLoop instance.
// We leak the AutomationProxy pointer here to avoid a crash.
if (MessageLoop::current() == NULL) {
proxy_.release();
}
}
void AutomationProxyCacheEntry::StartSendUmaInterval(
ChromeFrameHistogramSnapshots* snapshots, int send_interval) {
DCHECK(snapshots);
DCHECK(!snapshots_);
snapshots_ = snapshots;
uma_send_interval_ = send_interval;
thread_->message_loop()->PostDelayedTask(FROM_HERE,
NewRunnableMethod(this, &AutomationProxyCacheEntry::SendUMAData),
send_interval);
}
void AutomationProxyCacheEntry::CreateProxy(ChromeFrameLaunchParams* params,
LaunchDelegate* delegate) {
DCHECK(IsSameThread(base::PlatformThread::CurrentId()));
DCHECK(delegate);
DCHECK(params);
DCHECK(proxy_.get() == NULL);
// We *must* create automationproxy in a thread that has message loop,
// since SyncChannel::Context construction registers event to be watched
// through ObjectWatcher which subscribes for the current thread message loop
// destruction notification.
// At same time we must destroy/stop the thread from another thread.
std::string channel_id = AutomationProxy::GenerateChannelID();
ChromeFrameAutomationProxyImpl* proxy =
new ChromeFrameAutomationProxyImpl(this, channel_id,
params->launch_timeout());
// Ensure that the automation proxy actually respects our choice on whether
// or not to check the version.
proxy->set_perform_version_check(params->version_check());
// Launch browser
scoped_ptr<CommandLine> command_line(
chrome_launcher::CreateLaunchCommandLine());
command_line->AppendSwitchASCII(switches::kAutomationClientChannelID,
channel_id);
// Run Chrome in Chrome Frame mode. In practice, this modifies the paths
// and registry keys that Chrome looks in via the BrowserDistribution
// mechanism.
command_line->AppendSwitch(switches::kChromeFrame);
// Chrome Frame never wants Chrome to start up with a First Run UI.
command_line->AppendSwitch(switches::kNoFirstRun);
command_line->AppendSwitch(switches::kDisablePopupBlocking);
// Disable the "Whoa! Chrome has crashed." dialog, because that isn't very
// useful for Chrome Frame users.
#ifndef NDEBUG
command_line->AppendSwitch(switches::kNoErrorDialogs);
#endif
// In headless mode runs like reliability test runs we want full crash dumps
// from chrome.
if (IsHeadlessMode())
command_line->AppendSwitch(switches::kFullMemoryCrashReport);
// In accessible mode automation tests expect renderer accessibility to be
// enabled in chrome.
if (IsAccessibleMode())
command_line->AppendSwitch(switches::kForceRendererAccessibility);
DVLOG(1) << "Profile path: " << params->profile_path().value();
command_line->AppendSwitchPath(switches::kUserDataDir,
params->profile_path());
// Ensure that Chrome is running the specified version of chrome.dll.
command_line->AppendSwitchNative(switches::kChromeVersion,
GetCurrentModuleVersion());
if (!params->language().empty())
command_line->AppendSwitchNative(switches::kLang, params->language());
std::wstring command_line_string(command_line->command_line_string());
// If there are any extra arguments, append them to the command line.
if (!params->extra_arguments().empty()) {
command_line_string += L' ' + params->extra_arguments();
}
automation_server_launch_start_time_ = base::TimeTicks::Now();
if (!base::LaunchApp(command_line_string, false, false, NULL)) {
// We have no code for launch failure.
launch_result_ = AUTOMATION_LAUNCH_RESULT_INVALID;
} else {
// Launch timeout may happen if the new instance tries to communicate
// with an existing Chrome instance that is hung and displays msgbox
// asking to kill the previous one. This could be easily observed if the
// already running Chrome instance is running as high-integrity process
// (started with "Run as Administrator" or launched by another high
// integrity process) hence our medium-integrity process
// cannot SendMessage to it with request to activate itself.
// TODO(stoyan) AutomationProxy eats Hello message, hence installing
// message filter is pointless, we can leverage ObjectWatcher and use
// system thread pool to notify us when proxy->AppLaunch event is signaled.
LaunchTimeStats launch_stats;
// Wait for the automation server launch result, then stash away the
// version string it reported.
launch_result_ = proxy->WaitForAppLaunch();
launch_stats.Dump();
base::TimeDelta delta =
base::TimeTicks::Now() - automation_server_launch_start_time_;
if (launch_result_ == AUTOMATION_SUCCESS) {
THREAD_SAFE_UMA_HISTOGRAM_TIMES(
"ChromeFrame.AutomationServerLaunchSuccessTime", delta);
} else {
THREAD_SAFE_UMA_HISTOGRAM_TIMES(
"ChromeFrame.AutomationServerLaunchFailedTime", delta);
}
THREAD_SAFE_UMA_HISTOGRAM_CUSTOM_COUNTS("ChromeFrame.LaunchResult",
launch_result_,
AUTOMATION_SUCCESS,
AUTOMATION_CREATE_TAB_FAILED,
AUTOMATION_CREATE_TAB_FAILED + 1);
}
TRACE_EVENT_END("chromeframe.createproxy", this, "");
// Finally set the proxy.
proxy_.reset(proxy);
launch_delegates_.push_back(delegate);
delegate->LaunchComplete(proxy_.get(), launch_result_);
}
void AutomationProxyCacheEntry::RemoveDelegate(LaunchDelegate* delegate,
base::WaitableEvent* done,
bool* was_last_delegate) {
DCHECK(IsSameThread(base::PlatformThread::CurrentId()));
DCHECK(delegate);
DCHECK(done);
DCHECK(was_last_delegate);
*was_last_delegate = false;
LaunchDelegates::iterator it = std::find(launch_delegates_.begin(),
launch_delegates_.end(), delegate);
if (it == launch_delegates_.end()) {
NOTREACHED();
} else {
if (launch_delegates_.size() == 1) {
*was_last_delegate = true;
if (snapshots_)
SendUMAData();
// Process pending notifications.
thread_->message_loop()->RunAllPending();
// Take down the proxy since we no longer have any clients.
// Make sure we only do this once all pending messages have been cleared.
proxy_.reset(NULL);
}
// Be careful to remove from the list after running pending
// tasks. Otherwise the delegate being removed might miss out
// on pending notifications such as LaunchComplete.
launch_delegates_.erase(it);
}
done->Signal();
}
void AutomationProxyCacheEntry::AddDelegate(LaunchDelegate* delegate) {
DCHECK(IsSameThread(base::PlatformThread::CurrentId()));
DCHECK(std::find(launch_delegates_.begin(),
launch_delegates_.end(),
delegate) == launch_delegates_.end())
<< "Same delegate being added twice";
DCHECK(launch_result_ != AUTOMATION_LAUNCH_RESULT_INVALID);
launch_delegates_.push_back(delegate);
delegate->LaunchComplete(proxy_.get(), launch_result_);
}
void AutomationProxyCacheEntry::OnChannelError() {
DCHECK(IsSameThread(base::PlatformThread::CurrentId()));
launch_result_ = AUTOMATION_SERVER_CRASHED;
LaunchDelegates::const_iterator it = launch_delegates_.begin();
for (; it != launch_delegates_.end(); ++it) {
(*it)->AutomationServerDied();
}
}
void AutomationProxyCacheEntry::SendUMAData() {
DCHECK(IsSameThread(base::PlatformThread::CurrentId()));
DCHECK(snapshots_);
// IE uses the chrome frame provided UMA data uploading scheme. NPAPI
// continues to use Chrome to upload UMA data.
if (CrashMetricsReporter::GetInstance()->active()) {
return;
}
if (!proxy_.get()) {
DLOG(WARNING) << __FUNCTION__ << " NULL proxy, can't send UMA data";
} else {
ChromeFrameHistogramSnapshots::HistogramPickledList histograms =
snapshots_->GatherAllHistograms();
if (!histograms.empty()) {
proxy_->Send(new AutomationMsg_RecordHistograms(histograms));
}
MessageLoop::current()->PostDelayedTask(FROM_HERE,
NewRunnableMethod(this, &AutomationProxyCacheEntry::SendUMAData),
uma_send_interval_);
}
}
DISABLE_RUNNABLE_METHOD_REFCOUNT(ProxyFactory);
ProxyFactory::ProxyFactory()
: uma_send_interval_(0) {
uma_send_interval_ = GetConfigInt(kDefaultSendUMADataInterval,
kUmaSendIntervalValue);
}
ProxyFactory::~ProxyFactory() {
for (size_t i = 0; i < proxies_.container().size(); ++i) {
DWORD result = proxies_[i]->WaitForThread(0);
if (WAIT_OBJECT_0 != result)
// TODO(stoyan): Don't leak proxies on exit.
DLOG(ERROR) << "Proxies leaked on exit.";
}
}
void ProxyFactory::GetAutomationServer(
LaunchDelegate* delegate, ChromeFrameLaunchParams* params,
void** automation_server_id) {
TRACE_EVENT_BEGIN("chromeframe.createproxy", this, "");
scoped_refptr<AutomationProxyCacheEntry> entry;
// Find already existing launcher thread for given profile
base::AutoLock lock(lock_);
for (size_t i = 0; i < proxies_.container().size(); ++i) {
if (proxies_[i]->IsSameProfile(params->profile_name())) {
entry = proxies_[i];
break;
}
}
if (entry == NULL) {
DVLOG(1) << __FUNCTION__ << " creating new proxy entry";
entry = new AutomationProxyCacheEntry(params, delegate);
proxies_.container().push_back(entry);
// IE uses the chrome frame provided UMA data uploading scheme. NPAPI
// continues to use Chrome to upload UMA data.
if (!CrashMetricsReporter::GetInstance()->active()) {
entry->StartSendUmaInterval(&chrome_frame_histograms_,
uma_send_interval_);
}
} else if (delegate) {
// Notify the new delegate of the launch status from the worker thread
// and add it to the list of delegates.
entry->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(entry.get(),
&AutomationProxyCacheEntry::AddDelegate, delegate));
}
DCHECK(automation_server_id != NULL);
DCHECK(!entry->IsSameThread(base::PlatformThread::CurrentId()));
*automation_server_id = entry;
}
bool ProxyFactory::ReleaseAutomationServer(void* server_id,
LaunchDelegate* delegate) {
if (!server_id) {
NOTREACHED();
return false;
}
AutomationProxyCacheEntry* entry =
reinterpret_cast<AutomationProxyCacheEntry*>(server_id);
#ifndef NDEBUG
lock_.Acquire();
Vector::ContainerType::iterator it = std::find(proxies_.container().begin(),
proxies_.container().end(),
entry);
DCHECK(it != proxies_.container().end());
DCHECK(!entry->IsSameThread(base::PlatformThread::CurrentId()));
lock_.Release();
#endif
// AddRef the entry object as we might need to take it out of the proxy
// stack and then uninitialize the entry.
entry->AddRef();
bool last_delegate = false;
if (delegate) {
base::WaitableEvent done(true, false);
entry->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(entry,
&AutomationProxyCacheEntry::RemoveDelegate, delegate, &done,
&last_delegate));
done.Wait();
}
if (last_delegate) {
lock_.Acquire();
Vector::ContainerType::iterator it = std::find(proxies_.container().begin(),
proxies_.container().end(),
entry);
proxies_.container().erase(it);
lock_.Release();
}
entry->Release();
return true;
}
static base::LazyInstance<ProxyFactory,
base::LeakyLazyInstanceTraits<ProxyFactory> >
g_proxy_factory(base::LINKER_INITIALIZED);
template <> struct RunnableMethodTraits<ChromeFrameAutomationClient> {
static void RetainCallee(ChromeFrameAutomationClient* obj) {}
static void ReleaseCallee(ChromeFrameAutomationClient* obj) {}
};
ChromeFrameAutomationClient::ChromeFrameAutomationClient()
: chrome_frame_delegate_(NULL),
chrome_window_(NULL),
tab_window_(NULL),
parent_window_(NULL),
automation_server_(NULL),
automation_server_id_(NULL),
ui_thread_id_(NULL),
init_state_(UNINITIALIZED),
use_chrome_network_(false),
proxy_factory_(g_proxy_factory.Pointer()),
handle_top_level_requests_(false),
tab_handle_(-1),
session_id_(-1),
external_tab_cookie_(0),
url_fetcher_(NULL),
url_fetcher_flags_(PluginUrlRequestManager::NOT_THREADSAFE),
navigate_after_initialization_(false),
route_all_top_level_navigations_(false) {
}
ChromeFrameAutomationClient::~ChromeFrameAutomationClient() {
// Uninitialize must be called prior to the destructor
DCHECK(automation_server_ == NULL);
}
bool ChromeFrameAutomationClient::Initialize(
ChromeFrameDelegate* chrome_frame_delegate,
ChromeFrameLaunchParams* chrome_launch_params) {
DCHECK(!IsWindow());
chrome_frame_delegate_ = chrome_frame_delegate;
#ifndef NDEBUG
if (chrome_launch_params_ && chrome_launch_params_ != chrome_launch_params) {
DCHECK_EQ(chrome_launch_params_->url(), chrome_launch_params->url());
DCHECK_EQ(chrome_launch_params_->referrer(),
chrome_launch_params->referrer());
}
#endif
chrome_launch_params_ = chrome_launch_params;
ui_thread_id_ = base::PlatformThread::CurrentId();
#ifndef NDEBUG
// In debug mode give more time to work with a debugger.
if (IsDebuggerPresent()) {
// Don't use INFINITE (which is -1) or even MAXINT since we will convert
// from milliseconds to microseconds when stored in a base::TimeDelta,
// thus * 1000. An hour should be enough.
chrome_launch_params_->set_launch_timeout(60 * 60 * 1000);
} else {
DCHECK_LT(chrome_launch_params_->launch_timeout(),
MAXINT / 2000);
chrome_launch_params_->set_launch_timeout(
chrome_launch_params_->launch_timeout() * 2);
}
#endif // NDEBUG
// Create a window on the UI thread for marshaling messages back and forth
// from the IPC thread. This window cannot be a message only window as the
// external chrome tab window is created as a child of this window. This
// window is eventually reparented to the ActiveX/NPAPI plugin window.
if (!Create(GetDesktopWindow(), NULL, NULL,
WS_CHILDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
WS_EX_TOOLWINDOW)) {
NOTREACHED();
return false;
}
// Keep object in memory, while the window is alive.
// Corresponding Release is in OnFinalMessage();
AddRef();
// Mark our state as initializing. We'll reach initialized once
// InitializeComplete is called successfully.
init_state_ = INITIALIZING;
HRESULT hr = S_OK;
if (chrome_launch_params_->url().is_valid())
navigate_after_initialization_ = false;
proxy_factory_->GetAutomationServer(static_cast<LaunchDelegate*>(this),
chrome_launch_params_, &automation_server_id_);
return true;
}
void ChromeFrameAutomationClient::Uninitialize() {
if (init_state_ == UNINITIALIZED) {
DLOG(WARNING) << __FUNCTION__ << ": Automation client not initialized";
return;
}
init_state_ = UNINITIALIZING;
// Called from client's FinalRelease() / destructor
// ChromeFrameAutomationClient may wait for the initialization (launch)
// to complete while Uninitialize is called.
// We either have to:
// 1) Make ReleaseAutomationServer blocking call (wait until thread exits)
// 2) Behave like a COM object i.e. increase module lock count.
// Otherwise the DLL may get unloaded while we have running threads.
// Unfortunately in NPAPI case we cannot increase module lock count, hence
// we stick with blocking/waiting
if (url_fetcher_) {
// Clean up any outstanding requests
url_fetcher_->StopAllRequests();
url_fetcher_ = NULL;
}
if (tab_) {
tab_->RemoveObserver(this);
if (automation_server_)
automation_server_->ReleaseTabProxy(tab_->handle());
tab_ = NULL; // scoped_refptr::Release
}
// Wait for the background thread to exit.
ReleaseAutomationServer();
// We must destroy the window, since if there are pending tasks
// window procedure may be invoked after DLL is unloaded.
// Unfortunately pending tasks are leaked.
if (::IsWindow(m_hWnd))
DestroyWindow();
// DCHECK(navigate_after_initialization_ == false);
handle_top_level_requests_ = false;
ui_thread_id_ = 0;
chrome_frame_delegate_ = NULL;
init_state_ = UNINITIALIZED;
}
bool ChromeFrameAutomationClient::InitiateNavigation(
const std::string& url,
const std::string& referrer,
NavigationConstraints* navigation_constraints) {
if (url.empty())
return false;
GURL parsed_url(url);
// Catch invalid URLs early.
// Can we allow this navigation to happen?
if (!CanNavigate(parsed_url, navigation_constraints)) {
DLOG(ERROR) << __FUNCTION__ << " Not allowing navigation to: " << url;
return false;
}
// If we are not yet initialized ignore attempts to navigate to the same url.
// Navigation attempts to the same URL could occur if the automation client
// was reused for a new active document instance.
if (!chrome_launch_params_ || is_initialized() ||
parsed_url != chrome_launch_params_->url()) {
// Important: Since we will be using the referrer_ variable from a
// different thread, we need to force a new std::string buffer instance for
// the referrer_ GURL variable. Otherwise we can run into strangeness when
// the GURL is accessed and it could result in a bad URL that can cause the
// referrer to be dropped or something worse.
GURL referrer_gurl(referrer.c_str());
if (!chrome_launch_params_) {
FilePath profile_path;
chrome_launch_params_ = new ChromeFrameLaunchParams(parsed_url,
referrer_gurl, profile_path, L"", SimpleResourceLoader::GetLanguage(),
L"", false, false, route_all_top_level_navigations_);
} else {
chrome_launch_params_->set_referrer(referrer_gurl);
chrome_launch_params_->set_url(parsed_url);
}
navigate_after_initialization_ = false;
if (is_initialized()) {
BeginNavigate();
} else {
navigate_after_initialization_ = true;
}
}
return true;
}
bool ChromeFrameAutomationClient::NavigateToIndex(int index) {
// Could be NULL if we failed to launch Chrome in LaunchAutomationServer()
if (!automation_server_ || !tab_.get() || !tab_->is_valid()) {
return false;
}
DCHECK(::IsWindow(chrome_window_));
IPC::SyncMessage* msg = new AutomationMsg_NavigateExternalTabAtIndex(
tab_->handle(), index, NULL);
automation_server_->SendAsAsync(msg, new BeginNavigateContext(this),
this);
return true;
}
bool ChromeFrameAutomationClient::ForwardMessageFromExternalHost(
const std::string& message, const std::string& origin,
const std::string& target) {
// Could be NULL if we failed to launch Chrome in LaunchAutomationServer()
if (!is_initialized())
return false;
tab_->HandleMessageFromExternalHost(message, origin, target);
return true;
}
bool ChromeFrameAutomationClient::SetProxySettings(
const std::string& json_encoded_proxy_settings) {
if (!is_initialized())
return false;
automation_server_->SendProxyConfig(json_encoded_proxy_settings);
return true;
}
void ChromeFrameAutomationClient::BeginNavigate() {
// Could be NULL if we failed to launch Chrome in LaunchAutomationServer()
if (!automation_server_ || !tab_.get()) {
DLOG(WARNING) << "BeginNavigate - can't navigate.";
ReportNavigationError(AUTOMATION_MSG_NAVIGATION_ERROR,
chrome_launch_params_->url().spec());
return;
}
DCHECK(::IsWindow(chrome_window_));
if (!tab_->is_valid()) {
DLOG(WARNING) << "BeginNavigate - tab isn't valid.";
return;
}
IPC::SyncMessage* msg =
new AutomationMsg_NavigateInExternalTab(tab_->handle(),
chrome_launch_params_->url(), chrome_launch_params_->referrer(),
NULL);
automation_server_->SendAsAsync(msg, new BeginNavigateContext(this), this);
RECT client_rect = {0};
chrome_frame_delegate_->GetBounds(&client_rect);
Resize(client_rect.right - client_rect.left,
client_rect.bottom - client_rect.top,
SWP_NOACTIVATE | SWP_NOZORDER);
}
void ChromeFrameAutomationClient::BeginNavigateCompleted(
AutomationMsg_NavigationResponseValues result) {
if (result == AUTOMATION_MSG_NAVIGATION_ERROR)
ReportNavigationError(AUTOMATION_MSG_NAVIGATION_ERROR,
chrome_launch_params_->url().spec());
}
void ChromeFrameAutomationClient::FindInPage(const std::wstring& search_string,
FindInPageDirection forward,
FindInPageCase match_case,
bool find_next) {
DCHECK(tab_.get());
// What follows is quite similar to TabProxy::FindInPage() but uses
// the SyncMessageReplyDispatcher to avoid concerns about blocking
// synchronous messages.
AutomationMsg_Find_Params params;
params.unused = 0;
params.search_string = WideToUTF16Hack(search_string);
params.find_next = find_next;
params.match_case = (match_case == CASE_SENSITIVE);
params.forward = (forward == FWD);
IPC::SyncMessage* msg =
new AutomationMsg_Find(tab_->handle(), params, NULL, NULL);
automation_server_->SendAsAsync(msg, NULL, this);
}
void ChromeFrameAutomationClient::InstallExtension(
const FilePath& crx_path,
void* user_data) {
if (automation_server_ == NULL) {
InstallExtensionComplete(crx_path,
user_data,
AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
return;
}
InstallExtensionContext* ctx = new InstallExtensionContext(
this, crx_path, user_data);
IPC::SyncMessage* msg = new AutomationMsg_InstallExtension(crx_path, NULL);
// The context will delete itself after it is called.
automation_server_->SendAsAsync(msg, ctx, this);
}
void ChromeFrameAutomationClient::InstallExtensionComplete(
const FilePath& crx_path,
void* user_data,
AutomationMsg_ExtensionResponseValues res) {
DCHECK_EQ(base::PlatformThread::CurrentId(), ui_thread_id_);
if (chrome_frame_delegate_) {
chrome_frame_delegate_->OnExtensionInstalled(crx_path, user_data, res);
}
}
void ChromeFrameAutomationClient::GetEnabledExtensions(void* user_data) {
if (automation_server_ == NULL) {
GetEnabledExtensionsComplete(user_data, &std::vector<FilePath>());
return;
}
GetEnabledExtensionsContext* ctx = new GetEnabledExtensionsContext(
this, user_data);
IPC::SyncMessage* msg = new AutomationMsg_GetEnabledExtensions(
ctx->extension_directories());
// The context will delete itself after it is called.
automation_server_->SendAsAsync(msg, ctx, this);
}
void ChromeFrameAutomationClient::GetEnabledExtensionsComplete(
void* user_data,
std::vector<FilePath>* extension_directories) {
DCHECK_EQ(base::PlatformThread::CurrentId(), ui_thread_id_);
if (chrome_frame_delegate_) {
chrome_frame_delegate_->OnGetEnabledExtensionsComplete(
user_data, *extension_directories);
}
delete extension_directories;
}
int ChromeFrameAutomationClient::GetSessionId() const {
return session_id_;
}
void ChromeFrameAutomationClient::OnChromeFrameHostMoved() {
// Use a local var to avoid the small possibility of getting the tab_
// member be cleared while we try to use it.
// Note that TabProxy is a RefCountedThreadSafe object, so we should be OK.
scoped_refptr<TabProxy> tab(tab_);
// There also is a possibility that tab_ has not been set yet,
// so we still need to test for NULL.
if (tab)
tab->OnHostMoved();
}
void ChromeFrameAutomationClient::LoadExpandedExtension(
const FilePath& path,
void* user_data) {
if (automation_server_ == NULL) {
InstallExtensionComplete(path,
user_data,
AUTOMATION_MSG_EXTENSION_INSTALL_FAILED);
return;
}
InstallExtensionContext* ctx = new InstallExtensionContext(
this, path, user_data);
IPC::SyncMessage* msg = new AutomationMsg_LoadExpandedExtension(path, NULL);
// The context will delete itself after it is called.
automation_server_->SendAsAsync(msg, ctx, this);
}
void ChromeFrameAutomationClient::CreateExternalTab() {
AutomationLaunchResult launch_result = AUTOMATION_SUCCESS;
DCHECK(IsWindow());
DCHECK(automation_server_ != NULL);
if (chrome_launch_params_->url().is_valid()) {
navigate_after_initialization_ = false;
}
const ExternalTabSettings settings(
m_hWnd,
gfx::Rect(),
WS_CHILD,
chrome_launch_params_->incognito(),
!use_chrome_network_,
handle_top_level_requests_,
chrome_launch_params_->url(),
chrome_launch_params_->referrer(),
// Infobars disabled in widget mode.
!chrome_launch_params_->widget_mode(),
chrome_launch_params_->route_all_top_level_navigations());
THREAD_SAFE_UMA_HISTOGRAM_CUSTOM_COUNTS(
"ChromeFrame.HostNetworking", !use_chrome_network_, 0, 1, 2);
THREAD_SAFE_UMA_HISTOGRAM_CUSTOM_COUNTS(
"ChromeFrame.HandleTopLevelRequests", handle_top_level_requests_, 0, 1,
2);
IPC::SyncMessage* message =
new AutomationMsg_CreateExternalTab(settings, NULL, NULL, 0, 0);
automation_server_->SendAsAsync(message, new CreateExternalTabContext(this),
this);
}
AutomationLaunchResult ChromeFrameAutomationClient::CreateExternalTabComplete(
HWND chrome_window, HWND tab_window, int tab_handle, int session_id) {
if (!automation_server_) {
// If we receive this notification while shutting down, do nothing.
DLOG(ERROR) << "CreateExternalTabComplete called when automation server "
<< "was null!";
return AUTOMATION_CREATE_TAB_FAILED;
}
AutomationLaunchResult launch_result = AUTOMATION_SUCCESS;
if (tab_handle == 0 || !::IsWindow(chrome_window)) {
launch_result = AUTOMATION_CREATE_TAB_FAILED;
} else {
chrome_window_ = chrome_window;
tab_window_ = tab_window;
tab_ = automation_server_->CreateTabProxy(tab_handle);
tab_->AddObserver(this);
tab_handle_ = tab_handle;
session_id_ = session_id;
}
return launch_result;