ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / chromeos / login / helper.cc
blob69696fcda58e15ea83a4fd2946f353c44d462dfa
1 // Copyright (c) 2012 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/chromeos/login/helper.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/json/json_reader.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/chromeos/login/startup_utils.h"
12 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
13 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
14 #include "chrome/browser/chromeos/profiles/profile_helper.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "chromeos/chromeos_switches.h"
17 #include "chromeos/network/managed_network_configuration_handler.h"
18 #include "chromeos/network/network_handler.h"
19 #include "chromeos/network/network_state.h"
20 #include "chromeos/network/network_state_handler.h"
21 #include "components/guest_view/browser/guest_view_manager.h"
22 #include "content/public/browser/storage_partition.h"
23 #include "content/public/browser/web_contents.h"
24 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
25 #include "third_party/cros_system_api/dbus/service_constants.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/gfx/image/image_skia.h"
28 #include "ui/gfx/screen.h"
30 namespace chromeos {
32 namespace {
34 // Gets the WebContents instance of current login display. If there is none,
35 // returns nullptr.
36 content::WebContents* GetLoginWebContents() {
37 LoginDisplayHost* host = LoginDisplayHostImpl::default_host();
38 if (!host || !host->GetWebUILoginView())
39 return nullptr;
41 return host->GetWebUILoginView()->GetWebContents();
44 // Callback used by GetPartition below to return the first guest contents with a
45 // matching partition name.
46 bool FindGuestByPartitionName(const std::string& partition_name,
47 content::WebContents** out_guest_contents,
48 content::WebContents* guest_contents) {
49 std::string domain;
50 std::string name;
51 bool in_memory;
52 extensions::WebViewGuest::GetGuestPartitionConfigForSite(
53 guest_contents->GetSiteInstance()->GetSiteURL(), &domain, &name,
54 &in_memory);
55 if (partition_name != name)
56 return false;
58 *out_guest_contents = guest_contents;
59 return true;
62 // Gets the storage partition of guest contents of a given embedder.
63 // If a name is given, returns the partition associated with the name.
64 // Otherwise, returns the default shared in-memory partition. Returns nullptr if
65 // a matching partition could not be found.
66 content::StoragePartition* GetPartition(content::WebContents* embedder,
67 const std::string& partition_name) {
68 guest_view::GuestViewManager* manager =
69 guest_view::GuestViewManager::FromBrowserContext(
70 embedder->GetBrowserContext());
71 if (!manager)
72 return nullptr;
74 content::WebContents* guest_contents = nullptr;
75 manager->ForEachGuest(embedder, base::Bind(&FindGuestByPartitionName,
76 partition_name, &guest_contents));
78 return guest_contents ? content::BrowserContext::GetStoragePartition(
79 guest_contents->GetBrowserContext(),
80 guest_contents->GetSiteInstance())
81 : nullptr;
84 } // namespace
86 gfx::Rect CalculateScreenBounds(const gfx::Size& size) {
87 gfx::Rect bounds =
88 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().bounds();
89 if (!size.IsEmpty()) {
90 int horizontal_diff = bounds.width() - size.width();
91 int vertical_diff = bounds.height() - size.height();
92 bounds.Inset(horizontal_diff / 2, vertical_diff / 2);
94 return bounds;
97 int GetCurrentUserImageSize() {
98 // The biggest size that the profile picture is displayed at is currently
99 // 220px, used for the big preview on OOBE and Change Picture options page.
100 static const int kBaseUserImageSize = 220;
101 float scale_factor = gfx::Display::GetForcedDeviceScaleFactor();
102 if (scale_factor > 1.0f)
103 return static_cast<int>(scale_factor * kBaseUserImageSize);
104 return kBaseUserImageSize * gfx::ImageSkia::GetMaxSupportedScale();
107 namespace login {
109 NetworkStateHelper::NetworkStateHelper() {}
110 NetworkStateHelper::~NetworkStateHelper() {}
112 base::string16 NetworkStateHelper::GetCurrentNetworkName() const {
113 NetworkStateHandler* nsh = NetworkHandler::Get()->network_state_handler();
114 const NetworkState* network =
115 nsh->ConnectedNetworkByType(NetworkTypePattern::NonVirtual());
116 if (network) {
117 if (network->Matches(NetworkTypePattern::Ethernet()))
118 return l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
119 return base::UTF8ToUTF16(network->name());
122 network = nsh->ConnectingNetworkByType(NetworkTypePattern::NonVirtual());
123 if (network) {
124 if (network->Matches(NetworkTypePattern::Ethernet()))
125 return l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
126 return base::UTF8ToUTF16(network->name());
128 return base::string16();
131 void NetworkStateHelper::CreateNetworkFromOnc(
132 const std::string& onc_spec) const {
133 std::string error;
134 scoped_ptr<base::Value> root = base::JSONReader::ReadAndReturnError(
135 onc_spec, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error);
137 base::DictionaryValue* toplevel_onc = nullptr;
138 if (!root || !root->GetAsDictionary(&toplevel_onc)) {
139 LOG(ERROR) << "Invalid JSON Dictionary: " << error;
140 return;
143 NetworkHandler::Get()->managed_network_configuration_handler()->
144 CreateConfiguration(
145 "", *toplevel_onc,
146 base::Bind(&NetworkStateHelper::OnCreateConfiguration,
147 base::Unretained(this)),
148 base::Bind(&NetworkStateHelper::OnCreateConfigurationFailed,
149 base::Unretained(this)));
152 void NetworkStateHelper::OnCreateConfiguration(
153 const std::string& service_path) const {
154 // Do Nothing.
157 void NetworkStateHelper::OnCreateConfigurationFailed(
158 const std::string& error_name,
159 scoped_ptr<base::DictionaryValue> error_data) const {
160 LOG(ERROR) << "Failed to create network configuration: " << error_name;
163 bool NetworkStateHelper::IsConnected() const {
164 chromeos::NetworkStateHandler* nsh =
165 chromeos::NetworkHandler::Get()->network_state_handler();
166 return nsh->ConnectedNetworkByType(chromeos::NetworkTypePattern::Default()) !=
167 nullptr;
170 bool NetworkStateHelper::IsConnecting() const {
171 chromeos::NetworkStateHandler* nsh =
172 chromeos::NetworkHandler::Get()->network_state_handler();
173 return nsh->ConnectingNetworkByType(
174 chromeos::NetworkTypePattern::Default()) != nullptr;
177 content::StoragePartition* GetSigninPartition() {
178 content::WebContents* embedder = GetLoginWebContents();
179 if (!embedder)
180 return nullptr;
182 // Note the partition name must match the sign-in webview used. For now,
183 // this is the default unnamed, shared, in-memory partition.
184 return GetPartition(embedder, std::string());
187 net::URLRequestContextGetter* GetSigninContext() {
188 if (StartupUtils::IsWebviewSigninEnabled()) {
189 content::StoragePartition* signin_partition = GetSigninPartition();
191 // Special case for unit tests. There's no LoginDisplayHost thus no
192 // webview instance. TODO(nkostylev): Investigate if there's a better
193 // place to address this like dependency injection. http://crbug.com/477402
194 if (!signin_partition && !LoginDisplayHostImpl::default_host())
195 return ProfileHelper::GetSigninProfile()->GetRequestContext();
197 if (!signin_partition)
198 return nullptr;
200 return signin_partition->GetURLRequestContext();
203 return ProfileHelper::GetSigninProfile()->GetRequestContext();
206 } // namespace login
208 } // namespace chromeos