• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "chromecast/shell/browser/cast_browser_main_parts.h"
6 
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "chromecast/common/chromecast_config.h"
11 #include "chromecast/metrics/cast_metrics_service_client.h"
12 #include "chromecast/net/network_change_notifier_cast.h"
13 #include "chromecast/net/network_change_notifier_factory_cast.h"
14 #include "chromecast/service/cast_service.h"
15 #include "chromecast/shell/browser/cast_browser_context.h"
16 #include "chromecast/shell/browser/cast_browser_process.h"
17 #include "chromecast/shell/browser/devtools/remote_debugging_server.h"
18 #include "chromecast/shell/browser/url_request_context_factory.h"
19 #include "chromecast/shell/browser/webui/webui_cast.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/common/content_switches.h"
22 
23 #if defined(OS_ANDROID)
24 #include "net/android/network_change_notifier_factory_android.h"
25 #endif  // defined(OS_ANDROID)
26 
27 namespace chromecast {
28 namespace shell {
29 
30 namespace {
31 
32 struct DefaultCommandLineSwitch {
33   const char* const switch_name;
34   const char* const switch_value;
35 };
36 
37 DefaultCommandLineSwitch g_default_switches[] = {
38   { switches::kDisableApplicationCache, "" },
39   { switches::kDisablePlugins, "" },
40   // Always enable HTMLMediaElement logs.
41   { switches::kBlinkPlatformLogChannels, "Media"},
42   { NULL, NULL },  // Termination
43 };
44 
AddDefaultCommandLineSwitches(CommandLine * command_line)45 void AddDefaultCommandLineSwitches(CommandLine* command_line) {
46   int i = 0;
47   while (g_default_switches[i].switch_name != NULL) {
48     command_line->AppendSwitchASCII(
49         std::string(g_default_switches[i].switch_name),
50         std::string(g_default_switches[i].switch_value));
51     ++i;
52   }
53 }
54 
55 }  // namespace
56 
CastBrowserMainParts(const content::MainFunctionParams & parameters,URLRequestContextFactory * url_request_context_factory)57 CastBrowserMainParts::CastBrowserMainParts(
58     const content::MainFunctionParams& parameters,
59     URLRequestContextFactory* url_request_context_factory)
60     : BrowserMainParts(),
61       cast_browser_process_(new CastBrowserProcess()),
62       parameters_(parameters),
63       url_request_context_factory_(url_request_context_factory) {
64   CommandLine* command_line = CommandLine::ForCurrentProcess();
65   AddDefaultCommandLineSwitches(command_line);
66 }
67 
~CastBrowserMainParts()68 CastBrowserMainParts::~CastBrowserMainParts() {
69 }
70 
PreMainMessageLoopStart()71 void CastBrowserMainParts::PreMainMessageLoopStart() {
72 #if defined(OS_ANDROID)
73   net::NetworkChangeNotifier::SetFactory(
74       new net::NetworkChangeNotifierFactoryAndroid());
75 #else
76   net::NetworkChangeNotifier::SetFactory(
77       new NetworkChangeNotifierFactoryCast());
78 #endif  // defined(OS_ANDROID)
79 }
80 
PostMainMessageLoopStart()81 void CastBrowserMainParts::PostMainMessageLoopStart() {
82 #if defined(OS_ANDROID)
83   base::MessageLoopForUI::current()->Start();
84 #endif  // defined(OS_ANDROID)
85 }
86 
PreCreateThreads()87 int CastBrowserMainParts::PreCreateThreads() {
88   ChromecastConfig::Create(new PrefRegistrySimple());
89   return 0;
90 }
91 
PreMainMessageLoopRun()92 void CastBrowserMainParts::PreMainMessageLoopRun() {
93   url_request_context_factory_->InitializeOnUIThread();
94 
95   cast_browser_process_->SetBrowserContext(
96       new CastBrowserContext(url_request_context_factory_));
97   cast_browser_process_->SetMetricsServiceClient(
98       metrics::CastMetricsServiceClient::Create(
99           content::BrowserThread::GetBlockingPool(),
100           ChromecastConfig::GetInstance()->pref_service(),
101           cast_browser_process_->browser_context()->GetRequestContext()));
102   cast_browser_process_->SetRemoteDebuggingServer(new RemoteDebuggingServer());
103 
104   InitializeWebUI();
105 
106   cast_browser_process_->SetCastService(
107       CastService::Create(cast_browser_process_->browser_context(),
108                           url_request_context_factory_->GetSystemGetter()));
109   cast_browser_process_->cast_service()->Start();
110 }
111 
MainMessageLoopRun(int * result_code)112 bool CastBrowserMainParts::MainMessageLoopRun(int* result_code) {
113   // If parameters_.ui_task is not NULL, we are running browser tests. In this
114   // case, the browser's main message loop will not run.
115   if (parameters_.ui_task) {
116     parameters_.ui_task->Run();
117   } else {
118     base::MessageLoopForUI::current()->Run();
119   }
120   return true;
121 }
122 
PostMainMessageLoopRun()123 void CastBrowserMainParts::PostMainMessageLoopRun() {
124   cast_browser_process_->cast_service()->Stop();
125   cast_browser_process_.reset();
126 }
127 
128 }  // namespace shell
129 }  // namespace chromecast
130