Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / browser / app_window / app_window_contents.cc
blobe2e0e29959bf4c7e02c597322c40470e4d23477a
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 "extensions/browser/app_window/app_window_contents.h"
7 #include <string>
8 #include <utility>
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/resource_dispatcher_host.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/renderer_preferences.h"
19 #include "extensions/browser/app_window/native_app_window.h"
20 #include "extensions/common/extension_messages.h"
22 namespace extensions {
24 AppWindowContentsImpl::AppWindowContentsImpl(AppWindow* host) : host_(host) {}
26 AppWindowContentsImpl::~AppWindowContentsImpl() {}
28 void AppWindowContentsImpl::Initialize(content::BrowserContext* context,
29 const GURL& url) {
30 url_ = url;
32 web_contents_.reset(
33 content::WebContents::Create(content::WebContents::CreateParams(
34 context, content::SiteInstance::CreateForURL(context, url_))));
36 Observe(web_contents_.get());
37 web_contents_->GetMutableRendererPrefs()->
38 browser_handles_all_top_level_requests = true;
39 web_contents_->GetRenderViewHost()->SyncRendererPrefs();
42 void AppWindowContentsImpl::LoadContents(int32 creator_process_id) {
43 // If the new view is in the same process as the creator, block the created
44 // RVH from loading anything until the background page has had a chance to
45 // do any initialization it wants. If it's a different process, the new RVH
46 // shouldn't communicate with the background page anyway (e.g. sandboxed).
47 if (web_contents_->GetMainFrame()->GetProcess()->GetID() ==
48 creator_process_id) {
49 SuspendRenderFrameHost(web_contents_->GetMainFrame());
50 } else {
51 VLOG(1) << "AppWindow created in new process ("
52 << web_contents_->GetMainFrame()->GetProcess()->GetID()
53 << ") != creator (" << creator_process_id << "). Routing disabled.";
56 web_contents_->GetController().LoadURL(
57 url_, content::Referrer(), ui::PAGE_TRANSITION_LINK,
58 std::string());
61 void AppWindowContentsImpl::NativeWindowChanged(
62 NativeAppWindow* native_app_window) {
63 base::ListValue args;
64 base::DictionaryValue* dictionary = new base::DictionaryValue();
65 args.Append(dictionary);
66 host_->GetSerializedState(dictionary);
68 content::RenderFrameHost* rfh = web_contents_->GetMainFrame();
69 rfh->Send(new ExtensionMsg_MessageInvoke(
70 rfh->GetRoutingID(), host_->extension_id(), "app.window",
71 "updateAppWindowProperties", args, false));
74 void AppWindowContentsImpl::NativeWindowClosed() {
75 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
76 rvh->Send(new ExtensionMsg_AppWindowClosed(rvh->GetRoutingID()));
79 void AppWindowContentsImpl::DispatchWindowShownForTests() const {
80 base::ListValue args;
81 content::RenderFrameHost* rfh = web_contents_->GetMainFrame();
82 rfh->Send(new ExtensionMsg_MessageInvoke(
83 rfh->GetRoutingID(), host_->extension_id(), "app.window",
84 "appWindowShownForTests", args, false));
87 content::WebContents* AppWindowContentsImpl::GetWebContents() const {
88 return web_contents_.get();
91 WindowController* AppWindowContentsImpl::GetWindowController() const {
92 return nullptr;
95 bool AppWindowContentsImpl::OnMessageReceived(const IPC::Message& message) {
96 bool handled = true;
97 IPC_BEGIN_MESSAGE_MAP(AppWindowContentsImpl, message)
98 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions,
99 UpdateDraggableRegions)
100 IPC_MESSAGE_UNHANDLED(handled = false)
101 IPC_END_MESSAGE_MAP()
102 return handled;
105 void AppWindowContentsImpl::UpdateDraggableRegions(
106 const std::vector<DraggableRegion>& regions) {
107 host_->UpdateDraggableRegions(regions);
110 void AppWindowContentsImpl::SuspendRenderFrameHost(
111 content::RenderFrameHost* rfh) {
112 DCHECK(rfh);
113 content::BrowserThread::PostTask(
114 content::BrowserThread::IO, FROM_HERE,
115 base::Bind(&content::ResourceDispatcherHost::BlockRequestsForRoute,
116 base::Unretained(content::ResourceDispatcherHost::Get()),
117 rfh->GetProcess()->GetID(), rfh->GetRoutingID()));
120 } // namespace extensions