Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: tools/chrome_remote_control/chrome_remote_control/google_credentials_backend.py

Issue 10914237: Port perf.py's _LoginToGoogleAccount to chrome_remote_control (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 import sys
6 import time
7 import logging
8 import os
9 import chrome_remote_control
10 from credentials_backend import CredentialsBackend
11
12 class GoogleCredentialsBackend(CredentialsBackend):
13 label = 'google'
14
15 def __init__(self, tab):
16 self._tab = tab
17 self._logged_in = False
18
19 def LoginNeeded(self):
20 if not self._logged_in:
21 self._logged_in = self._LoginToGoogleAccount()
22
23 def LoginNoLongerNeeded(self):
dtu 2012/09/24 21:35:10 This is a placeholder for the future, right? What
hartmanng 2012/09/26 22:06:15 I added this in response to http://codereview.chro
24 pass
25
26 def GoogleAccountsLogin(self, username, password, url=None):
27 """Log into Google Accounts.
28
29 Attempts to login to Google by entering the username/password into the
30 google login page and click submit button.
31
32 Args:
33 username: users login input.
34 password: users login password input.
35 url: an alternative url for login page, if None, original one will be
36 used.
37 """
38 url = url or 'https://accounts.google.com/'
39
40 try:
41 logging.info('Loading %s...', url)
42 self._tab.page.Navigate(url)
43 time.sleep(2) # Wait for unpredictable redirects.
tonyg 2012/09/24 20:41:01 Looks brittle. Is there anything better to WaitFor
hartmanng 2012/09/26 22:06:15 Done.
44 self._tab.WaitForDocumentReadyStateToBeInteractiveOrBetter()
45 logging.info('Loaded page: %s', url)
46
47 email_id = 'document.getElementById("Email").value = "%s"; ' % username
48 password = 'document.getElementById("Passwd").value = "%s"; ' % password
49 self._tab.runtime.Execute(email_id)
50 self._tab.runtime.Execute(password)
51
52 self.SubmitFormAndWait('gaia_loginform')
53
54 return True
55 except chrome_remote_control.TimeoutException:
56 logging.warning('Timed out while loading: %s', url)
57 print 'Timed out while loading: ', url
58 return False
59
60
61 def _LoginToGoogleAccount(self):
62 """Logs in to a test Google account.
63
64 Login with user-defined credentials if they exist.
65 Else fail.
66
67 Raises:
68 RuntimeError: if could not get credential information.
69 """
70 config = CredentialsBackend._GetConfig(
71 {'google_username': None,
72 'google_password': None,
73 'google_account_url': 'https://accounts.google.com/'})
74 google_account_url = config.get('google_account_url')
75 username = config.get('google_username')
76 password = config.get('google_password')
77 if username and password:
78 logging.info(
79 'Using google account credential from %s',
80 self._config_file, 'credentials.json')
81 else:
82 message = 'No credentials could be found. ' \
83 'Please specify credential information in %s.' \
84 % self._config_file
85 raise RuntimeError(message)
86 return self.GoogleAccountsLogin(username, password, url=google_account_url)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698