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 "apps/custom_launcher_page_contents.h"
6
7#include <string>
8
9#include "chrome/browser/chrome_notification_types.h"
10#include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
11#include "content/public/browser/render_view_host.h"
12#include "content/public/browser/site_instance.h"
13#include "content/public/browser/web_contents.h"
14#include "content/public/common/renderer_preferences.h"
15#include "extensions/browser/app_window/app_delegate.h"
16#include "extensions/browser/app_window/app_web_contents_helper.h"
17#include "extensions/browser/view_type_utils.h"
18#include "extensions/common/extension_messages.h"
19
20namespace apps {
21
22CustomLauncherPageContents::CustomLauncherPageContents(
23    scoped_ptr<extensions::AppDelegate> app_delegate,
24    const std::string& extension_id)
25    : app_delegate_(app_delegate.Pass()), extension_id_(extension_id) {
26}
27
28CustomLauncherPageContents::~CustomLauncherPageContents() {
29}
30
31void CustomLauncherPageContents::Initialize(content::BrowserContext* context,
32                                            const GURL& url) {
33  extension_function_dispatcher_.reset(
34      new extensions::ExtensionFunctionDispatcher(context, this));
35
36  web_contents_.reset(
37      content::WebContents::Create(content::WebContents::CreateParams(
38          context, content::SiteInstance::CreateForURL(context, url))));
39
40  Observe(web_contents());
41  web_contents_->GetMutableRendererPrefs()
42      ->browser_handles_all_top_level_requests = true;
43  web_contents_->GetRenderViewHost()->SyncRendererPrefs();
44
45  helper_.reset(new extensions::AppWebContentsHelper(
46      context, extension_id_, web_contents_.get(), app_delegate_.get()));
47  web_contents_->SetDelegate(this);
48
49  extensions::SetViewType(web_contents(), extensions::VIEW_TYPE_LAUNCHER_PAGE);
50
51  // This observer will activate the extension when it is navigated to, which
52  // allows Dispatcher to give it the proper context and makes it behave like an
53  // extension.
54  extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
55      web_contents());
56
57  web_contents_->GetController().LoadURL(url,
58                                         content::Referrer(),
59                                         ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
60                                         std::string());
61}
62
63content::WebContents* CustomLauncherPageContents::OpenURLFromTab(
64    content::WebContents* source,
65    const content::OpenURLParams& params) {
66  DCHECK_EQ(web_contents_.get(), source);
67  return helper_->OpenURLFromTab(params);
68}
69
70void CustomLauncherPageContents::AddNewContents(
71    content::WebContents* source,
72    content::WebContents* new_contents,
73    WindowOpenDisposition disposition,
74    const gfx::Rect& initial_pos,
75    bool user_gesture,
76    bool* was_blocked) {
77  app_delegate_->AddNewContents(new_contents->GetBrowserContext(),
78                                new_contents,
79                                disposition,
80                                initial_pos,
81                                user_gesture,
82                                was_blocked);
83}
84
85bool CustomLauncherPageContents::IsPopupOrPanel(
86    const content::WebContents* source) const {
87  return true;
88}
89
90bool CustomLauncherPageContents::ShouldSuppressDialogs() {
91  return true;
92}
93
94bool CustomLauncherPageContents::PreHandleGestureEvent(
95    content::WebContents* source,
96    const blink::WebGestureEvent& event) {
97  return extensions::AppWebContentsHelper::ShouldSuppressGestureEvent(event);
98}
99
100content::ColorChooser* CustomLauncherPageContents::OpenColorChooser(
101    content::WebContents* web_contents,
102    SkColor initial_color,
103    const std::vector<content::ColorSuggestion>& suggestionss) {
104  return app_delegate_->ShowColorChooser(web_contents, initial_color);
105}
106
107void CustomLauncherPageContents::RunFileChooser(
108    content::WebContents* tab,
109    const content::FileChooserParams& params) {
110  app_delegate_->RunFileChooser(tab, params);
111}
112
113void CustomLauncherPageContents::RequestToLockMouse(
114    content::WebContents* web_contents,
115    bool user_gesture,
116    bool last_unlocked_by_target) {
117  DCHECK_EQ(web_contents_.get(), web_contents);
118  helper_->RequestToLockMouse();
119}
120
121void CustomLauncherPageContents::RequestMediaAccessPermission(
122    content::WebContents* web_contents,
123    const content::MediaStreamRequest& request,
124    const content::MediaResponseCallback& callback) {
125  DCHECK_EQ(web_contents_.get(), web_contents);
126  helper_->RequestMediaAccessPermission(request, callback);
127}
128
129bool CustomLauncherPageContents::CheckMediaAccessPermission(
130    content::WebContents* web_contents,
131    const GURL& security_origin,
132    content::MediaStreamType type) {
133  DCHECK_EQ(web_contents_.get(), web_contents);
134  return helper_->CheckMediaAccessPermission(security_origin, type);
135}
136
137bool CustomLauncherPageContents::OnMessageReceived(
138    const IPC::Message& message) {
139  bool handled = true;
140  IPC_BEGIN_MESSAGE_MAP(CustomLauncherPageContents, message)
141  IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
142  IPC_MESSAGE_UNHANDLED(handled = false)
143  IPC_END_MESSAGE_MAP()
144  return handled;
145}
146
147extensions::WindowController*
148CustomLauncherPageContents::GetExtensionWindowController() const {
149  return NULL;
150}
151
152content::WebContents* CustomLauncherPageContents::GetAssociatedWebContents()
153    const {
154  return web_contents();
155}
156
157void CustomLauncherPageContents::OnRequest(
158    const ExtensionHostMsg_Request_Params& params) {
159  extension_function_dispatcher_->Dispatch(params,
160                                           web_contents_->GetRenderViewHost());
161}
162
163}  // namespace apps
164