Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / app_list / start_page_service.cc
blob151bd658cc2eb1ab06793d1e7805fbc9cc6b5ec8
1 // Copyright 2013 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.
5 #include "chrome/browser/ui/app_list/start_page_service.h"
7 #include <string>
9 #include "base/command_line.h"
10 #include "base/memory/singleton.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/extension_system_factory.h"
13 #include "chrome/browser/extensions/install_tracker_factory.h"
14 #include "chrome/browser/media/media_stream_infobar_delegate.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/app_list/recommended_apps.h"
17 #include "chrome/browser/ui/app_list/start_page_observer.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/url_constants.h"
20 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
21 #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h"
22 #include "content/public/browser/notification_details.h"
23 #include "content/public/browser/notification_observer.h"
24 #include "content/public/browser/notification_registrar.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/notification_source.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_contents_delegate.h"
29 #include "extensions/common/extension.h"
30 #include "ui/app_list/app_list_switches.h"
32 namespace app_list {
34 class StartPageService::Factory : public BrowserContextKeyedServiceFactory {
35 public:
36 static StartPageService* GetForProfile(Profile* profile) {
37 if (!CommandLine::ForCurrentProcess()->HasSwitch(
38 ::switches::kShowAppListStartPage) &&
39 !app_list::switches::IsVoiceSearchEnabled()) {
40 return NULL;
43 return static_cast<StartPageService*>(
44 GetInstance()->GetServiceForBrowserContext(profile, true));
47 static Factory* GetInstance() {
48 return Singleton<Factory>::get();
51 private:
52 friend struct DefaultSingletonTraits<Factory>;
54 Factory()
55 : BrowserContextKeyedServiceFactory(
56 "AppListStartPageService",
57 BrowserContextDependencyManager::GetInstance()) {
58 DependsOn(extensions::ExtensionSystemFactory::GetInstance());
59 DependsOn(extensions::InstallTrackerFactory::GetInstance());
62 virtual ~Factory() {}
64 // BrowserContextKeyedServiceFactory overrides:
65 virtual BrowserContextKeyedService* BuildServiceInstanceFor(
66 content::BrowserContext* context) const OVERRIDE {
67 Profile* profile = static_cast<Profile*>(context);
68 return new StartPageService(profile);
71 DISALLOW_COPY_AND_ASSIGN(Factory);
74 class StartPageService::ProfileDestroyObserver
75 : public content::NotificationObserver {
76 public:
77 explicit ProfileDestroyObserver(StartPageService* service)
78 : service_(service) {
79 registrar_.Add(this,
80 chrome::NOTIFICATION_PROFILE_DESTROYED,
81 content::Source<Profile>(service_->profile()));
83 virtual ~ProfileDestroyObserver() {}
85 private:
86 // content::NotificationObserver
87 virtual void Observe(int type,
88 const content::NotificationSource& source,
89 const content::NotificationDetails& details) OVERRIDE {
90 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type);
91 DCHECK_EQ(service_->profile(), content::Source<Profile>(source).ptr());
92 service_->Shutdown();
95 StartPageService* service_; // Owner of this class.
96 content::NotificationRegistrar registrar_;
98 DISALLOW_COPY_AND_ASSIGN(ProfileDestroyObserver);
101 class StartPageService::StartPageWebContentsDelegate
102 : public content::WebContentsDelegate {
103 public:
104 StartPageWebContentsDelegate() {}
105 virtual ~StartPageWebContentsDelegate() {}
107 virtual void RequestMediaAccessPermission(
108 content::WebContents* web_contents,
109 const content::MediaStreamRequest& request,
110 const content::MediaResponseCallback& callback) OVERRIDE {
111 if (MediaStreamInfoBarDelegate::Create(web_contents, request, callback))
112 NOTREACHED() << "Media stream not allowed for WebUI";
115 private:
116 DISALLOW_COPY_AND_ASSIGN(StartPageWebContentsDelegate);
119 // static
120 StartPageService* StartPageService::Get(Profile* profile) {
121 return Factory::GetForProfile(profile);
124 StartPageService::StartPageService(Profile* profile)
125 : profile_(profile),
126 profile_destroy_observer_(new ProfileDestroyObserver(this)),
127 recommended_apps_(new RecommendedApps(profile)) {
128 contents_.reset(content::WebContents::Create(
129 content::WebContents::CreateParams(profile_)));
130 contents_delegate_.reset(new StartPageWebContentsDelegate());
131 contents_->SetDelegate(contents_delegate_.get());
133 GURL url(chrome::kChromeUIAppListStartPageURL);
134 CommandLine* command_line = CommandLine::ForCurrentProcess();
135 if (command_line->HasSwitch(::switches::kAppListStartPageURL)) {
136 url = GURL(
137 command_line->GetSwitchValueASCII(::switches::kAppListStartPageURL));
140 contents_->GetController().LoadURL(
141 url,
142 content::Referrer(),
143 content::PAGE_TRANSITION_AUTO_TOPLEVEL,
144 std::string());
147 StartPageService::~StartPageService() {}
149 void StartPageService::AddObserver(StartPageObserver* observer) {
150 observers_.AddObserver(observer);
153 void StartPageService::RemoveObserver(StartPageObserver* observer) {
154 observers_.RemoveObserver(observer);
157 void StartPageService::ToggleSpeechRecognition() {
158 contents_->GetWebUI()->CallJavascriptFunction(
159 "appList.startPage.toggleSpeechRecognition");
162 content::WebContents* StartPageService::GetStartPageContents() {
163 return CommandLine::ForCurrentProcess()->HasSwitch(
164 ::switches::kShowAppListStartPage) ? contents_.get() : NULL;
167 content::WebContents* StartPageService::GetSpeechRecognitionContents() {
168 return app_list::switches::IsVoiceSearchEnabled() ? contents_.get() : NULL;
171 void StartPageService::OnSpeechResult(
172 const base::string16& query, bool is_final) {
173 FOR_EACH_OBSERVER(StartPageObserver,
174 observers_,
175 OnSpeechResult(query, is_final));
178 void StartPageService::OnSpeechSoundLevelChanged(int16 level) {
179 FOR_EACH_OBSERVER(StartPageObserver,
180 observers_,
181 OnSpeechSoundLevelChanged(level));
184 void StartPageService::OnSpeechRecognitionStateChanged(
185 SpeechRecognitionState new_state) {
186 FOR_EACH_OBSERVER(StartPageObserver,
187 observers_,
188 OnSpeechRecognitionStateChanged(new_state));
191 void StartPageService::Shutdown() {
192 contents_.reset();
195 } // namespace app_list