| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "blimp/engine/browser/blimp_window.h" | |
| 6 | |
| 7 #include "base/strings/string_util.h" | |
| 8 #include "blimp/engine/browser/blimp_browser_main_parts.h" | |
| 9 #include "blimp/engine/browser/blimp_content_browser_client.h" | |
| 10 #include "content/public/browser/navigation_controller.h" | |
| 11 #include "content/public/browser/navigation_entry.h" | |
| 12 #include "content/public/browser/render_view_host.h" | |
| 13 #include "content/public/browser/render_widget_host_view.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 | |
| 16 namespace blimp { | |
| 17 namespace engine { | |
| 18 | |
| 19 namespace { | |
| 20 const int kDefaultWindowWidthDip = 1; | |
| 21 const int kDefaultWindowHeightDip = 1; | |
| 22 | |
| 23 // TODO(haibinlu): cleanup BlimpWindows on shutdown. See crbug/540498. | |
| 24 typedef std::vector<BlimpWindow*> BlimpWindows; | |
| 25 base::LazyInstance<BlimpWindows>::Leaky g_instances = LAZY_INSTANCE_INITIALIZER; | |
| 26 | |
| 27 // Returns the default size if |size| has 0 for width and/or height; | |
| 28 // otherwise, returns |size|. | |
| 29 gfx::Size AdjustWindowSize(const gfx::Size& size) { | |
| 30 return size.IsEmpty() | |
| 31 ? gfx::Size(kDefaultWindowWidthDip, kDefaultWindowHeightDip) | |
| 32 : size; | |
| 33 } | |
| 34 } // namespace | |
| 35 | |
| 36 BlimpWindow::~BlimpWindow() { | |
| 37 BlimpWindows* instances = g_instances.Pointer(); | |
| 38 BlimpWindows::iterator it( | |
| 39 std::find(instances->begin(), instances->end(), this)); | |
| 40 DCHECK(it != instances->end()); | |
| 41 instances->erase(it); | |
| 42 } | |
| 43 | |
| 44 // static | |
| 45 void BlimpWindow::Create(content::BrowserContext* browser_context, | |
| 46 const GURL& url, | |
| 47 content::SiteInstance* site_instance, | |
| 48 const gfx::Size& initial_size) { | |
| 49 content::WebContents::CreateParams create_params(browser_context, | |
| 50 site_instance); | |
| 51 scoped_ptr<content::WebContents> web_contents( | |
| 52 content::WebContents::Create(create_params)); | |
| 53 BlimpWindow* win = DoCreate(web_contents.Pass(), initial_size); | |
| 54 if (!url.is_empty()) | |
| 55 win->LoadURL(url); | |
| 56 } | |
| 57 | |
| 58 void BlimpWindow::LoadURL(const GURL& url) { | |
| 59 content::NavigationController::LoadURLParams params(url); | |
| 60 params.transition_type = ui::PageTransitionFromInt( | |
| 61 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | |
| 62 web_contents_->GetController().LoadURLWithParams(params); | |
| 63 web_contents_->Focus(); | |
| 64 } | |
| 65 | |
| 66 void BlimpWindow::AddNewContents(content::WebContents* source, | |
| 67 content::WebContents* new_contents, | |
| 68 WindowOpenDisposition disposition, | |
| 69 const gfx::Rect& initial_rect, | |
| 70 bool user_gesture, | |
| 71 bool* was_blocked) { | |
| 72 DoCreate(scoped_ptr<content::WebContents>(new_contents), initial_rect.size()); | |
| 73 } | |
| 74 | |
| 75 content::WebContents* BlimpWindow::OpenURLFromTab( | |
| 76 content::WebContents* source, | |
| 77 const content::OpenURLParams& params) { | |
| 78 // CURRENT_TAB is the only one we implement for now. | |
| 79 if (params.disposition != CURRENT_TAB) | |
| 80 return nullptr; | |
| 81 // TOOD(haibinlu): add helper method to get LoadURLParams from OpenURLParams. | |
| 82 content::NavigationController::LoadURLParams load_url_params(params.url); | |
| 83 load_url_params.source_site_instance = params.source_site_instance; | |
| 84 load_url_params.referrer = params.referrer; | |
| 85 load_url_params.frame_tree_node_id = params.frame_tree_node_id; | |
| 86 load_url_params.transition_type = params.transition; | |
| 87 load_url_params.extra_headers = params.extra_headers; | |
| 88 load_url_params.should_replace_current_entry = | |
| 89 params.should_replace_current_entry; | |
| 90 | |
| 91 if (params.transferred_global_request_id != content::GlobalRequestID()) { | |
| 92 load_url_params.is_renderer_initiated = params.is_renderer_initiated; | |
| 93 load_url_params.transferred_global_request_id = | |
| 94 params.transferred_global_request_id; | |
| 95 } else if (params.is_renderer_initiated) { | |
| 96 load_url_params.is_renderer_initiated = true; | |
| 97 } | |
| 98 | |
| 99 source->GetController().LoadURLWithParams(load_url_params); | |
| 100 return source; | |
| 101 } | |
| 102 | |
| 103 void BlimpWindow::RequestToLockMouse(content::WebContents* web_contents, | |
| 104 bool user_gesture, | |
| 105 bool last_unlocked_by_target) { | |
| 106 web_contents->GotResponseToLockMouseRequest(true); | |
| 107 } | |
| 108 | |
| 109 void BlimpWindow::CloseContents(content::WebContents* source) { | |
| 110 delete this; | |
| 111 } | |
| 112 | |
| 113 void BlimpWindow::ActivateContents(content::WebContents* contents) { | |
| 114 contents->GetRenderViewHost()->Focus(); | |
| 115 } | |
| 116 | |
| 117 void BlimpWindow::DeactivateContents(content::WebContents* contents) { | |
| 118 contents->GetRenderViewHost()->Blur(); | |
| 119 } | |
| 120 | |
| 121 BlimpWindow::BlimpWindow(scoped_ptr<content::WebContents> web_contents) | |
| 122 : web_contents_(web_contents.Pass()) { | |
| 123 web_contents_->SetDelegate(this); | |
| 124 g_instances.Get().push_back(this); | |
| 125 } | |
| 126 | |
| 127 // static | |
| 128 BlimpWindow* BlimpWindow::DoCreate( | |
| 129 scoped_ptr<content::WebContents> web_contents, | |
| 130 const gfx::Size& initial_size) { | |
| 131 BlimpWindow* win = new BlimpWindow(web_contents.Pass()); | |
| 132 content::RenderWidgetHostView* host_view = | |
| 133 win->web_contents_->GetRenderWidgetHostView(); | |
| 134 if (host_view) | |
| 135 host_view->SetSize(AdjustWindowSize(initial_size)); | |
| 136 return win; | |
| 137 } | |
| 138 | |
| 139 } // namespace engine | |
| 140 } // namespace blimp | |
| OLD | NEW |